// ── Core State & Helpers ──────────────────────────────────────────────
let currentConfig = null;
let tableFieldCache = {};
let currentKeyColumns = [];
let clickActions = [];
let isPageUnloading = false;
let allPagesStates = {};

const defaultConfig = {
    pageTypes: ['list'],
    functionality: {
        spreadsheet: false,
        updateSelected: false,
        inlineEdit: false,
        inlineAdd: false,
        deleteRecords: true,
        copyRecords: false
    },
    settings: {},
    keyColumns: []
};

async function autoDetectPrimaryKey(tableName) {
    if (!tableFieldCache[tableName]) {
        try {
            const res = await fetch(`${baseUrl}/project/${projectSlug}/tables/details/${tableName}`);
            const data = await res.json();
            if (data.success) tableFieldCache[tableName] = data.columns;
            else return;
        } catch (err) { return; }
    }
    const pkCols = tableFieldCache[tableName].filter(col => col.COLUMN_KEY === 'PRI').map(col => col.COLUMN_NAME);
    currentKeyColumns = pkCols;
    if (currentConfig) currentConfig.keyColumns = pkCols;
}

window.defaultConfig = defaultConfig;
window.autoDetectPrimaryKey = autoDetectPrimaryKey;
window.allPagesStates = allPagesStates;