import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__ from "../block-provider/hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_SchemaSettingOptions_mjs__ from "../schema-component/antd/form-item/SchemaSettingOptions.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__FilterProvider_mjs__ from "./FilterProvider.mjs";
var utils_rslib_entry_FilterBlockType = /*#__PURE__*/ function(FilterBlockType) {
    FilterBlockType[FilterBlockType["FORM"] = 0] = "FORM";
    FilterBlockType[FilterBlockType["TABLE"] = 1] = "TABLE";
    FilterBlockType[FilterBlockType["TREE"] = 2] = "TREE";
    FilterBlockType[FilterBlockType["COLLAPSE"] = 3] = "COLLAPSE";
    return FilterBlockType;
}({});
const mergeFilter = function(filters) {
    let op = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : '$and';
    const items = filters.filter((f)=>{
        if (f && 'object' == typeof f && !Array.isArray(f)) return Object.values(f).filter((v)=>void 0 !== v).length;
    });
    if (0 === items.length) return {};
    if (1 === items.length) return items[0];
    return {
        [op]: items
    };
};
const getSupportFieldsByAssociation = (inheritCollectionsChain, block)=>{
    var _block_associatedFields;
    return null === (_block_associatedFields = block.associatedFields) || void 0 === _block_associatedFields ? void 0 : _block_associatedFields.filter((field)=>null == inheritCollectionsChain ? void 0 : inheritCollectionsChain.some((collectionName)=>collectionName === field.target));
};
const getSupportFieldsByForeignKey = (filterBlockCollection, block)=>{
    var _block_foreignKeyFields;
    return null === (_block_foreignKeyFields = block.foreignKeyFields) || void 0 === _block_foreignKeyFields ? void 0 : _block_foreignKeyFields.filter((foreignKeyField)=>filterBlockCollection.fields.some((field)=>'belongsTo' !== field.type && field.foreignKey === foreignKeyField.name));
};
const useSupportedBlocks = (filterBlockType)=>{
    const { getDataBlocks } = (0, __WEBPACK_EXTERNAL_MODULE__FilterProvider_mjs__.useFilterBlock)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const collection = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { getAllCollectionsInheritChain } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    if (0 === filterBlockType || 3 === filterBlockType) return getDataBlocks().filter((block)=>isSameCollection(block.collection, collection));
    if (1 === filterBlockType || 2 === filterBlockType) return getDataBlocks().filter((block)=>{
        var _getSupportFieldsByAssociation, _getSupportFieldsByForeignKey;
        return fieldSchema['x-uid'] !== block.uid && (isSameCollection(block.collection, collection) || (null === (_getSupportFieldsByAssociation = getSupportFieldsByAssociation(getAllCollectionsInheritChain(collection.name, collection.dataSource), block)) || void 0 === _getSupportFieldsByAssociation ? void 0 : _getSupportFieldsByAssociation.length) || (null === (_getSupportFieldsByForeignKey = getSupportFieldsByForeignKey(collection, block)) || void 0 === _getSupportFieldsByForeignKey ? void 0 : _getSupportFieldsByForeignKey.length));
    });
};
const transformToFilter = (values, fieldSchema, getCollectionJoinField, collectionName)=>{
    const { operators } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_form_item_SchemaSettingOptions_mjs__.findFilterOperators)(fieldSchema);
    values = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.flatten)(values, {
        breakOn (param) {
            let { value, path } = param;
            if ([
                '$match',
                '$notMatch',
                '$anyOf',
                '$noneOf',
                '$childIn',
                '$childNotIn',
                '$dateBetween',
                '$in',
                '$notIn'
            ].includes(operators[path])) return true;
            const collectionField = getCollectionJoinField("".concat(collectionName, ".").concat(path));
            if (null == collectionField ? void 0 : collectionField.target) {
                if (Array.isArray(value)) return true;
                const targetKey = collectionField.targetKey || 'id';
                if (value && null != value[targetKey]) return true;
            }
            return false;
        }
    });
    const result = {
        $and: Object.keys(values).map((key)=>{
            const defKey = key;
            let value = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].get(values, key);
            const collectionField = getCollectionJoinField("".concat(collectionName, ".").concat(key));
            if (null == collectionField ? void 0 : collectionField.target) {
                value = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.getValuesByPath)(value, collectionField.targetKey || 'id');
                key = "".concat(key, ".").concat(collectionField.targetKey || 'id');
            }
            if (!value) return null;
            if ('$isTruly' === operators[key] || '$isFalsy' === operators[key]) {
                if ('true' === value) return {
                    [key]: {
                        $isTruly: true
                    }
                };
                if ('false' === value) return {
                    [key]: {
                        $isFalsy: true
                    }
                };
            }
            return {
                [key]: {
                    [operators[key] || operators[defKey] || '$eq']: value
                }
            };
        }).filter(Boolean)
    };
    return result;
};
const useAssociatedFields = ()=>{
    const { fields } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    return fields.filter((field)=>isAssocField(field)) || [];
};
const isAssocField = (field)=>[
        'o2o',
        'oho',
        'obo',
        'm2o',
        'createdBy',
        'updatedBy',
        'o2m',
        'm2m',
        'linkTo',
        'chinaRegion'
    ].includes(null == field ? void 0 : field.interface);
