import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE_ahooks__ from "ahooks";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_details_multi_setDataLoadingModeSettingsItem_mjs__ from "../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__modules_blocks_useSourceKey_mjs__ from "../../modules/blocks/useSourceKey.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_record_index_mjs__ from "../collection-record/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "../utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__DataBlockProvider_mjs__ from "./DataBlockProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__DataBlockResourceProvider_mjs__ from "./DataBlockResourceProvider.mjs";
const BlockRequestContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
BlockRequestContext.displayName = 'BlockRequestContext';
function useCurrentRequest(options) {
    const dataLoadingMode = (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_details_multi_setDataLoadingModeSettingsItem_mjs__.useDataLoadingMode)();
    const resource = (0, __WEBPACK_EXTERNAL_MODULE__DataBlockResourceProvider_mjs__.useDataBlockResource)();
    const { action, params = {}, record, requestService, requestOptions } = options;
    if (void 0 === params.filterByTk) delete params.filterByTk;
    const request = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(requestService ? requestService : (customParams)=>{
        if (record) return Promise.resolve({
            data: record
        });
        if (!action) throw new Error("[tachybase]: The 'action' parameter is missing in the 'DataBlockRequestProvider' component");
        return resource[action]({
            ...params,
            ...customParams
        }).then((res)=>res.data);
    }, {
        ...requestOptions,
        manual: true
    });
    (0, __WEBPACK_EXTERNAL_MODULE_ahooks__.useDeepCompareEffect)(()=>{
        if (action && 'auto' === dataLoadingMode) request.run();
    }, [
        params,
        action,
        record
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_ahooks__.useUpdateEffect)(()=>{
        if (action && 'auto' === dataLoadingMode) request.run();
    }, [
        resource
    ]);
    return request;
}
function useParentRequest(options) {
    const { sourceId, association, parentRecord } = options;
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const dataBlockProps = (0, __WEBPACK_EXTERNAL_MODULE__DataBlockProvider_mjs__.useDataBlockProps)();
    const headers = (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useDataSourceHeaders)(dataBlockProps.dataSource);
    const sourceKey = (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_useSourceKey_mjs__.useSourceKey)(association);
    return (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(async ()=>{
        if (parentRecord) return Promise.resolve({
            data: parentRecord
        });
        if (!association || !sourceKey) return Promise.resolve({
            data: void 0
        });
        const arr = association.split('.');
        const url = "".concat(arr[0], ":get?filter[").concat(sourceKey, "]=").concat(sourceId);
        const res = await api.request({
            url,
            headers
        });
        return res.data;
    }, {
        refreshDeps: [
            association,
            parentRecord,
            sourceId
        ]
    });
}
const BlockRequestProvider = (param)=>{
    let { children } = param;
    var _parentRequest_data, _currentRequest_data;
    const props = (0, __WEBPACK_EXTERNAL_MODULE__DataBlockProvider_mjs__.useDataBlockProps)();
    const { action, filterByTk, sourceId, params = {}, association, collection, record, parentRecord, requestOptions, requestService } = props;
    const currentRequest = useCurrentRequest({
        action,
        sourceId,
        record,
        association,
        collection,
        requestOptions,
        requestService,
        params: {
            ...params,
            filterByTk: filterByTk || params.filterByTk
        }
    });
    const parentRequest = useParentRequest({
        sourceId,
        association,
        parentRecord
    });
    const memoizedParentRecord = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _parentRequest_data, _parentRequest_data1;
        return (null === (_parentRequest_data = parentRequest.data) || void 0 === _parentRequest_data ? void 0 : _parentRequest_data.data) && new __WEBPACK_EXTERNAL_MODULE__collection_record_index_mjs__.CollectionRecord({
            isNew: false,
            data: null === (_parentRequest_data1 = parentRequest.data) || void 0 === _parentRequest_data1 ? void 0 : _parentRequest_data1.data
        });
    }, [
        null === (_parentRequest_data = parentRequest.data) || void 0 === _parentRequest_data ? void 0 : _parentRequest_data.data
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(BlockRequestContext.Provider, {
        value: currentRequest,
        children: 'list' !== action ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__collection_record_index_mjs__.CollectionRecordProvider, {
            isNew: null == action,
            record: null === (_currentRequest_data = currentRequest.data) || void 0 === _currentRequest_data ? void 0 : _currentRequest_data.data,
            parentRecord: memoizedParentRecord,
            children: children
        }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__collection_record_index_mjs__.CollectionRecordProvider, {
            isNew: false,
            record: null,
            parentRecord: memoizedParentRecord,
            children: children
        })
    });
};
const useDataBlockRequest = ()=>{
    const context = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(BlockRequestContext);
    return context;
};
export { BlockRequestContext, BlockRequestProvider, useDataBlockRequest };
