import { jsx } from "react/jsx-runtime";
import { connect, isValid, mapProps, mapReadPretty, toArr } from "@tachybase/schema";
import { isPlainObject } from "@tachybase/utils/client";
import { CloseCircleFilled, CloseOutlined, LoadingOutlined } from "@ant-design/icons";
import { Empty, Radio, Select, Spin, Tag } from "antd";
import FormulaSelect from "./FormulaSelect.mjs";
import { ReadPretty } from "./ReadPretty.mjs";
import { defaultFieldNames, getCurrentOptions } from "./utils.mjs";
const isEmptyObject = (val)=>!isValid(val) || 'object' == typeof val && 0 === Object.keys(val).length;
const ObjectSelect = (props)=>{
    const { value, options, onChange, fieldNames, mode, loading, rawOptions, defaultValue, ...others } = props;
    const toValue = (v)=>{
        var _getCurrentOptions;
        if (isEmptyObject(v)) return;
        const values = toArr(v).filter((item)=>item).map((val)=>isPlainObject(val) ? val[fieldNames.value] : val);
        const currentOptions = null == (_getCurrentOptions = getCurrentOptions(values, options, fieldNames)) ? void 0 : _getCurrentOptions.map((val)=>({
                label: val[fieldNames.label],
                value: val[fieldNames.value]
            }));
        if ([
            'tags',
            'multiple'
        ].includes(mode) || props.multiple) return currentOptions;
        return currentOptions.shift();
    };
    return /*#__PURE__*/ jsx(Select, {
        role: "button",
        "data-testid": "select-object-".concat(mode || 'single'),
        value: toValue(value),
        defaultValue: toValue(defaultValue),
        allowClear: {
            clearIcon: /*#__PURE__*/ jsx(CloseCircleFilled, {
                role: "button",
                "aria-label": "icon-close-select"
            })
        },
        labelInValue: true,
        notFoundContent: loading ? /*#__PURE__*/ jsx(Spin, {}) : /*#__PURE__*/ jsx(Empty, {
            image: Empty.PRESENTED_IMAGE_SIMPLE
        }),
        options: options,
        fieldNames: fieldNames,
        showSearch: true,
        popupMatchSelectWidth: false,
        filterOption: (input, option)=>{
            var _option_;
            return (null != (_option_ = null == option ? void 0 : option[fieldNames.label || 'label']) ? _option_ : '').includes(input);
        },
        filterSort: (optionA, optionB)=>{
            var _optionA_, _optionB_;
            return (null != (_optionA_ = null == optionA ? void 0 : optionA[fieldNames.label || 'label']) ? _optionA_ : '').toLowerCase().localeCompare((null != (_optionB_ = null == optionB ? void 0 : optionB[fieldNames.label || 'label']) ? _optionB_ : '').toLowerCase());
        },
        onChange: (changed)=>{
            const current = getCurrentOptions(toArr(changed).map((v)=>v.value), rawOptions || options, fieldNames);
            if ([
                'tags',
                'multiple'
            ].includes(mode) || props.multiple) null == onChange || onChange(current);
            else null == onChange || onChange(current.shift() || null);
        },
        mode: mode,
        tagRender: (props)=>/*#__PURE__*/ jsx(Tag, {
                role: "button",
                "aria-label": props.label,
                closeIcon: /*#__PURE__*/ jsx(CloseOutlined, {
                    role: "button",
                    "aria-label": "icon-close-tag"
                }),
                ...props,
                children: props.label
            }),
        ...others
    });
};
const filterOption = (input, option)=>{
    var _option_label;
    return (null != (_option_label = null == option ? void 0 : option.label) ? _option_label : '').toLowerCase().includes((input || '').toLowerCase());
};
const InternalSelect = connect((props)=>{
    const { objectValue, loading, value, rawOptions, defaultValue, ...others } = props;
    let mode = props.multiple ? 'multiple' : props.mode;
    if (mode && ![
        'multiple',
        'tags'
    ].includes(mode)) mode = void 0;
    if ([
        'CustomTitle'
    ].includes(props.mode) && ('formula' in others.fieldNames || 'collection' in props)) return /*#__PURE__*/ jsx(FormulaSelect, {
        ...props
    });
    if (objectValue) return /*#__PURE__*/ jsx(ObjectSelect, {
        rawOptions: rawOptions,
        ...others,
        defaultValue: defaultValue,
        value: value,
        mode: mode,
        loading: loading
    });
    const toValue = (v)=>{
        if ([
            'tags',
            'multiple'
        ].includes(props.mode) || props.multiple) {
            if (v) return toArr(v);
            return;
        }
        return v;
    };
    return /*#__PURE__*/ jsx(Select, {
        role: "button",
        "data-testid": "select-".concat(mode || 'single'),
        showSearch: true,
        filterOption: filterOption,
        allowClear: {
            clearIcon: /*#__PURE__*/ jsx(CloseCircleFilled, {
                role: "button",
                "aria-label": "icon-close-select"
            })
        },
        popupMatchSelectWidth: false,
        notFoundContent: loading ? /*#__PURE__*/ jsx(Spin, {}) : /*#__PURE__*/ jsx(Empty, {
            image: Empty.PRESENTED_IMAGE_SIMPLE
        }),
        value: toValue(value),
        defaultValue: toValue(defaultValue),
        tagRender: (props)=>/*#__PURE__*/ jsx(Tag, {
                role: "button",
                "aria-label": props.label,
                closeIcon: /*#__PURE__*/ jsx(CloseOutlined, {
                    role: "button",
                    "aria-label": "icon-close-tag"
                }),
                ...props,
                children: props.label
            }),
        ...others,
        onChange: (changed)=>{
            var _props_onChange;
            null == (_props_onChange = props.onChange) || _props_onChange.call(props, void 0 === changed ? null : changed);
        },
        mode: mode
    });
}, mapProps({
    dataSource: 'options'
}, (props, field)=>({
        ...props,
        fieldNames: {
            ...defaultFieldNames,
            ...props.fieldNames
        },
        suffixIcon: (null == field ? void 0 : field['loading']) || (null == field ? void 0 : field['validating']) ? /*#__PURE__*/ jsx(LoadingOutlined, {}) : props.suffixIcon
    })), mapReadPretty(ReadPretty));
const InternalRadioGroup = connect((props)=>{
    const { options, value, onChange } = props;
    const handleCancel = (e)=>{
        e.stopPropagation();
        const currentValue = e.target.value;
        if ('radio' === e.target.type && currentValue === value) null == onChange || onChange('');
    };
    return /*#__PURE__*/ jsx("div", {
        onClick: handleCancel,
        children: /*#__PURE__*/ jsx(Radio.Group, {
            ...props,
            value: value,
            options: options
        })
    });
}, mapProps({
    dataSource: 'options',
    onInput: 'onChange',
    value: 'value'
}), mapReadPretty(ReadPretty));
const Select_Select = (props)=>{
    switch(props.mode){
        case 'Radio group':
            return /*#__PURE__*/ jsx(InternalRadioGroup, {
                ...props
            });
        case 'Select':
        default:
            return /*#__PURE__*/ jsx(InternalSelect, {
                ...props
            });
    }
};
Select_Select.ReadPretty = ReadPretty;
const select_Select = Select_Select;
export { Select_Select as Select, select_Select as default };
