import { getTransform, shouldGetDefaultParams } from '../../utils';
import { globalConfig } from '../../constants';
import fetch from '../fetch';
/**
 * 拼请求参数并发请求（单次请求与单批请求共用）
 * 不处理响应转换、loading、锁、callback，由调用方负责
 */
export function buildQueryAndFetch(options) {
    var _a, _b, _c;
    const { remote, innerParams, currentPage, pageSize, clearDataType, paramsData } = options;
    const paramsTransform = getTransform((_a = remote === null || remote === void 0 ? void 0 : remote.paramsTransform) !== null && _a !== void 0 ? _a : globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.defaultParamsTransform);
    const { pageSize: _ignoredPageSize, ...restParamsData } = paramsData;
    const finalQuery = paramsTransform({
        ...shouldGetDefaultParams({
            currentPage,
            ...((innerParams === null || innerParams === void 0 ? void 0 : innerParams.current) || {}),
        }, clearDataType),
        ...((remote === null || remote === void 0 ? void 0 : remote.params) || {}),
        ...restParamsData,
        currentPage,
        pageSize,
    });
    const method = ((_b = remote === null || remote === void 0 ? void 0 : remote.method) !== null && _b !== void 0 ? _b : 'post').toLowerCase();
    const doFetch = (remote === null || remote === void 0 ? void 0 : remote.fetch) instanceof Function
        ? remote === null || remote === void 0 ? void 0 : remote.fetch(finalQuery)
        : fetch({
            url: remote === null || remote === void 0 ? void 0 : remote.url,
            method: (_c = remote === null || remote === void 0 ? void 0 : remote.method) !== null && _c !== void 0 ? _c : 'post',
            ...((remote === null || remote === void 0 ? void 0 : remote.requestOptions) || {}),
            [method === 'post' ? 'data' : 'params']: finalQuery,
        });
    return doFetch;
}
