"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    FilterBlockProvider: ()=>FilterBlockProvider,
    useFilterBlock: ()=>useFilterBlock,
    DataBlockCollector: ()=>DataBlockCollector
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const schema_namespaceObject = require("@tachybase/schema");
const external_lodash_namespaceObject = require("lodash");
const BlockProvider_js_namespaceObject = require("../block-provider/BlockProvider.js");
const index_js_namespaceObject = require("../collection-manager/index.js");
const setDataLoadingModeSettingsItem_js_namespaceObject = require("../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem.js");
const external_schema_component_index_js_namespaceObject = require("../schema-component/index.js");
const external_utils_js_namespaceObject = require("./utils.js");
const FilterContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
FilterContext.displayName = 'FilterContext';
const FilterBlockProvider = (param)=>{
    let { children } = param;
    const [dataBlocks, setDataBlocks] = external_react_default().useState([]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(FilterContext.Provider, {
        value: {
            dataBlocks,
            setDataBlocks
        },
        children: children
    });
};
const DataBlockCollector = (param)=>{
    let { children, params } = param;
    const collection = (0, index_js_namespaceObject.useCollection_deprecated)();
    const { recordDataBlocks, removeDataBlock } = useFilterBlock();
    const { service } = (0, BlockProvider_js_namespaceObject.useBlockRequestContext)();
    const field = (0, schema_namespaceObject.useField)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const associatedFields = (0, external_utils_js_namespaceObject.useAssociatedFields)();
    const container = (0, external_react_namespaceObject.useRef)(null);
    const dataLoadingMode = (0, setDataLoadingModeSettingsItem_js_namespaceObject.useDataLoadingMode)();
    const shouldApplyFilter = 'FilterFormBlockProvider' !== field.decoratorType && 'FormBlockProvider' !== field.decoratorType && 'filter' !== field.decoratorProps.blockType;
    const addBlockToDataBlocks = (0, external_react_namespaceObject.useCallback)(()=>{
        recordDataBlocks({
            uid: fieldSchema['x-uid'],
            title: field.componentProps.title,
            doFilter: service.runAsync,
            collection,
            associatedFields,
            foreignKeyFields: collection.foreignKeyFields,
            defaultFilter: (null == params ? void 0 : params.filter) || {},
            service,
            dom: container.current,
            dataLoadingMode,
            clearFilter (uid) {
                var _this_service_params, _this_service_params_, _this_service_params1;
                const param = (null === (_this_service_params = this.service.params) || void 0 === _this_service_params ? void 0 : _this_service_params[0]) || {};
                const storedFilter = (null === (_this_service_params1 = this.service.params) || void 0 === _this_service_params1 ? void 0 : null === (_this_service_params_ = _this_service_params1[1]) || void 0 === _this_service_params_ ? void 0 : _this_service_params_.filters) || {};
                delete storedFilter[uid];
                const mergedFilter = (0, external_utils_js_namespaceObject.mergeFilter)([
                    ...Object.values(storedFilter).map((filter)=>(0, external_schema_component_index_js_namespaceObject.removeNullCondition)(filter)),
                    (null == params ? void 0 : params.filter) || {}
                ]);
                this.service.run({
                    ...param,
                    page: 1,
                    filter: mergedFilter
                }, {
                    filters: storedFilter
                });
            },
            clearData () {
                this.service.mutate(void 0);
            }
        });
    }, [
        associatedFields,
        collection,
        dataLoadingMode,
        field,
        fieldSchema,
        null == params ? void 0 : params.filter,
        recordDataBlocks,
        service
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (shouldApplyFilter) addBlockToDataBlocks();
    }, [
        params.filter,
        service,
        dataLoadingMode,
        shouldApplyFilter,
        addBlockToDataBlocks
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>()=>{
            removeDataBlock(fieldSchema['x-uid']);
        }, []);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        ref: container,
        children: children
    });
};
const useFilterBlock = ()=>{
    const ctx = external_react_default().useContext(FilterContext);
    if (!ctx) return {
        inProvider: false,
        recordDataBlocks: ()=>{},
        getDataBlocks: ()=>[],
        removeDataBlock: ()=>{}
    };
    const { dataBlocks, setDataBlocks } = ctx;
    const recordDataBlocks = (block)=>{
        const existingBlock = dataBlocks.find((item)=>item.uid === block.uid);
        if (existingBlock) {
            existingBlock.service = block.service;
            existingBlock.defaultFilter = block.defaultFilter;
            existingBlock.dataLoadingMode = block.dataLoadingMode;
            return;
        }
        setDataBlocks((prev)=>(0, external_lodash_namespaceObject.uniqBy)([
                ...prev,
                block
            ], 'uid'));
    };
    const getDataBlocks = ()=>dataBlocks;
    const removeDataBlock = (uid)=>{
        setDataBlocks((prev)=>prev.filter((item)=>item.uid !== uid));
    };
    return {
        recordDataBlocks,
        getDataBlocks,
        removeDataBlock,
        inProvider: true
    };
};
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
});
