import { jsx } from "react/jsx-runtime";
import { useState } from "react";
import { connect, isValid, mapProps, mapReadPretty, useField } from "@tachybase/schema";
import { CheckOutlined, CloseOutlined } from "@ant-design/icons";
import { Checkbox, Radio, Tag } from "antd";
import uniq from "lodash/uniq";
import { useTranslation } from "react-i18next";
import { useCollectionField } from "../../../data-source/collection-field/CollectionFieldProvider.mjs";
import { EllipsisWithTooltip } from "../input/EllipsisWithTooltip.mjs";
const ReadPretty = (props)=>{
    if (props.value) return /*#__PURE__*/ jsx(CheckOutlined, {
        style: {
            color: '#52c41a'
        }
    });
    return props.showUnchecked ? /*#__PURE__*/ jsx(CloseOutlined, {
        style: {
            color: '#ff4d4f'
        }
    }) : null;
};
const InternalCheckbox = connect((props)=>{
    const changeHandler = (val)=>{
        null == props || props.onChange(val);
    };
    return /*#__PURE__*/ jsx(Checkbox, {
        ...props,
        onChange: changeHandler
    });
}, mapProps({
    value: 'checked',
    onInput: 'onChange'
}), mapReadPretty(ReadPretty));
const InternalRadioGroup = connect((props)=>{
    const { t } = useTranslation();
    const [value, setValue] = useState();
    return /*#__PURE__*/ jsx(Radio.Group, {
        ...props,
        onChange: (value)=>{
            if ('null' !== value.target.value) props.onChange(value.target.value);
            else props.onChange(null);
            setValue(value.target.value);
        },
        value: props.value || value,
        options: [
            {
                label: t('?'),
                value: 'null'
            },
            {
                label: t('Yes'),
                value: 'true'
            },
            {
                label: t('No'),
                value: 'false'
            }
        ],
        optionType: "button"
    });
}, mapReadPretty(ReadPretty));
const Checkbox_Checkbox = (props)=>{
    switch(props.mode){
        case 'Radio group':
            return /*#__PURE__*/ jsx(InternalRadioGroup, {
                ...props
            });
        case 'Checkbox':
        default:
            return /*#__PURE__*/ jsx(InternalCheckbox, {
                ...props
            });
    }
};
Checkbox_Checkbox.ReadPretty = ReadPretty;
Checkbox_Checkbox.__ANT_CHECKBOX = true;
Checkbox_Checkbox.Group = connect(Checkbox.Group, mapProps({
    dataSource: 'options'
}), mapReadPretty((props)=>{
    if (!isValid(props.value)) return null;
    const field = useField();
    const collectionField = useCollectionField();
    const dataSource = field.dataSource || (null == collectionField ? void 0 : collectionField.uiSchema.enum) || [];
    const value = uniq(field.value ? field.value : []);
    return /*#__PURE__*/ jsx(EllipsisWithTooltip, {
        ellipsis: props.ellipsis,
        children: dataSource.filter((option)=>value.includes(option.value)).map((option, key)=>/*#__PURE__*/ jsx(Tag, {
                color: option.color,
                icon: option.icon,
                children: option.label
            }, key))
    });
}));
const checkbox_Checkbox = Checkbox_Checkbox;
export { Checkbox_Checkbox as Checkbox, InternalCheckbox, InternalRadioGroup, checkbox_Checkbox as default };
