"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isCubeDirect = exports.isAiColumnCubePlatformSourceMode = void 0;
exports.createOrModifyAiColumn = createOrModifyAiColumn;
exports.queryAiTableColumns = queryAiTableColumns;
exports.queryTableTasks = queryTableTasks;
exports.submitTasks = submitTasks;
exports.pollTasks = pollTasks;
exports.removeAiColumn = removeAiColumn;
exports.generateAiSummary = generateAiSummary;
exports.submitAiColumnPreview = submitAiColumnPreview;
exports.queryAiColumnPreview = queryAiColumnPreview;
exports.updateAiCellData = updateAiCellData;
const i18n_1 = __importDefault(require("../../../locales/i18n.js"));
const ai_request_1 = require("../../../service/ai-request.js");
const ai_column_cube_platform_bridge_1 = require("./ai-column-cube-platform-bridge");
var ai_column_cube_platform_bridge_2 = require("./ai-column-cube-platform-bridge");
Object.defineProperty(exports, "isAiColumnCubePlatformSourceMode", { enumerable: true, get: function () { return ai_column_cube_platform_bridge_2.isAiColumnCubePlatformSourceMode; } });
Object.defineProperty(exports, "isCubeDirect", { enumerable: true, get: function () { return ai_column_cube_platform_bridge_2.isCubeDirect; } });
// ==================== API 服务方法 ====================
/**
 * 新增或修改 AI 列
 */
async function createOrModifyAiColumn(params, bizRequestConfig, cubePlatformCtx) {
    var _a, _b;
    try {
        if ((0, ai_column_cube_platform_bridge_1.isAiColumnCubePlatformSourceMode)(params.tableId, bizRequestConfig)) {
            return (0, ai_column_cube_platform_bridge_1.createOrModifyAiColumnViaCubePlatform)(params, cubePlatformCtx, bizRequestConfig);
        }
        const aiFieldFun = (_a = window.__CN_CUBE_INSTANCE__) === null || _a === void 0 ? void 0 : _a.__AI_Field_Fun__;
        if (!aiFieldFun) {
            return {
                success: false,
                errorMsg: '__CN_CUBE_INSTANCE__ or __AI_Field_Fun__ is not available',
            };
        }
        const result = await aiFieldFun.createOrModifyAiColumn(params);
        return ((_b = result) !== null && _b !== void 0 ? _b : {
            success: false,
            errorMsg: 'createOrModifyAiColumn returned empty result',
        });
    }
    catch (error) {
        console.warn(error);
        return { success: false, errorMsg: error === null || error === void 0 ? void 0 : error.message };
    }
}
/**
 * 查询表格所有 AI 列
 */
async function queryAiTableColumns(params, bizRequestConfig, cubePlatformCtx) {
    try {
        if ((0, ai_column_cube_platform_bridge_1.isAiColumnCubePlatformSourceMode)(params.tableId, bizRequestConfig)) {
            return (0, ai_column_cube_platform_bridge_1.queryAiTableColumnsViaCubePlatform)(params, cubePlatformCtx, bizRequestConfig);
        }
        const result = await new Promise((resolve) => {
            setTimeout(() => {
                var _a;
                const aiFieldFun = (_a = window.__CN_CUBE_INSTANCE__) === null || _a === void 0 ? void 0 : _a.__AI_Field_Fun__;
                const data = aiFieldFun === null || aiFieldFun === void 0 ? void 0 : aiFieldFun.getAiColumn(params.tableId);
                resolve({ data, success: true });
            }, 100);
        });
        return result;
    }
    catch (error) {
        console.warn(error);
    }
}
/**
 * 查询全表任务
 */
async function queryTableTasks(params, bizRequestConfig, cubePlatformCtx) {
    var _a, _b;
    try {
        if ((_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.aiColumn) === null || _a === void 0 ? void 0 : _a.queryTableTasksUrl) {
            const taskData = await (0, ai_request_1.aiRequest)({
                url: bizRequestConfig.aiColumn.queryTableTasksUrl,
                method: 'GET',
                params: { tableId: params.tableId },
            });
            return { success: true, data: taskData };
        }
        if ((0, ai_column_cube_platform_bridge_1.isAiColumnCubePlatformSourceMode)(params.tableId, bizRequestConfig)) {
            return (0, ai_column_cube_platform_bridge_1.queryTableTasksViaCubePlatform)(params, cubePlatformCtx, bizRequestConfig);
        }
        const aiFieldFun = (_b = window.__CN_CUBE_INSTANCE__) === null || _b === void 0 ? void 0 : _b.__AI_Field_Fun__;
        const result = await (aiFieldFun === null || aiFieldFun === void 0 ? void 0 : aiFieldFun.queryTableTasks(params.tableId));
        return result;
    }
    catch (error) {
        console.warn(error);
    }
}
/**
 * 提交任务
 */
async function submitTasks(params, bizRequestConfig) {
    var _a;
    try {
        const taskData = await (0, ai_request_1.aiRequest)({
            url: (_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.aiColumn) === null || _a === void 0 ? void 0 : _a.submitTasksUrl,
            path: '/aiGridTasks/submitTask.do',
            method: 'POST',
            data: params,
        });
        return { success: true, data: taskData };
    }
    catch (error) {
        console.warn(error);
        return {
            success: false,
            data: { runningTasks: [], finishedTasks: [], failedTasks: [] },
            errorMsg: error === null || error === void 0 ? void 0 : error.message,
        };
    }
}
/**
 * 轮询任务
 */
