// 第三方依赖
import isFunction from 'lodash/isFunction';
import { MAX_API_PAGE_SIZE } from './batch/batch-request';
import { getResponseTransform } from './utils/response-transform';
import { buildQueryAndFetch } from './utils/build-query-and-fetch';
import { isBirda } from '../../../utils/env/index.js';
/**
 * 执行单次请求（非批次请求，包含完整的请求生命周期）
 */
export 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 (isBirda() && userPageSize > MAX_API_PAGE_SIZE) {
        pageSize = MAX_API_PAGE_SIZE;
    }
    const responseTransform = getResponseTransform(remote);
    const doFetch = 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 && isFunction(callback) && callback(finalRes, clearDataType);
        }, errorHandler);
    })
        .catch(errorHandler)
        .finally(() => {
        isFunction(remote === null || remote === void 0 ? void 0 : remote.onFinally) && (remote === null || remote === void 0 ? void 0 : remote.onFinally());
    });
}
