"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isCubeDirect = void 0;
exports.isAiColumnCubePlatformSourceMode = isAiColumnCubePlatformSourceMode;
exports.queryAiTableColumnsViaCubePlatform = queryAiTableColumnsViaCubePlatform;
exports.createOrModifyAiColumnViaCubePlatform = createOrModifyAiColumnViaCubePlatform;
exports.queryTableTasksViaCubePlatform = queryTableTasksViaCubePlatform;
exports.removeAiColumnViaCubePlatform = removeAiColumnViaCubePlatform;
/**
 * 源码场景、无魔方 window 实例时，直连魔方开放平台持久化 AI 列（getList / createOrUpdate + HTTP）。
 * 与 {@link ai-column-service.ts} 中原有 `__AI_Field_Fun__` 链路隔离，避免改动默认分支。
 */
const ai_request_1 = require("../../../service/ai-request.js");
const isCubeDirect_1 = require("../../cube-platform/isCubeDirect");
Object.defineProperty(exports, "isCubeDirect", { enumerable: true, get: function () { return isCubeDirect_1.isCubeDirect; } });
const cube_platform_1 = require("../../cube-platform");
const get_cube_platform_user_id_1 = require("../../cube-platform/open-platform/get-cube-platform-user-id");
const process_user_cube_config_1 = require("../../cube-platform/open-platform/process-user-cube-config");
/** @deprecated 语义同 {@link isCubeDirect}，保留兼容旧引用 */
function isAiColumnCubePlatformSourceMode(tableId, bizRequestConfig) {
    return (0, isCubeDirect_1.isCubeDirect)(tableId, bizRequestConfig);
}
async function queryAiTableColumnsViaCubePlatform(params, cubePlatformCtx, bizRequestConfig) {
    var _a;
    const meta = (0, cube_platform_1.getCubePlatformMeta)(params.tableId, bizRequestConfig);
    if (!meta) {
        return { success: true, data: [] };
    }
    const { columns, userCubeConfigRowId } = await (0, cube_platform_1.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 };
}
async function createOrModifyAiColumnViaCubePlatform(params, cubePlatformCtx, bizRequestConfig) {
    var _a, _b, _c;
    const meta = (0, cube_platform_1.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 = (0, get_cube_platform_user_id_1.getCubePlatformUserId)(meta);
    await (0, ai_request_1.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 (0, cube_platform_1.persistAiColumnsToCube)({
        meta,
        userCubeConfigRowIdHint: cubePlatformCtx === null || cubePlatformCtx === void 0 ? void 0 : cubePlatformCtx.userCubeConfigRowIdHint,
        bizRequestConfig,
        mutateComponents: (components) => (0, process_user_cube_config_1.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 };
}
async function queryTableTasksViaCubePlatform(params, cubePlatformCtx, bizRequestConfig) {
    var _a;
    const meta = (0, cube_platform_1.getCubePlatformMeta)(params.tableId, bizRequestConfig);
    if (!meta) {
        return {
            success: true,
            data: { runningTasks: [], finishedTasks: [], failedTasks: [] },
        };
    }
    const { columns, userCubeConfigRowId } = await (0, cube_platform_1.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 (0, ai_request_1.aiRequest)({
        path: '/aiGridTasks/queryTableTasks.do',
        method: 'GET',
        params: {
            tableId: params.tableId,
            columnCode: codes.join(','),
        },
    });
    return { success: true, data: taskData };
}
async function removeAiColumnViaCubePlatform(params, cubePlatformCtx, bizRequestConfig) {
    var _a, _b;
    const meta = (0, cube_platform_1.getCubePlatformMeta)(params.tableId, bizRequestConfig);
    if (!meta) {
        return {
            success: false,
            errorMsg: 'cube platform pageId/appCode unavailable (set bizRequestConfig.cubePlatform.getListRequestConfig.data)',
        };
    }
    await (0, ai_request_1.aiRequest)({
        path: '/aiTableColumns/remove.do',
        method: 'POST',
        data: params,
    });
    const persist = await (0, cube_platform_1.persistAiColumnsToCube)({
        meta,
        userCubeConfigRowIdHint: cubePlatformCtx === null || cubePlatformCtx === void 0 ? void 0 : cubePlatformCtx.userCubeConfigRowIdHint,
        bizRequestConfig,
        mutateComponents: (components) => (0, process_user_cube_config_1.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 };
}
