import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { connect, mapProps } from "@tachybase/schema";
import { useBoolean } from "ahooks";
import { Input, Radio, Space } from "antd";
import { createStyles } from "antd-style";
import dayjs from "dayjs";
import { useToken } from "../../index.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      width: 100%;\n      & > .ant-space-item {\n        flex: 1;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const ExpiresRadio_date = dayjs();
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        space: css(_templateObject())
    };
});
const DateFormatCom = (props)=>{
    const date = dayjs();
    return /*#__PURE__*/ jsxs("div", {
        style: {
            display: 'inline-flex'
        },
        children: [
            /*#__PURE__*/ jsx("span", {
                children: props.format
            }),
            /*#__PURE__*/ jsx(DateTimeFormatPreview, {
                content: date.format(props.format)
            })
        ]
    });
};
const DateTimeFormatPreview = (param)=>{
    let { content } = param;
    const { token } = useToken();
    return /*#__PURE__*/ jsx("span", {
        style: {
            display: 'inline-block',
            background: token.colorBgTextHover,
            marginLeft: token.marginMD,
            lineHeight: '1',
            padding: token.paddingXXS,
            borderRadius: token.borderRadiusOuter
        },
        children: content
    });
};
const InternalExpiresRadio = (props)=>{
    const { styles } = useStyles();
    const { onChange, defaultValue, formats, timeFormat } = props;
    const [isCustom, { setFalse, setTrue }] = useBoolean(props.value && !formats.includes(props.value));
    const targetValue = props.value && !formats.includes(props.value) ? props.value : defaultValue;
    const [customFormatPreview, setCustomFormatPreview] = useState(targetValue ? ExpiresRadio_date.format(targetValue) : null);
    const onSelectChange = (v)=>{
        if ('custom' === v.target.value) {
            setTrue();
            onChange(targetValue);
        } else {
            setFalse();
            onChange(v.target.value);
        }
    };
    return /*#__PURE__*/ jsx(Space, {
        className: styles.space,
        children: /*#__PURE__*/ jsx(Radio.Group, {
            value: isCustom ? 'custom' : props.value,
            onChange: onSelectChange,
            children: /*#__PURE__*/ jsx(Space, {
                direction: "vertical",
                children: props.options.map((v)=>{
                    if ('custom' === v.value) return /*#__PURE__*/ jsxs(Radio, {
                        value: v.value,
                        children: [
                            /*#__PURE__*/ jsx(Input, {
                                style: {
                                    width: '150px'
                                },
                                defaultValue: targetValue,
                                onChange: (e)=>{
                                    e.target.value ? setCustomFormatPreview(ExpiresRadio_date.format(e.target.value)) : setCustomFormatPreview(null);
                                    if (isCustom) onChange(e.target.value);
                                }
                            }),
                            /*#__PURE__*/ jsx(DateTimeFormatPreview, {
                                content: customFormatPreview
                            })
                        ]
                    }, v.value);
                    return /*#__PURE__*/ jsx(Radio, {
                        value: v.value,
                        "aria-label": v.value,
                        children: /*#__PURE__*/ jsx("span", {
                            role: "button",
                            "aria-label": v.value,
                            children: v.label
                        })
                    }, v.value);
                })
            })
        })
    });
};
const ExpiresRadio = connect(InternalExpiresRadio, mapProps({
    dataSource: 'options'
}));
export { DateFormatCom, ExpiresRadio };
