"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__, {
    useFilterActionProps: ()=>useFilterActionProps,
    getCustomCondition: ()=>getCustomCondition,
    useFilterOptions: ()=>useFilterOptions,
    useGetFilterFieldOptions: ()=>useGetFilterFieldOptions,
    useFilterFieldOptions: ()=>useFilterFieldOptions,
    removeNullCondition: ()=>removeNullCondition,
    useGetFilterOptions: ()=>useGetFilterOptions,
    useFilterFieldProps: ()=>useFilterFieldProps
});
const schema_namespaceObject = require("@tachybase/schema");
const external_flat_namespaceObject = require("flat");
var external_flat_default = /*#__PURE__*/ __webpack_require__.n(external_flat_namespaceObject);
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const external_react_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../../../block-provider/index.js");
const external_collection_manager_index_js_namespaceObject = require("../../../collection-manager/index.js");
const utils_js_namespaceObject = require("../../../filter-provider/utils.js");
const setDataLoadingModeSettingsItem_js_namespaceObject = require("../../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem.js");
const external_utils_js_namespaceObject = require("./utils.js");
const useGetFilterOptions = ()=>{
    const { getCollectionFields } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const getFilterFieldOptions = useGetFilterFieldOptions();
    return (collectionName, dataSource)=>{
        const fields = getCollectionFields(collectionName, dataSource);
        const options = getFilterFieldOptions(fields);
        return options;
    };
};
const useFilterOptions = (collectionName)=>{
    const { getCollectionFields } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const fields = getCollectionFields(collectionName);
    const options = useFilterFieldOptions(fields);
    return options;
};
const useGetFilterFieldOptions = ()=>{
    var _fieldSchema_xcomponentprops;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const nonfilterable = (null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.nonfilterable) || [];
    const { getCollectionFields, getInterface } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const field2option = (field, depth)=>{
        var _field_uiSchema, _operators_filter;
        if (nonfilterable.length && 1 === depth && nonfilterable.includes(field.name)) return;
        if (!field.interface) return;
        const fieldInterface = getInterface(field.interface);
        if (!(null == fieldInterface ? void 0 : fieldInterface.filterable)) return;
        const { nested, children, operators } = fieldInterface.filterable;
        const option = {
            name: field.name,
            type: field.type,
            target: field.target,
            title: (null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || field.name,
            schema: null == field ? void 0 : field.uiSchema,
            interface: field.interface,
            operators: (null == operators ? void 0 : null === (_operators_filter = operators.filter) || void 0 === _operators_filter ? void 0 : _operators_filter.call(operators, (operator)=>!(null == operator ? void 0 : operator.visible) || operator.visible(field))) || []
        };
        if (field.target && depth > 2) return;
        if (depth > 2) return option;
        if (null == children ? void 0 : children.length) option['children'] = children;
        if (nested) {
            const targetFields = getCollectionFields(field.target);
            const options = getOptions(targetFields, depth + 1).filter(Boolean);
            option['children'] = option['children'] || [];
            option['children'].push(...options);
        }
        return option;
    };
    const getOptions = (fields, depth)=>{
        const options = [];
        fields.forEach((field)=>{
            const option = field2option(field, depth);
            if (option) options.push(option);
        });
        return options;
    };
    return (fields)=>getOptions(fields, 1);
};
const useFilterFieldOptions = (fields)=>{
    var _fieldSchema_xcomponentprops;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const nonfilterable = (null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.nonfilterable) || [];
    const { getCollectionFields, getInterface } = (0, external_collection_manager_index_js_namespaceObject.useCollectionManager_deprecated)();
    const field2option = (field, depth)=>{
        var _field_uiSchema, _operators_filter;
        if (nonfilterable.length && 1 === depth && nonfilterable.includes(field.name)) return;
        if (!field.interface) return;
        const fieldInterface = getInterface(field.interface);
        if (!(null == fieldInterface ? void 0 : fieldInterface.filterable)) return;
        const { nested, children, operators } = fieldInterface.filterable;
        const option = {
            name: field.name,
            type: field.type,
            target: field.target,
            title: (null == field ? void 0 : null === (_field_uiSchema = field.uiSchema) || void 0 === _field_uiSchema ? void 0 : _field_uiSchema.title) || field.name,
            schema: null == field ? void 0 : field.uiSchema,
            operators: (null == operators ? void 0 : null === (_operators_filter = operators.filter) || void 0 === _operators_filter ? void 0 : _operators_filter.call(operators, (operator)=>!(null == operator ? void 0 : operator.visible) || operator.visible(field))) || []
        };
        if (field.target && depth > 2) return;
        if (depth > 2) return option;
        if (null == children ? void 0 : children.length) option['children'] = children;
        if (nested) {
            const targetFields = getCollectionFields(field.target);
            const options = getOptions(targetFields, depth + 1).filter(Boolean);
            option['children'] = option['children'] || [];
            option['children'].push(...options);
        }
        return option;
    };
    const getOptions = (fields, depth)=>{
        const options = [];
        fields.forEach((field)=>{
            const option = field2option(field, depth);
            if (option) options.push(option);
        });
        return options;
    };
    return getOptions(fields, 1);
};
const isEmpty = (obj)=>Array.isArray(obj) && 0 === obj.length || obj && 0 === Object.keys(obj).length && Object.getPrototypeOf(obj) === Object.prototype;
const getCustomCondition = function(filter, fieldSchema) {
    let customFlat = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : external_flat_default();
    var _filterSchema_$and, _filterSchema_$or;
    const filterSchema = fieldSchema ? fieldSchema['x-filter-rules'] : '';
    const filterSchemaItem = customFlat(filterSchema || '');
    const items = customFlat(filter || {});
    const isCustomFilter = (null == filterSchema ? void 0 : null === (_filterSchema_$and = filterSchema.$and) || void 0 === _filterSchema_$and ? void 0 : _filterSchema_$and.length) || (null == filterSchema ? void 0 : null === (_filterSchema_$or = filterSchema.$or) || void 0 === _filterSchema_$or ? void 0 : _filterSchema_$or.length);
    const isFilterCustom = !!isCustomFilter && (0, external_utils_js_namespaceObject.hasDuplicateKeys)(items, filterSchemaItem);
    if (isFilterCustom) return customFlat.unflatten(items);
    if (!isCustomFilter) return customFlat.unflatten({});
    {
        var _flatFieldSchema_$and, _flatFieldSchema_$or;
        for(const filterKey in filterSchemaItem){
            var _filterSchemaItem_filterKey;
            const match = null === (_filterSchemaItem_filterKey = filterSchemaItem[filterKey]) || void 0 === _filterSchemaItem_filterKey ? void 0 : _filterSchemaItem_filterKey.slice(11, -2);
            const collection = null == match ? void 0 : match.split('.')[0];
            const filterItems = Object.keys(items).filter((item)=>item.includes(collection));
            if (filterItems.length > 1) filterSchemaItem[filterKey] = filterItems.map((key)=>items[key]);
            else filterSchemaItem[filterKey] = items[filterItems[0]];
        }
        for(const item in filterSchemaItem)if (!filterSchemaItem[item] || filterSchemaItem[item].includes('$nFilter')) delete filterSchemaItem[item];
        const flatFieldSchema = customFlat.unflatten(filterSchemaItem);
        flatFieldSchema['$and'] = null == flatFieldSchema ? void 0 : null === (_flatFieldSchema_$and = flatFieldSchema['$and']) || void 0 === _flatFieldSchema_$and ? void 0 : _flatFieldSchema_$and.filter(Boolean);
        flatFieldSchema['$or'] = null == flatFieldSchema ? void 0 : null === (_flatFieldSchema_$or = flatFieldSchema['$or']) || void 0 === _flatFieldSchema_$or ? void 0 : _flatFieldSchema_$or.filter(Boolean);
        return flatFieldSchema;
    }
};
const removeNullCondition = function(filter) {
    let customFlat = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : external_flat_default();
    const items = customFlat(filter || {});
    const values = {};
    for(const key in items){
        const value = items[key];
        if (null != value && !isEmpty(value)) values[key] = value;
    }
    return customFlat.unflatten(values);
};
const useFilterActionProps = ()=>{
    const { name } = (0, external_collection_manager_index_js_namespaceObject.useCollection_deprecated)();
    const options = useFilterOptions(name);
    const { service, props } = (0, index_js_namespaceObject.useBlockRequestContext)();
    return useFilterFieldProps({
        options,
        service,
        params: null == props ? void 0 : props.params
    });
};
const useFilterFieldProps = (param)=>{
    let { options, service, params } = param;
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const field = (0, schema_namespaceObject.useField)();
    const dataLoadingMode = (0, setDataLoadingModeSettingsItem_js_namespaceObject.useDataLoadingMode)();
    return {
        options,
        onSubmit (values) {
            var _service_params_, _service_params, _service_params1;
            const defaultFilter = params.filter;
            const filter = removeNullCondition(null == values ? void 0 : values.filter);
            if ('manual' === dataLoadingMode && external_lodash_default().isEmpty(filter)) return service.mutate(void 0);
            const filters = (null === (_service_params = service.params) || void 0 === _service_params ? void 0 : null === (_service_params_ = _service_params[1]) || void 0 === _service_params_ ? void 0 : _service_params_.filters) || {};
            filters["filterAction"] = filter;
            service.run({
                ...null === (_service_params1 = service.params) || void 0 === _service_params1 ? void 0 : _service_params1[0],
                page: 1,
                filter: (0, utils_js_namespaceObject.mergeFilter)([
                    ...Object.values(filters),
                    defaultFilter
                ])
            }, {
                filters
            });
            const items = (null == filter ? void 0 : filter.$and) || (null == filter ? void 0 : filter.$or);
            if (null == items ? void 0 : items.length) field.title = t('{{count}} filter items', {
                count: (null == items ? void 0 : items.length) || 0
            });
            else field.title = t('Filter');
        },
        onReset () {
            var _service_params_, _service_params, _service_params1;
            const filter = params.filter;
            const filters = (null === (_service_params = service.params) || void 0 === _service_params ? void 0 : null === (_service_params_ = _service_params[1]) || void 0 === _service_params_ ? void 0 : _service_params_.filters) || {};
            delete filters["filterAction"];
            const newParams = [
                {
                    ...null === (_service_params1 = service.params) || void 0 === _service_params1 ? void 0 : _service_params1[0],
                    filter: (0, utils_js_namespaceObject.mergeFilter)([
                        ...Object.values(filters),
                        filter
                    ]),
                    page: 1
                },
                {
                    filters
                }
            ];
            field.title = t('Filter');
            if ('manual' === dataLoadingMode) {
                service.params = newParams;
                return service.mutate(void 0);
            }
            service.run(...newParams);
        }
    };
};
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
});
