import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_flat__ from "flat";
import * as __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__ from "lodash/cloneDeep";
import * as __WEBPACK_EXTERNAL_MODULE_lodash_get__ from "lodash/get";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__context_mjs__ from "./context.mjs";
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, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { options, collectionName, field: ctxField } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.FilterContext) || {};
    const values = (0, __WEBPACK_EXTERNAL_MODULE_flat__["default"])(field.value || {});
    const path = Object.keys(values).shift() || '';
    const collectionField = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const [fieldPath = ''] = path.split('.$');
        return getCollectionJoinField("".concat(collectionName || name, ".").concat(fieldPath));
    }, [
        name,
        path
    ]);
    const data2value = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(()=>{
        var _field_data_dataIndex, _field_data_operator, _field_data, _field_data1;
        field.value = __WEBPACK_EXTERNAL_MODULE_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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.merge)(null == option ? void 0 : option.schema, null == operator ? void 0 : operator.schema);
        field.data.value = (0, __WEBPACK_EXTERNAL_MODULE_lodash_get__["default"])((0, __WEBPACK_EXTERNAL_MODULE_flat__.unflatten)(field.value), "".concat(fieldPath, ".$").concat(operatorValue));
    };
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(value2data, [
        field.path
    ]);
    const setDataIndex = (0, __WEBPACK_EXTERNAL_MODULE_react__.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 = (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(null == option ? void 0 : option.schema);
        const s2 = (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(null == operator ? void 0 : operator.schema);
        field.data.schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE_react__.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 = (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(null == option ? void 0 : option.schema);
        const s2 = (0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(null == operator ? void 0 : operator.schema);
        field.data.schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.merge)(s1, s2);
        field.data.value = operator.noValue ? operator.default || true : void 0;
        data2value();
    }, [
        data2value,
        field.data,
        options
    ]);
    const setValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((value)=>{
        field.data.value = value;
        data2value();
    }, [
        data2value,
        field.data
    ]);
    return {
        fields: options,
        ...(null == field ? void 0 : field.data) || {},
        collectionField,
        setDataIndex,
        setOperator,
        setValue
    };
};
export { useValues };