async function pollTasks(params, bizRequestConfig) {
    var _a;
    try {
        const taskData = await (0, ai_request_1.aiRequest)({
            url: (_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.aiColumn) === null || _a === void 0 ? void 0 : _a.pollTasksUrl,
            path: '/aiGridTasks/queryTask.do',
            method: 'POST',
            data: params,
        });
        return { success: true, data: taskData };
    }
    catch (error) {
        console.warn(error);
        return {
            success: false,
            data: { runningTasks: [], finishedTasks: [], failedTasks: [] },
            errorMsg: error === null || error === void 0 ? void 0 : error.message,
        };
    }
}
/**
 * 删除 AI 列
 */
async function removeAiColumn(params, bizRequestConfig, cubePlatformCtx) {
    var _a, _b;
    try {
        if ((_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.aiColumn) === null || _a === void 0 ? void 0 : _a.removeColumnUrl) {
            await (0, ai_request_1.aiRequest)({
                url: bizRequestConfig.aiColumn.removeColumnUrl,
                method: 'POST',
                data: params,
            });
            return { success: true };
        }
        if ((0, ai_column_cube_platform_bridge_1.isAiColumnCubePlatformSourceMode)(params.tableId, bizRequestConfig)) {
            return (0, ai_column_cube_platform_bridge_1.removeAiColumnViaCubePlatform)(params, cubePlatformCtx, bizRequestConfig);
        }
        const aiFieldFun = (_b = window.__CN_CUBE_INSTANCE__) === null || _b === void 0 ? void 0 : _b.__AI_Field_Fun__;
        const result = await (aiFieldFun === null || aiFieldFun === void 0 ? void 0 : aiFieldFun.removeAiColumn(params));
        return result;
    }
    catch (error) {
        console.warn(error);
        return { success: false, errorMsg: error === null || error === void 0 ? void 0 : error.message };
    }
}
// ==================== AI 总结相关接口 ====================
/**
 * 生成 AI 总结
 */
async function generateAiSummary(params, bizRequestConfig) {
    var _a;
    try {
        const summaryData = await (0, ai_request_1.aiRequest)({
            url: (_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.aiColumn) === null || _a === void 0 ? void 0 : _a.generateSummaryUrl,
            path: '/aiTableRowSummary/submitAndQueryTask.do',
            method: 'POST',
            data: params,
        });
        return { success: true, data: summaryData };
    }
    catch (error) {
        console.warn('生成 AI 总结失败:', error);
        return null;
    }
}
// ==================== AI 列预览相关接口 ====================
/**
 * 提交 AI 列预览任务
 */
async function submitAiColumnPreview(params, bizRequestConfig) {
    var _a;
    try {
        const taskInfo = await (0, ai_request_1.aiRequest)({
            url: (_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.aiColumn) === null || _a === void 0 ? void 0 : _a.submitPreviewUrl,
            path: '/aiTableColumns/submitPreview.do',
            method: 'POST',
            data: params,
        });
        return { success: true, data: taskInfo };
    }
    catch (error) {
        console.warn(error);
        return {
            success: false,
            errorMsg: (error === null || error === void 0 ? void 0 : error.message) || i18n_1.default.get({ id: 'PreviewRequestFailed', dm: '预览请求失败' }),
        };
    }
}
/**
 * 查询 AI 列预览结果
 */
async function queryAiColumnPreview(params, bizRequestConfig) {
    var _a;
    try {
        const previewData = await (0, ai_request_1.aiRequest)({
            url: (_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.aiColumn) === null || _a === void 0 ? void 0 : _a.queryPreviewUrl,
            path: '/aiTableColumns/queryPreviewResult.do',
            method: 'GET',
            params: {
                tableId: params.tableId,
                taskId: params.taskId,
            },
        });
        return { success: true, data: previewData };
    }
    catch (error) {
        console.warn(error);
        return {
            success: false,
            errorMsg: (error === null || error === void 0 ? void 0 : error.message) || i18n_1.default.get({ id: 'QueryPreviewResultFailed', dm: '查询预览结果失败' }),
        };
    }
}
// ==================== AI 单元格数据更新相关接口 ====================
/**
 * 更新 AI 单元格数据
 */
async function updateAiCellData(params, bizRequestConfig) {
    var _a;
    try {
        await (0, ai_request_1.aiRequest)({
            url: (_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.aiColumn) === null || _a === void 0 ? void 0 : _a.updateCellDataUrl,
            path: '/aiGridTasks/updateTask.do',
            method: 'POST',
            data: {
                tableId: params.tableId,
                columnCode: params.columnCode,
                content: params.content,
                rowId: params.rowId,
            },
        });
        return { success: true };
    }
    catch (error) {
        console.warn('更新 AI 单元格数据失败:', error);
        return {
            success: false,
            errorMsg: (error === null || error === void 0 ? void 0 : error.message) ||
                i18n_1.default.get({ id: 'FailedToUpdateAICellData', dm: '更新 AI 单元格数据失败' }),
        };
    }
}
