"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveConfig = resolveConfig;
// ===================== Field type normalization =====================
function resolveFieldTypes(enabled, options) {
    const isEnabled = (type) => {
        if (!enabled)
            return false;
        return enabled.includes(type);
    };
    return {
        ai: {
            enabled: isEnabled('ai'),
            // [TODO] 后续按需开放
            // ...(isEnabled('ai') && options?.ai?.subTypes ? { allowedSubTypes: options.ai.subTypes } : {}),
        },
        lookup: {
            enabled: isEnabled('lookup'),
            ...(isEnabled('lookup') && (options === null || options === void 0 ? void 0 : options.lookup)
                ? {
                    hiddenSubTable: options.lookup.hiddenSubTable,
                    addFieldCallback: options.lookup.addFieldCallback,
                    deleteFieldCallback: options.lookup.deleteFieldCallback,
                }
                : {}),
        },
        formula: {
            enabled: isEnabled('formula'),
            // [TODO] 后续按需开放
            // ...(isEnabled('formula') && options?.formula?.maxNestingDepth != null
            //   ? { maxNestingDepth: options.formula.maxNestingDepth }
            //   : {}),
        },
    };
}
// ===================== View type normalization =====================
const DEFAULT_TASK_TOOLBAR_PERMISSIONS = {
    cardManagement: true,
    filter: true,
    sort: true,
};
const TASK_DEFAULTS = {
    enableSummary: true,
    toolbarVisible: true,
    toolbarPermissions: { ...DEFAULT_TASK_TOOLBAR_PERMISSIONS },
};
const DEFAULT_TOOLBAR_PERMISSIONS = {
    fieldManagement: true,
    filter: true,
    sort: true,
    group: true,
    colorFill: true,
};
function resolveViewTypes(raw) {
    if (raw === undefined || raw === null) {
        return {
            grid: {
                enabled: true,
                toolbarVisible: true,
                toolbarPermissions: { ...DEFAULT_TOOLBAR_PERMISSIONS },
            },
            task: { enabled: true, ...TASK_DEFAULTS },
            smartFollow: { enabled: false, enableDetailSummary: false },
        };
    }
    // grid: always enabled, cannot be disabled
    const gridRaw = raw.grid;
    let gridCap;
    if (!gridRaw) {
        gridCap = {
            enabled: true,
            toolbarVisible: true,
            toolbarPermissions: { ...DEFAULT_TOOLBAR_PERMISSIONS },
        };
    }
    else {
        const tb = gridRaw.toolbar;
        if (tb === false) {
            gridCap = {
                enabled: true,
                toolbarVisible: false,
                toolbarPermissions: { ...DEFAULT_TOOLBAR_PERMISSIONS },
            };
        }
        else if (tb && typeof tb === 'object') {
            gridCap = {
                enabled: true,
                toolbarVisible: true,
                toolbarPermissions: {
                    fieldManagement: tb.fieldManagement !== false,
                    filter: tb.filter !== false,
                    sort: tb.sort !== false,
                    group: tb.group !== false,
                    colorFill: tb.colorFill !== false,
                },
            };
        }
        else {
            gridCap = {
                enabled: true,
                toolbarVisible: true,
                toolbarPermissions: { ...DEFAULT_TOOLBAR_PERMISSIONS },
            };
        }
    }
    // task
    const taskRaw = raw.task;
    let taskCap;
    if (taskRaw === false) {
        taskCap = {
            enabled: false,
            enableSummary: false,
            toolbarVisible: true,
            toolbarPermissions: { ...DEFAULT_TASK_TOOLBAR_PERMISSIONS },
        };
    }
    else if (taskRaw && typeof taskRaw === 'object') {
        const tb = taskRaw.toolbar;
        let taskToolbarVisible = true;
        let taskToolbarPerms = { ...DEFAULT_TASK_TOOLBAR_PERMISSIONS };
        if (tb === false) {
            taskToolbarVisible = false;
        }
        else if (tb && typeof tb === 'object') {
            taskToolbarPerms = {
                cardManagement: tb.cardManagement !== false,
                filter: tb.filter !== false,
                sort: tb.sort !== false,
            };
        }
        taskCap = {
            enabled: true,
            enableSummary: taskRaw.enableSummary !== false,
            toolbarVisible: taskToolbarVisible,
            toolbarPermissions: taskToolbarPerms,
        };
    }
    else {
        taskCap = { enabled: true, ...TASK_DEFAULTS };
    }
    // 智能跟单视图配置：省略默认禁用，true 启用默认配置，false 禁用，配置对象表示启用并定制
    const sfRaw = raw.smartFollow;
    let sfCap;
    if (sfRaw === false || sfRaw === undefined || sfRaw === null) {
        sfCap = { enabled: false, enableDetailSummary: false };
    }
    else if (sfRaw === true) {
        sfCap = { enabled: true, enableDetailSummary: true };
    }
    else if (sfRaw && typeof sfRaw === 'object') {
        sfCap = { enabled: true, enableDetailSummary: sfRaw.enableDetailSummary };
    }
    else {
        sfCap = { enabled: false, enableDetailSummary: false };
    }
    return { grid: gridCap, task: taskCap, smartFollow: sfCap };
}
function resolveConfig(enabledFieldTypes, fieldTypeOptions, viewTypes) {
    return {
        fieldTypes: resolveFieldTypes(enabledFieldTypes, fieldTypeOptions),
        viewTypes: resolveViewTypes(viewTypes),
    };
}
