import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { connect, mapProps } from "@tachybase/schema";
import { Cascader } from "antd";
import { useRequest } from "../../../api-client/index.mjs";
import { useCollectionManager } from "../../../data-source/index.mjs";
const AssociationCascader = connect((props)=>{
    var _props_params, _props_params1;
    const { fieldNames, collection, associationField } = props;
    const cm = useCollectionManager();
    const titleField = cm.getCollection(collection).titleField;
    const joinTitleField = cm.getCollection(collection + '.' + associationField).titleField;
    const params = {
        appends: [
            associationField
        ],
        fields: [
            titleField
        ],
        paginate: false,
        filter: (null == props ? void 0 : null == (_props_params = props.params) ? void 0 : _props_params.filter) ? {
            ...null == props ? void 0 : null == (_props_params1 = props.params) ? void 0 : _props_params1.filter
        } : {}
    };
    const { data } = useRequest({
        resource: collection,
        action: 'list',
        params
    });
    const options = 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];
                    null != (_acc = acc)[_key = key] || (_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__*/ jsx(SingleValueCascader, {
        options: options,
        ...props,
        showSearch: true,
        titleField: titleField,
        joinTitleField: joinTitleField
    });
}, 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__*/ jsx(Cascader, {
        ...newProps
    });
};
SingleValueCascader.displayName = 'SingleValueCascader';
AssociationCascader.displayName = 'AssociationCascader';
const association_cascader_AssociationCascader = AssociationCascader;
export { AssociationCascader, SingleValueCascader, association_cascader_AssociationCascader as default };
