import $i18n from '../../../locales/i18n.js';
import { aiRequest } from '../../../service/ai-request.js';
import { createOrModifyAiColumnViaCubePlatform, isAiColumnCubePlatformSourceMode, queryAiTableColumnsViaCubePlatform, queryTableTasksViaCubePlatform, removeAiColumnViaCubePlatform, } from './ai-column-cube-platform-bridge';
export { isAiColumnCubePlatformSourceMode, isCubeDirect } from './ai-column-cube-platform-bridge';
// ==================== API 服务方法 ====================
/**
 * 新增或修改 AI 列
 */
export async function createOrModifyAiColumn(params, bizRequestConfig, cubePlatformCtx) {
    var _a, _b;
    try {
        if (isAiColumnCubePlatformSourceMode(params.tableId, bizRequestConfig)) {
            return 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 列
 */
export async function queryAiTableColumns(params, bizRequestConfig, cubePlatformCtx) {
    try {
        if (isAiColumnCubePlatformSourceMode(params.tableId, bizRequestConfig)) {
            return 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);
    }
}
/**
 * 查询全表任务
 */
export 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 aiRequest({
                url: bizRequestConfig.aiColumn.queryTableTasksUrl,
                method: 'GET',
                params: { tableId: params.tableId },
            });
            return { success: true, data: taskData };
        }
        if (isAiColumnCubePlatformSourceMode(params.tableId, bizRequestConfig)) {
            return 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);
    }
}
/**
 * 提交任务
 */
export async function submitTasks(params, bizRequestConfig) {
    var _a;
    try {
        const taskData = await 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,
        };
    }
}
/**
 * 轮询任务
 */
export async function pollTasks(params, bizRequestConfig) {
    var _a;
    try {
        const taskData = await 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 列
 */
export 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 aiRequest({
                url: bizRequestConfig.aiColumn.removeColumnUrl,
                method: 'POST',
                data: params,
            });
            return { success: true };
        }
        if (isAiColumnCubePlatformSourceMode(params.tableId, bizRequestConfig)) {
            return 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 总结
 */
export async function generateAiSummary(params, bizRequestConfig) {
    var _a;
    try {
        const summaryData = await 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 列预览任务
 */
export async function submitAiColumnPreview(params, bizRequestConfig) {
    var _a;
    try {
        const taskInfo = await 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.get({ id: 'PreviewRequestFailed', dm: '预览请求失败' }),
        };
    }
}
/**
 * 查询 AI 列预览结果
 */
export async function queryAiColumnPreview(params, bizRequestConfig) {
    var _a;
    try {
        const previewData = await 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.get({ id: 'QueryPreviewResultFailed', dm: '查询预览结果失败' }),
        };
    }
}
// ==================== AI 单元格数据更新相关接口 ====================
/**
 * 更新 AI 单元格数据
 */
export async function updateAiCellData(params, bizRequestConfig) {
    var _a;
    try {
        await 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.get({ id: 'FailedToUpdateAICellData', dm: '更新 AI 单元格数据失败' }),
        };
    }
}
