"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__, {
    useValues: ()=>useValues
});
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_flat_namespaceObject = require("flat");
var external_flat_default = /*#__PURE__*/ __webpack_require__.n(external_flat_namespaceObject);
const cloneDeep_namespaceObject = require("lodash/cloneDeep");
var cloneDeep_default = /*#__PURE__*/ __webpack_require__.n(cloneDeep_namespaceObject);
const get_namespaceObject = require("lodash/get");
var get_default = /*#__PURE__*/ __webpack_require__.n(get_namespaceObject);
const index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_context_js_namespaceObject = require("./context.js");
const findOption = function() {
    let dataIndex = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [], options = arguments.length > 1 ? arguments[1] : void 0;
    var _dataIndex_forEach;
    let items = options;
    let option;
    null == dataIndex || null === (_dataIndex_forEach = dataIndex.forEach) || void 0 === _dataIndex_forEach || _dataIndex_forEach.call(dataIndex, (name)=>{
        const item = items.find((item)=>item.name === name);
        if (item) option = item;
        items = (null == item ? void 0 : item.children) || [];
    });
    return option;
};
const useValues = ()=>{
    const { name } = (0, index_js_namespaceObject.useCollection_deprecated)();
    const { getCollectionJoinField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const field = (0, schema_namespaceObject.useField)();
    const { options, collectionName, field: ctxField } = (0, external_react_namespaceObject.useContext)(external_context_js_namespaceObject.FilterContext) || {};
    const values = external_flat_default()(field.value || {});
    const path = Object.keys(values).shift() || '';
    const collectionField = (0, external_react_namespaceObject.useMemo)(()=>{
        const [fieldPath = ''] = path.split('.$');
        return getCollectionJoinField("".concat(collectionName || name, ".").concat(fieldPath));
    }, [
        name,
        path
    ]);
    const data2value = (0, external_react_namespaceObject.useCallback)(()=>{
        var _field_data_dataIndex, _field_data_operator, _field_data, _field_data1;
        field.value = external_flat_default().unflatten({
            ["".concat(null === (_field_data_dataIndex = field.data.dataIndex) || void 0 === _field_data_dataIndex ? void 0 : _field_data_dataIndex.join('.'), ".").concat(null === (_field_data = field.data) || void 0 === _field_data ? void 0 : null === (_field_data_operator = _field_data.operator) || void 0 === _field_data_operator ? void 0 : _field_data_operator.value)]: null === (_field_data1 = field.data) || void 0 === _field_data1 ? void 0 : _field_data1.value
        });
    }, [
        field
    ]);
    const value2data = ()=>{
        var _operators_find;
        field.data = field.data || {};
        if (!path || !options) return;
        const [fieldPath = '', otherPath = ''] = path.split('.$');
        const [operatorValue] = otherPath.split('.', 2);
        const dataIndex = fieldPath.split('.');
        const option = findOption(dataIndex, options);
        const operators = null == option ? void 0 : option.operators;
        const operator = null == operators ? void 0 : null === (_operators_find = operators.find) || void 0 === _operators_find ? void 0 : _operators_find.call(operators, (item)=>item.value === "$".concat(operatorValue));
        field.data.dataIndex = dataIndex;
        if ((null == dataIndex ? void 0 : dataIndex.length) > 1) {
            const fieldNames = dataIndex.concat();
            fieldNames.pop();
            const targetField = getCollectionJoinField("".concat(name, ".").concat(fieldNames.join('.')));
            ctxField.collectionName = null == targetField ? void 0 : targetField.target;
        } else ctxField.collectionName = null;
        field.data.operators = operators;
        field.data.operator = operator;
        field.data.schema = (0, schema_namespaceObject.merge)(null == option ? void 0 : option.schema, null == operator ? void 0 : operator.schema);
        field.data.value = get_default()((0, external_flat_namespaceObject.unflatten)(field.value), "".concat(fieldPath, ".$").concat(operatorValue));
    };
    (0, external_react_namespaceObject.useEffect)(value2data, [
        field.path
    ]);
    const setDataIndex = (0, external_react_namespaceObject.useCallback)((dataIndex)=>{
        var _option_operators;
        const option = findOption(dataIndex, options);
        const operator = null == option ? void 0 : null === (_option_operators = option.operators) || void 0 === _option_operators ? void 0 : _option_operators[0];
        field.data = field.data || {};
        field.data.operators = null == option ? void 0 : option.operators;
        field.data.operator = operator;
        const s1 = cloneDeep_default()(null == option ? void 0 : option.schema);
        const s2 = cloneDeep_default()(null == operator ? void 0 : operator.schema);
        field.data.schema = (0, schema_namespaceObject.merge)(s1, s2);
        field.data.dataIndex = dataIndex;
        if ((null == dataIndex ? void 0 : dataIndex.length) > 1) {
            const fieldNames = dataIndex.concat();
            fieldNames.pop();
            const targetField = getCollectionJoinField("".concat(name, ".").concat(fieldNames.join('.')));
            ctxField.collectionName = null == targetField ? void 0 : targetField.target;
        } else ctxField.collectionName = null;
        field.data.value = (null == operator ? void 0 : operator.noValue) ? operator.default || true : void 0;
        data2value();
    }, [
        data2value,
        field,
        options
    ]);
    const setOperator = (0, external_react_namespaceObject.useCallback)((operatorValue)=>{
        var _field_data_operators_find, _field_data_operators, _field_data;
        const operator = null === (_field_data = field.data) || void 0 === _field_data ? void 0 : null === (_field_data_operators = _field_data.operators) || void 0 === _field_data_operators ? void 0 : null === (_field_data_operators_find = _field_data_operators.find) || void 0 === _field_data_operators_find ? void 0 : _field_data_operators_find.call(_field_data_operators, (item)=>item.value === operatorValue);
        field.data.operator = operator;
        const option = findOption(field.data.dataIndex, options);
        const s1 = cloneDeep_default()(null == option ? void 0 : option.schema);
        const s2 = cloneDeep_default()(null == operator ? void 0 : operator.schema);
        field.data.schema = (0, schema_namespaceObject.merge)(s1, s2);
        field.data.value = operator.noValue ? operator.default || true : void 0;
        data2value();
    }, [
        data2value,
        field.data,
        options
    ]);
    const setValue = (0, external_react_namespaceObject.useCallback)((value)=>{
        field.data.value = value;
        data2value();
    }, [
        data2value,
        field.data
    ]);
    return {
        fields: options,
        ...(null == field ? void 0 : field.data) || {},
        collectionField,
        setDataIndex,
        setOperator,
        setValue
    };
};
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
});
