"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeSingleRequest = executeSingleRequest;
// 第三方依赖
const isFunction_1 = __importDefault(require("lodash/isFunction"));
const batch_request_1 = require("./batch/batch-request");
const response_transform_1 = require("./utils/response-transform");
const build_query_and_fetch_1 = require("./utils/build-query-and-fetch");
const env_1 = require("../../../utils/env/index.js");
/**
 * 执行单次请求（非批次请求，包含完整的请求生命周期）
 */
function executeSingleRequest(options) {
    var _a;
    const { remote, innerParams, userPageSize, userCurrentPage, clearDataType, paramsData, setLoading, releaseLock, callback, errorHandler, } = options;
    const currentPage = (_a = paramsData.currentPage) !== null && _a !== void 0 ? _a : userCurrentPage;
    // 专业版场景（!isBirda()）不限制 pageSize，使用用户设置的 pageSize
    // 鸟搭场景限制最大 pageSize 为 MAX_API_PAGE_SIZE
    let pageSize = userPageSize;
    if ((0, env_1.isBirda)() && userPageSize > batch_request_1.MAX_API_PAGE_SIZE) {
        pageSize = batch_request_1.MAX_API_PAGE_SIZE;
    }
    const responseTransform = (0, response_transform_1.getResponseTransform)(remote);
    const doFetch = (0, build_query_and_fetch_1.buildQueryAndFetch)({
        remote,
        innerParams,
        currentPage,
        pageSize,
        clearDataType,
        paramsData,
    });
    if (!(doFetch === null || doFetch === void 0 ? void 0 : doFetch.then) || typeof doFetch.then !== 'function') {
        return Promise.reject(new Error('Invalid request: fetch did not return a thenable'));
    }
    return doFetch
        .then((res) => {
        var _a;
        const rawData = (_a = res === null || res === void 0 ? void 0 : res.data) !== null && _a !== void 0 ? _a : res;
        return Promise.resolve(responseTransform(rawData, res)).then((finalRes) => {
            setLoading(false);
            releaseLock();
            callback && (0, isFunction_1.default)(callback) && callback(finalRes, clearDataType);
        }, errorHandler);
    })
        .catch(errorHandler)
        .finally(() => {
        (0, isFunction_1.default)(remote === null || remote === void 0 ? void 0 : remote.onFinally) && (remote === null || remote === void 0 ? void 0 : remote.onFinally());
    });
}
