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__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_BlockProvider_mjs__ from "../block-provider/BlockProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../collection-manager/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__schema_component_index_mjs__ from "../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "./utils.mjs";
const FilterContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
FilterContext.displayName = 'FilterContext';
const FilterBlockProvider = (param)=>{
    let { children } = param;
    const [dataBlocks, setDataBlocks] = __WEBPACK_EXTERNAL_MODULE_react__["default"].useState([]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(FilterContext.Provider, {
        value: {
            dataBlocks,
            setDataBlocks
        },
        children: children
    });
};
const DataBlockCollector = (param)=>{
    let { children, params } = param;
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { recordDataBlocks, removeDataBlock } = useFilterBlock();
    const { service } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_BlockProvider_mjs__.useBlockRequestContext)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const associatedFields = (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useAssociatedFields)();
    const container = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const dataLoadingMode = (0, __WEBPACK_EXTERNAL_MODULE__modules_blocks_data_blocks_details_multi_setDataLoadingModeSettingsItem_mjs__.useDataLoadingMode)();
    const shouldApplyFilter = 'FilterFormBlockProvider' !== field.decoratorType && 'FormBlockProvider' !== field.decoratorType && 'filter' !== field.decoratorProps.blockType;
    const addBlockToDataBlocks = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.mergeFilter)([
                    ...Object.values(storedFilter).map((filter)=>(0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (shouldApplyFilter) addBlockToDataBlocks();
    }, [
        params.filter,
        service,
        dataLoadingMode,
        shouldApplyFilter,
        addBlockToDataBlocks
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>()=>{
            removeDataBlock(fieldSchema['x-uid']);
        }, []);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        ref: container,
        children: children
    });
};
const useFilterBlock = ()=>{
    const ctx = __WEBPACK_EXTERNAL_MODULE_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, __WEBPACK_EXTERNAL_MODULE_lodash__.uniqBy)([
                ...prev,
                block
            ], 'uid'));
    };
    const getDataBlocks = ()=>dataBlocks;
    const removeDataBlock = (uid)=>{
        setDataBlocks((prev)=>prev.filter((item)=>item.uid !== uid));
    };
    return {
        recordDataBlocks,
        getDataBlocks,
        removeDataBlock,
        inProvider: true
    };
};
export { DataBlockCollector, FilterBlockProvider, useFilterBlock };
