import { getCubePlatformMeta } from './get-cube-platform-meta';
import { loadUserCubeConfigFromPlatform, readAiColumnsConfigFromComponents, } from './process-user-cube-config';
function normalizeAiColumns(raw) {
    var _a, _b, _c, _d, _e;
    const out = [];
    for (const item of raw) {
        if (!item || typeof item !== 'object')
            continue;
        const o = item;
        if (typeof o.columnCode !== 'string' || typeof o.tableId !== 'string')
            continue;
        out.push({
            tableId: o.tableId,
            columnCode: o.columnCode,
            columnName: String((_a = o.columnName) !== null && _a !== void 0 ? _a : ''),
            scenarioId: String((_b = o.scenarioId) !== null && _b !== void 0 ? _b : ''),
            modelName: String((_c = o.modelName) !== null && _c !== void 0 ? _c : ''),
            gatewayType: String((_d = o.gatewayType) !== null && _d !== void 0 ? _d : ''),
            promptTemplate: String((_e = o.promptTemplate) !== null && _e !== void 0 ? _e : ''),
        });
    }
    return out;
}
/**
 * 源码未配业务 URL：getList → 按 tableId 读 `components[].config.columns.config`。
 */
export async function fetchAiColumnsFromCube(tableId, bizRequestConfig) {
    if (!tableId) {
        return { columns: [], userCubeConfigRowId: '' };
    }
    const meta = getCubePlatformMeta(tableId, bizRequestConfig);
    if (!meta) {
        return { columns: [], userCubeConfigRowId: '' };
    }
    try {
        const { userCubeConfig, userCubeConfigRowId } = await loadUserCubeConfigFromPlatform(meta, bizRequestConfig);
        const raw = readAiColumnsConfigFromComponents(userCubeConfig.components, tableId);
        return { columns: normalizeAiColumns(raw), userCubeConfigRowId };
    }
    catch {
        return { columns: [], userCubeConfigRowId: '' };
    }
}
