"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.persistUserCubeTableView = persistUserCubeTableView;
exports.persistAiColumnsToCube = persistAiColumnsToCube;
const process_user_cube_config_1 = require("./process-user-cube-config");
const submit_user_cube_create_or_update_1 = require("./submit-user-cube-create-or-update");
/**
 * 持久化当前 `tableId` 的视图配置到开放平台。
 *
 * @param userCubeConfigRowIdHint 可选；上次保存成功的行主键，更新时优先于「仅内存里的 content.id」。
 * @returns `userCubeConfigRowId` 供调用方缓存，下次更新带 `id`（createOrUpdate 顶层与 content 根级一致）。
 */
async function persistUserCubeTableView(options) {
    const { meta, tableId, viewsData, nodeId, userCubeConfigRowIdHint, bizRequestConfig } = options;
    try {
        const { userCubeConfig, userCubeConfigRowId: rowIdFromList } = await (0, process_user_cube_config_1.loadUserCubeConfigFromPlatform)(meta, bizRequestConfig);
        const nextConfig = {
            ...userCubeConfig,
            components: (0, process_user_cube_config_1.mergeTableViewIntoComponents)(userCubeConfig.components, {
                tableId,
                viewsData,
                nodeId,
            }),
        };
        return (0, submit_user_cube_create_or_update_1.submitUserCubeCreateOrUpdate)({
            meta,
            rowIdFromList,
            userCubeConfigRowIdHint,
            nextConfig,
            bizRequestConfig,
        });
    }
    catch (error) {
        return { success: false, errorMsg: error };
    }
}
/**
 * 合并 `components[]` 后写入开放平台（与 {@link persistUserCubeTableView} 同页 JSON，避免覆盖其它组件）。
 */
async function persistAiColumnsToCube(options) {
    const { meta, userCubeConfigRowIdHint, mutateComponents, bizRequestConfig } = options;
    try {
        const { userCubeConfig, userCubeConfigRowId: rowIdFromList } = await (0, process_user_cube_config_1.loadUserCubeConfigFromPlatform)(meta, bizRequestConfig);
        const nextConfig = {
            ...userCubeConfig,
            components: mutateComponents((userCubeConfig.components || [])),
        };
        return (0, submit_user_cube_create_or_update_1.submitUserCubeCreateOrUpdate)({
            meta,
            rowIdFromList,
            userCubeConfigRowIdHint,
            nextConfig,
            bizRequestConfig,
        });
    }
    catch (error) {
        return { success: false, errorMsg: error };
    }
}
