import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__ from "../../../data-source/index.mjs";
const AssociationCascader = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.connect)((props)=>{
    var _props_params, _props_params1;
    const { fieldNames, collection, associationField } = props;
    const cm = (0, __WEBPACK_EXTERNAL_MODULE__data_source_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)({
        resource: collection,
        action: 'list',
        params
    });
    const options = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SingleValueCascader, {
        options: options,
        ...props,
        showSearch: true,
        titleField: titleField,
        joinTitleField: joinTitleField
    });
}, (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Cascader, {
        ...newProps
    });
};
SingleValueCascader.displayName = 'SingleValueCascader';
AssociationCascader.displayName = 'AssociationCascader';
const AssociationCascader_rslib_entry_ = AssociationCascader;
export { AssociationCascader, SingleValueCascader, AssociationCascader_rslib_entry_ as default };
