"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    BlockRequestContext: ()=>BlockRequestContext,
    useDataBlockRequest: ()=>useDataBlockRequest,
    BlockRequestProvider: ()=>BlockRequestProvider
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const external_ahooks_namespaceObject = require("ahooks");
const index_js_namespaceObject = require("../../api-client/index.js");
const setDataLoadingModeSettingsItem_js_namespaceObject = require("../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem.js");
const useSourceKey_js_namespaceObject = require("../../modules/blocks/useSourceKey.js");
const external_collection_record_index_js_namespaceObject = require("../collection-record/index.js");
const external_utils_js_namespaceObject = require("../utils.js");
const external_DataBlockProvider_js_namespaceObject = require("./DataBlockProvider.js");
const external_DataBlockResourceProvider_js_namespaceObject = require("./DataBlockResourceProvider.js");
const BlockRequestContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
BlockRequestContext.displayName = 'BlockRequestContext';
function useCurrentRequest(options) {
    const dataLoadingMode = (0, setDataLoadingModeSettingsItem_js_namespaceObject.useDataLoadingMode)();
    const resource = (0, external_DataBlockResourceProvider_js_namespaceObject.useDataBlockResource)();
    const { action, params = {}, record, requestService, requestOptions } = options;
    if (void 0 === params.filterByTk) delete params.filterByTk;
    const request = (0, index_js_namespaceObject.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, external_ahooks_namespaceObject.useDeepCompareEffect)(()=>{
        if (action && 'auto' === dataLoadingMode) request.run();
    }, [
        params,
        action,
        record
    ]);
    (0, external_ahooks_namespaceObject.useUpdateEffect)(()=>{
        if (action && 'auto' === dataLoadingMode) request.run();
    }, [
        resource
    ]);
    return request;
}
function useParentRequest(options) {
    const { sourceId, association, parentRecord } = options;
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const dataBlockProps = (0, external_DataBlockProvider_js_namespaceObject.useDataBlockProps)();
    const headers = (0, external_utils_js_namespaceObject.useDataSourceHeaders)(dataBlockProps.dataSource);
    const sourceKey = (0, useSourceKey_js_namespaceObject.useSourceKey)(association);
    return (0, index_js_namespaceObject.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, external_DataBlockProvider_js_namespaceObject.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, external_react_namespaceObject.useMemo)(()=>{
        var _parentRequest_data, _parentRequest_data1;
        return (null === (_parentRequest_data = parentRequest.data) || void 0 === _parentRequest_data ? void 0 : _parentRequest_data.data) && new external_collection_record_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(BlockRequestContext.Provider, {
        value: currentRequest,
        children: 'list' !== action ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_collection_record_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_collection_record_index_js_namespaceObject.CollectionRecordProvider, {
            isNew: false,
            record: null,
            parentRecord: memoizedParentRecord,
            children: children
        })
    });
};
const useDataBlockRequest = ()=>{
    const context = (0, external_react_namespaceObject.useContext)(BlockRequestContext);
    return context;
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
