"use strict";
var __webpack_require__ = {};
(()=>{
    __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_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, index)=>{
        const item = items.find((v)=>v.name === name);
        if (item) option = item;
        items = (null == item ? void 0 : item.children) || [];
    });
    return option;
};
const useValues = (options)=>{
    const field = (0, schema_namespaceObject.useField)();
    const logic = (0, external_react_namespaceObject.useContext)(external_context_js_namespaceObject.LinkageLogicContext);
    const value2data = ()=>{
        var _field_data;
        field.data = {
            ...field.initialValue || field.value
        };
        const dataIndex = null === (_field_data = field.data) || void 0 === _field_data ? void 0 : _field_data.targetFields;
        const option = dataIndex && findOption(dataIndex, options) || {};
        const operators = (null == option ? void 0 : option.operators) || [];
        field.data.operators = operators;
        field.data.schema = null == option ? void 0 : option.schema;
    };
    (0, external_react_namespaceObject.useEffect)(value2data, [
        logic
    ]);
    return {
        fields: options,
        ...field.data,
        setDataIndex (dataIndex) {
            const option = findOption(dataIndex, options);
            field.data = field.data || {};
            const operators = null == option ? void 0 : option.operators;
            field.data.operators = null == operators ? void 0 : operators.filter((v)=>true);
            field.data.schema = null == option ? void 0 : option.schema;
            field.value = field.value || [];
            field.data.operator = void 0;
            field.data.value = void 0;
            field.value = {
                ...field.value,
                targetFields: dataIndex,
                operator: void 0,
                value: void 0
            };
        },
        setOperator (operatorValue) {
            field.data.operator = operatorValue;
            field.value = {
                ...field.value,
                operator: operatorValue
            };
        },
        setValue (value) {
            field.data.value = value;
            field.value = {
                ...field.value,
                value
            };
        }
    };
};
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
});
