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