import { jsx, jsxs } from "react/jsx-runtime";
import { useEffect, useMemo, useState } from "react";
import { connect, mapReadPretty, useFieldSchema } from "@tachybase/schema";
import { Select } from "antd";
import { useCollection_deprecated } from "../../../collection-manager/index.mjs";
import { useCompile } from "../../hooks/index.mjs";
import { EllipsisWithTooltip } from "../input/index.mjs";
import Cron from "./Cron.mjs";
const CronSetInternal = (props)=>{
    var _getField;
    const { onChange, value } = props;
    const { getField } = useCollection_deprecated();
    const fieldSchema = useFieldSchema();
    const uiSchemaOptions = null == (_getField = getField(null == fieldSchema ? void 0 : fieldSchema.name)) ? void 0 : _getField.uiSchema.enum;
    const compile = useCompile();
    const [customizeOption, setCustomizeOption] = useState({
        label: "{{t('Custom')}}",
        value: '@daily'
    });
    const options = useMemo(()=>(props.options || []).concat(uiSchemaOptions || []).concat([
            customizeOption
        ]).map((item)=>({
                ...item,
                label: compile(item.label)
            })), [
        props.options,
        customizeOption
    ]);
    useEffect(()=>{
        if (null == value) return;
        const item = options.find((o)=>o.value === value);
        if (!item) setCustomizeOption({
            ...customizeOption,
            value
        });
    }, [
        options,
        value
    ]);
    const isCustomize = useMemo(()=>value === customizeOption.value, [
        value,
        customizeOption
    ]);
    const onCronChange = function(value) {
        let customize = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : false;
        if (customize) setCustomizeOption({
            ...customizeOption,
            value
        });
        onChange(value);
    };
    return /*#__PURE__*/ jsxs("fieldset", {
        children: [
            /*#__PURE__*/ jsx(Select, {
                popupMatchSelectWidth: false,
                allowClear: true,
                ...props,
                value: value,
                options: options,
                onChange: (value)=>onCronChange(value)
            }),
            isCustomize ? /*#__PURE__*/ jsx(Cron, {
                value: value,
                onChange: (v)=>onCronChange(v, true),
                allowedDropdowns: [
                    'period',
                    'week-days',
                    'months',
                    'month-days'
                ],
                allowedPeriods: [
                    'year',
                    'month',
                    'week',
                    'day'
                ]
            }) : null
        ]
    });
};
const ReadPretty = (props)=>{
    var _getField;
    const { value } = props;
    const compile = useCompile();
    const fieldSchema = useFieldSchema();
    const { getField } = useCollection_deprecated();
    const uiSchemaOptions = null == (_getField = getField(null == fieldSchema ? void 0 : fieldSchema.name)) ? void 0 : _getField.uiSchema.enum;
    const options = useMemo(()=>(props.options || []).concat(uiSchemaOptions || []), [
        props.options
    ]);
    const label = useMemo(()=>{
        var _options_find;
        return value && (null == options ? void 0 : null == (_options_find = options.find((o)=>o.value === value)) ? void 0 : _options_find.label);
    }, [
        options,
        value
    ]);
    return /*#__PURE__*/ jsx(EllipsisWithTooltip, {
        ellipsis: true,
        children: value && (label ? compile(label) : /*#__PURE__*/ jsx(Cron.ReadPretty, {
            ...props
        }))
    });
};
const CronSet = connect(CronSetInternal, mapReadPretty(ReadPretty));
export { CronSet };
