/**
 * 源码场景、无魔方 window 实例时，直连魔方开放平台持久化 AI 列（getList / createOrUpdate + HTTP）。
 * 与 {@link ai-column-service.ts} 中原有 `__AI_Field_Fun__` 链路隔离，避免改动默认分支。
 */
import { aiRequest } from '../../../service/ai-request.js';
import { isCubeDirect } from '../../cube-platform/isCubeDirect';
import { fetchAiColumnsFromCube, getCubePlatformMeta, persistAiColumnsToCube, } from '../../cube-platform';
import { getCubePlatformUserId } from '../../cube-platform/open-platform/get-cube-platform-user-id';
import { removeAiColumnFromComponents, upsertAiColumnInComponents, } from '../../cube-platform/open-platform/process-user-cube-config';
/** @deprecated 语义同 {@link isCubeDirect}，保留兼容旧引用 */
export function isAiColumnCubePlatformSourceMode(tableId, bizRequestConfig) {
    return isCubeDirect(tableId, bizRequestConfig);
}
export async function queryAiTableColumnsViaCubePlatform(params, cubePlatformCtx, bizRequestConfig) {
    var _a;
    const meta = getCubePlatformMeta(params.tableId, bizRequestConfig);
    if (!meta) {
        return { success: true, data: [] };
    }
    const { columns, userCubeConfigRowId } = await fetchAiColumnsFromCube(params.tableId, bizRequestConfig);
    if (userCubeConfigRowId) {
        (_a = cubePlatformCtx === null || cubePlatformCtx === void 0 ? void 0 : cubePlatformCtx.onUserCubeConfigRowId) === null || _a === void 0 ? void 0 : _a.call(cubePlatformCtx, userCubeConfigRowId);
    }
    return { success: true, data: columns };
}
export async function createOrModifyAiColumnViaCubePlatform(params, cubePlatformCtx, bizRequestConfig) {
    var _a, _b, _c;
    const meta = getCubePlatformMeta(params.tableId, bizRequestConfig);
    if (!meta) {
        return {
            success: false,
            errorMsg: 'cube platform pageId/appCode unavailable (set bizRequestConfig.cubePlatform.getListRequestConfig.data)',
        };
    }
    const gw = window._GATEWAY_PF;
    const userId = getCubePlatformUserId(meta);
    await aiRequest({
        path: '/aiTableColumns/createOrModify.do',
        method: 'POST',
        data: {
            ...params,
            userId,
            userName: (_a = meta.userName) !== null && _a !== void 0 ? _a : gw === null || gw === void 0 ? void 0 : gw.userName,
            creator: userId,
        },
    });
    const persist = await persistAiColumnsToCube({
        meta,
        userCubeConfigRowIdHint: cubePlatformCtx === null || cubePlatformCtx === void 0 ? void 0 : cubePlatformCtx.userCubeConfigRowIdHint,
        bizRequestConfig,
        mutateComponents: (components) => upsertAiColumnInComponents(components, {
            tableId: params.tableId,
            nodeId: params.nodeId,
            column: params,
        }),
    });
    if (!persist.success) {
        return {
            success: false,
            errorMsg: String((_b = persist.errorMsg) !== null && _b !== void 0 ? _b : 'unknown'),
        };
    }
    if (persist.userCubeConfigRowId) {
        (_c = cubePlatformCtx === null || cubePlatformCtx === void 0 ? void 0 : cubePlatformCtx.onUserCubeConfigRowId) === null || _c === void 0 ? void 0 : _c.call(cubePlatformCtx, persist.userCubeConfigRowId);
    }
    return { success: true };
}
export async function queryTableTasksViaCubePlatform(params, cubePlatformCtx, bizRequestConfig) {
    var _a;
    const meta = getCubePlatformMeta(params.tableId, bizRequestConfig);
    if (!meta) {
        return {
            success: true,
            data: { runningTasks: [], finishedTasks: [], failedTasks: [] },
        };
    }
    const { columns, userCubeConfigRowId } = await fetchAiColumnsFromCube(params.tableId, bizRequestConfig);
    if (userCubeConfigRowId) {
        (_a = cubePlatformCtx === null || cubePlatformCtx === void 0 ? void 0 : cubePlatformCtx.onUserCubeConfigRowId) === null || _a === void 0 ? void 0 : _a.call(cubePlatformCtx, userCubeConfigRowId);
    }
    const codes = columns.map((c) => c.columnCode).filter(Boolean);
    if (!codes.length) {
        return {
            success: true,
            data: { runningTasks: [], finishedTasks: [], failedTasks: [] },
        };
    }
    const taskData = await aiRequest({
        path: '/aiGridTasks/queryTableTasks.do',
        method: 'GET',
        params: {
            tableId: params.tableId,
            columnCode: codes.join(','),
        },
    });
    return { success: true, data: taskData };
}
export async function removeAiColumnViaCubePlatform(params, cubePlatformCtx, bizRequestConfig) {
    var _a, _b;
    const meta = getCubePlatformMeta(params.tableId, bizRequestConfig);
    if (!meta) {
        return {
            success: false,
            errorMsg: 'cube platform pageId/appCode unavailable (set bizRequestConfig.cubePlatform.getListRequestConfig.data)',
        };
    }
    await aiRequest({
        path: '/aiTableColumns/remove.do',
        method: 'POST',
        data: params,
    });
    const persist = await persistAiColumnsToCube({
        meta,
        userCubeConfigRowIdHint: cubePlatformCtx === null || cubePlatformCtx === void 0 ? void 0 : cubePlatformCtx.userCubeConfigRowIdHint,
        bizRequestConfig,
        mutateComponents: (components) => removeAiColumnFromComponents(components, {
            tableId: params.tableId,
            columnCode: params.columnCode,
        }),
    });
    if (!persist.success) {
        return {
            success: false,
            errorMsg: String((_a = persist.errorMsg) !== null && _a !== void 0 ? _a : 'unknown'),
        };
    }
    if (persist.userCubeConfigRowId) {
        (_b = cubePlatformCtx === null || cubePlatformCtx === void 0 ? void 0 : cubePlatformCtx.onUserCubeConfigRowId) === null || _b === void 0 ? void 0 : _b.call(cubePlatformCtx, persist.userCubeConfigRowId);
    }
    return { success: true };
}
export { isCubeDirect };
