"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildGetListRequestBody = buildGetListRequestBody;
exports.requestCubeGetList = requestCubeGetList;
exports.fetchListAllPages = fetchListAllPages;
exports.fetchUserCubeTableRows = fetchUserCubeTableRows;
const get_cube_platform_user_id_1 = require("./get-cube-platform-user-id");
const cube_platform_view_constants_1 = require("../../../cn-ai-table/view/constants/cube-platform-view-constants");
const merge_cube_platform_request_1 = require("./merge-cube-platform-request");
const parse_response_1 = require("./parse-response");
/** 构建 getList 请求体（字段来自 `getCubePlatformMeta` 合并结果：`requestConfig.data` 与 window 等）。 */
function buildGetListRequestBody(meta, userId) {
    var _a;
    return {
        userId,
        pageId: meta.pageId,
        appCode: meta.appCode,
        tenantCode: (_a = meta.tenantCode) !== null && _a !== void 0 ? _a : '',
        loginType: meta.loginType,
    };
}
/** 请求开放平台 getList（底层为 POST，名称与接口路径一致）。 */
async function requestCubeGetList(data, bizRequestConfig) {
    var _a;
    const requestConfig = (_a = bizRequestConfig === null || bizRequestConfig === void 0 ? void 0 : bizRequestConfig.cubePlatform) === null || _a === void 0 ? void 0 : _a.getListRequestConfig;
    return (0, merge_cube_platform_request_1.requestCubeOpenPlatform)(cube_platform_view_constants_1.CUBE_PLATFORM_GET_LIST_URL, data, requestConfig, 'POST');
}
/** 带 `currentPage` / `pageSize` 分页，直到拿全或没有更多行。 */
async function fetchListAllPages(meta, bizRequestConfig) {
    const userId = (0, get_cube_platform_user_id_1.getCubePlatformUserId)(meta);
    const base = buildGetListRequestBody(meta, userId);
    const all = [];
    let currentPage = 1;
    let totalCount = 0;
    /* eslint-disable no-await-in-loop -- 分页必须顺序请求 */
    for (let i = 0; i < cube_platform_view_constants_1.CUBE_PLATFORM_GET_LIST_MAX_PAGES; i++) {
        const page = (0, parse_response_1.parseGetListPage)(await requestCubeGetList({
            ...base,
            currentPage,
            pageSize: cube_platform_view_constants_1.CUBE_PLATFORM_GET_LIST_PAGE_SIZE,
        }, bizRequestConfig));
        if (!page.success) {
            return i === 0 ? [] : all;
        }
        all.push(...page.rows);
        if (page.totalCount > 0) {
            totalCount = page.totalCount;
        }
        if (page.rows.length === 0) {
            break;
        }
        if (totalCount > 0 && all.length >= totalCount) {
            break;
        }
        if (totalCount === 0 && page.rows.length < cube_platform_view_constants_1.CUBE_PLATFORM_GET_LIST_PAGE_SIZE) {
            break;
        }
        if (totalCount > 0 && all.length < totalCount) {
            currentPage += 1;
            continue;
        }
        currentPage += 1;
    }
    /* eslint-enable no-await-in-loop */
    return all;
}
/** getList：先打首包（无分页参数），只有首包不够或为空时再走分页拉全量。 */
async function fetchUserCubeTableRows(meta, bizRequestConfig) {
    const userId = (0, get_cube_platform_user_id_1.getCubePlatformUserId)(meta);
    const listBody = buildGetListRequestBody(meta, userId);
    const listResponse = await requestCubeGetList(listBody, bizRequestConfig);
    const first = (0, parse_response_1.parseGetListPage)(listResponse);
    if (!first.success) {
        return [];
    }
    const needPaged = first.rows.length === 0 || (first.totalCount > 0 && first.totalCount > first.rows.length);
    if (needPaged) {
        return fetchListAllPages(meta, bizRequestConfig);
    }
    return first.rows;
}
