"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__, {
    SingleValueCascader: ()=>SingleValueCascader,
    AssociationCascader: ()=>AssociationCascader,
    default: ()=>AssociationCascader_rslib_entry_
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const index_js_namespaceObject = require("../../../api-client/index.js");
const external_data_source_index_js_namespaceObject = require("../../../data-source/index.js");
const AssociationCascader = (0, schema_namespaceObject.connect)((props)=>{
    var _props_params, _props_params1;
    const { fieldNames, collection, associationField } = props;
    const cm = (0, external_data_source_index_js_namespaceObject.useCollectionManager)();
    const titleField = cm.getCollection(collection).titleField;
    const joinTitleField = cm.getCollection(collection + '.' + associationField).titleField;
    const params = {
        appends: [
            associationField
        ],
        fields: [
            titleField
        ],
        pageSize: 99999,
        filter: (null == props ? void 0 : null === (_props_params = props.params) || void 0 === _props_params ? void 0 : _props_params.filter) ? {
            ...null == props ? void 0 : null === (_props_params1 = props.params) || void 0 === _props_params1 ? void 0 : _props_params1.filter
        } : {}
    };
    const { data } = (0, index_js_namespaceObject.useRequest)({
        resource: collection,
        action: 'list',
        params
    });
    const options = (0, external_react_namespaceObject.useMemo)(()=>{
        if (props.chartCascader) {
            const dataOptions = {};
            const options = [];
            null == data || data.data.forEach((item)=>{
                if (null == item ? void 0 : item[associationField]) {
                    const field = [];
                    if (Array.isArray(item[associationField]) && item[associationField].length) field.push(...item[associationField]);
                    else field.push({
                        ...item[associationField]
                    });
                    field.forEach((fieldItem)=>{
                        if (!Object.keys(fieldItem).length) return;
                        if (dataOptions[fieldItem[joinTitleField]]) dataOptions[fieldItem[joinTitleField]].push({
                            label: item[titleField],
                            value: item[titleField]
                        });
                        else dataOptions[fieldItem[joinTitleField]] = [
                            {
                                label: item[titleField],
                                value: item[titleField]
                            }
                        ];
                    });
                }
            });
            for(const key in dataOptions)options.push({
                label: key,
                value: key,
                children: [
                    ...dataOptions[key]
                ]
            });
            return options;
        }
        {
            const dict = (null == data ? void 0 : data.data.reduce((acc, item)=>{
                if (item[associationField]) {
                    var _acc, _key;
                    const key = item[associationField][joinTitleField];
                    var _;
                    null !== (_ = (_acc = acc)[_key = key]) && void 0 !== _ || (_acc[_key] = []);
                    acc[key].push(item[titleField]);
                }
                return acc;
            }, {})) || {};
            const options = Object.entries(dict).map((param)=>{
                let [key, values] = param;
                return {
                    [fieldNames.label]: key,
                    [fieldNames.value]: key,
                    children: values.map((value)=>({
                            [fieldNames.label]: value,
                            [fieldNames.value]: value
                        }))
                };
            });
            return options;
        }
    }, [
        associationField,
        joinTitleField,
        titleField,
        null == data ? void 0 : data.data
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(SingleValueCascader, {
        options: options,
        ...props,
        showSearch: true,
        titleField: titleField,
        joinTitleField: joinTitleField
    });
}, (0, schema_namespaceObject.mapProps)((props)=>({
        ...props
    })));
const SingleValueCascader = (props)=>{
    const { value, options, onChange, fieldNames, chartCascader, titleField, joinTitleField } = props;
    const fieldLabel = fieldNames ? fieldNames.value : titleField;
    const joinFieldLabel = fieldNames ? fieldNames.value : 'value';
    const arrayValue = value && options ? [] : void 0;
    if (arrayValue) for (const option of options){
        if (option[fieldLabel] === value) {
            arrayValue.push(option[fieldLabel]);
            break;
        }
        for (const subOption of option.children)if (subOption[joinFieldLabel] === value) {
            arrayValue.push(option[joinFieldLabel]);
            arrayValue.push(subOption[joinFieldLabel]);
            break;
        }
    }
    const newProps = {
        ...props,
        value: arrayValue,
        onChange: (v)=>{
            v ? onChange(v[v.length - 1]) : onChange(v);
        }
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Cascader, {
        ...newProps
    });
};
SingleValueCascader.displayName = 'SingleValueCascader';
AssociationCascader.displayName = 'AssociationCascader';
const AssociationCascader_rslib_entry_ = AssociationCascader;
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
});