const isSameCollection = (c1, c2)=>c1.name === c2.name && c1.dataSource === c2.dataSource;
const useFilterAPI = ()=>{
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { getDataBlocks } = (0, __WEBPACK_EXTERNAL_MODULE__FilterProvider_mjs__.useFilterBlock)();
    const { targets, uid } = (0, __WEBPACK_EXTERNAL_MODULE__block_provider_hooks_index_mjs__.findFilterTargets)(fieldSchema);
    const dataBlocks = getDataBlocks();
    const [isConnected, setIsConnected] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(()=>targets && targets.some((target)=>dataBlocks.some((dataBlock)=>dataBlock.uid === target.uid)));
    const targetsKeys = Object.keys(targets || {});
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        setIsConnected(targets && targets.some((target)=>dataBlocks.some((dataBlock)=>dataBlock.uid === target.uid)));
    }, [
        targetsKeys.length,
        dataBlocks
    ]);
    const doFilter = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(function(value) {
        let field = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 'id', operator = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : '$eq';
        dataBlocks.forEach((block)=>{
            var _block_service_params, _block_service_params_, _block_service_params1;
            const target = targets.find((target)=>target.uid === block.uid);
            if (!target) return;
            if (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isFunction(field)) field = field(target);
            if (__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isFunction(operator)) operator = operator(target);
            const param = (null === (_block_service_params = block.service.params) || void 0 === _block_service_params ? void 0 : _block_service_params[0]) || {};
            const storedFilter = (null === (_block_service_params1 = block.service.params) || void 0 === _block_service_params1 ? void 0 : null === (_block_service_params_ = _block_service_params1[1]) || void 0 === _block_service_params_ ? void 0 : _block_service_params_.filters) || {};
            if (void 0 !== value) storedFilter[uid] = {
                $and: [
                    {
                        [field]: {
                            [operator]: value
                        }
                    }
                ]
            };
            else delete storedFilter[uid];
            const mergedFilter = mergeFilter([
                ...Object.values(storedFilter).map((filter)=>(0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.removeNullCondition)(filter)),
                block.defaultFilter
            ]);
            block.doFilter({
                ...param,
                page: 1,
                filter: mergedFilter
            }, {
                filters: storedFilter
            });
        });
    }, [
        dataBlocks
    ]);
    return {
        isConnected,
        doFilter
    };
};
const isInFilterFormBlock = (fieldSchema)=>{
    while(fieldSchema){
        if (fieldSchema['x-filter-targets']) return 'FilterFormBlockProvider' === fieldSchema['x-decorator'];
        fieldSchema = fieldSchema.parent;
    }
    return false;
};
export { utils_rslib_entry_FilterBlockType as FilterBlockType, getSupportFieldsByAssociation, getSupportFieldsByForeignKey, isAssocField, isInFilterFormBlock, isSameCollection, mergeFilter, transformToFilter, useAssociatedFields, useFilterAPI, useSupportedBlocks };
