import { jsx } from "react/jsx-runtime";
import { useMemo } from "react";
import { connect, mapReadPretty } from "@tachybase/schema";
import { error } from "@tego/client";
import { createStyles } from "antd-style";
import cronstrue from "cronstrue";
import { Cron } from "react-js-cron";
import { useAPIClient } from "../../../api-client/index.mjs";
import "react-js-cron/dist/styles.css";
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      .react-js-cron {\n        padding: 0.5em 0.5em 0 0.5em;\n        border: 1px dashed #ccc;\n        .react-js-cron-field {\n          flex-shrink: 0;\n          margin-bottom: 0.5em;\n\n          > span {\n            flex-shrink: 0;\n            margin: 0 0.5em 0 0;\n          }\n\n          > .react-js-cron-select {\n            margin: 0 0.5em 0 0;\n          }\n        }\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        input: css(_templateObject())
    };
});
const Input = (props)=>{
    const { onChange, ...rest } = props;
    const { styles } = useStyles();
    return /*#__PURE__*/ jsx("fieldset", {
        className: styles.input,
        children: /*#__PURE__*/ jsx(Cron, {
            setValue: onChange,
            locale: window['cronLocale'],
            ...rest
        })
    });
};
const ReadPretty = (props)=>{
    const api = useAPIClient();
    const locale = api.auth.getLocale();
    const value = useMemo(()=>{
        try {
            return cronstrue.toString(props.value, {
                locale,
                use24HourTimeFormat: true
            });
        } catch  {
            error("The '".concat(props.value, "' is not a valid cron expression"));
            return props.value;
        }
    }, [
        props.value
    ]);
    return props.value ? /*#__PURE__*/ jsx("span", {
        children: value
    }) : null;
};
const Cron_Cron = connect(Input, mapReadPretty(ReadPretty));
Cron_Cron.ReadPretty = ReadPretty;
const cron_Cron = Cron_Cron;
export { Cron_Cron as Cron, cron_Cron as default };
