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_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../api-client/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      display: flex;\n      gap: 0.5em;\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const useStyles = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css } = param;
    return {
        field: css(_templateObject())
    };
});
function VerificationCode(param) {
    let { targetFieldName = 'phone', actionType, value, onChange } = param;
    const { message } = __WEBPACK_EXTERNAL_MODULE_antd__.App.useApp();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { styles } = useStyles();
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const [count, setCountdown] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(0);
    const timer = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (count <= 0 && timer.current) clearInterval(timer.current);
    }, [
        count
    ]);
    async function onGetCode() {
        if (count > 0) return;
        try {
            const { data: { data } } = await api.resource('verifications').create({
                values: {
                    type: actionType,
                    phone: form.values[targetFieldName]
                }
            });
            message.success(t('Operation succeeded'));
            if (value) onChange('');
            const expiresIn = data.expiresAt ? Math.ceil((Date.parse(data.expiresAt) - Date.now()) / 1000) : 60;
            setCountdown(expiresIn);
            timer.current = setInterval(()=>{
                setCountdown((count)=>count - 1);
            }, 1000);
        } catch (err) {
            console.error(err);
        }
    }
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("fieldset", {
        className: styles.field,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Input, {
                value: value,
                onChange: onChange,
                placeholder: t('Verification code')
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                onClick: onGetCode,
                disabled: count > 0,
                children: count > 0 ? t('Retry after {{count}} seconds', {
                    count
                }) : t('Send code')
            })
        ]
    });
}
export { VerificationCode as default };
