import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import React, { useEffect, useRef, useState } from "react";
import { locales } from "@pisell/utils";
import { Button, Iconfont } from "@pisell/materials";
import "../index.less";
//#region src/plus/saasRegister/components/VertifyEmail.tsx
const COUNTDOWN_NUM = 60;
const VertifyEmail = (props) => {
	const [loading, setLoading] = useState(false);
	const [countdown, setCountdown] = useState(0);
	const countdownRef = useRef(0);
	const timerRef = useRef(null);
	useEffect(() => {
		_onSendEmail();
		return () => {
			timerRef.current && clearInterval(timerRef.current);
			timerRef.current = null;
		};
	}, []);
	const _startCountdown = () => {
		if (timerRef.current) return;
		countdownRef.current = COUNTDOWN_NUM;
		setCountdown(countdownRef.current);
		timerRef.current = setInterval(() => {
			if (countdownRef.current > 0) {
				countdownRef.current = countdownRef.current - 1;
				setCountdown(countdownRef.current);
			} else {
				clearInterval(timerRef.current);
				timerRef.current = null;
			}
		}, 1e3);
	};
	const _onSendEmail = function() {
		var _ref = _asyncToGenerator(function* () {
			if (countdownRef.current > 0 || loading) return;
			setLoading(true);
			try {
				var _props$onSendEmail;
				yield props === null || props === void 0 || (_props$onSendEmail = props.onSendEmail) === null || _props$onSendEmail === void 0 ? void 0 : _props$onSendEmail.call(props);
				_startCountdown();
			} catch (error) {
				console.error("onResend failed:", error);
			} finally {
				setLoading(false);
			}
		});
		return function _onSendEmail() {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-register-form" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-register-form-main" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-register-form-title" }, locales.getText("pisell-saas-register-verify-email-title")), /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-register-form-subtitle" }, locales.getText("pisell-saas-register-verify-email-subtitle")), /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-register-form-email" }, props.email, /* @__PURE__ */ React.createElement(Iconfont, {
		type: "pisell2-edit-05",
		className: "pisell-saas-register-form-email-icon",
		onClick: props.onEditEmail
	}))), /* @__PURE__ */ React.createElement("div", {
		className: "pisell-saas-register-action",
		style: {
			marginTop: "32px",
			textAlign: "center"
		}
	}, locales.getText("pisell-saas-register-resend-text"), /* @__PURE__ */ React.createElement(Button, {
		type: "link",
		onClick: _onSendEmail,
		disabled: countdown > 0 || loading,
		className: "pisell-saas-register-link-button"
	}, locales.getText("pisell-saas-register-resend-button-text"), /* @__PURE__ */ React.createElement("span", { style: { marginLeft: "4px" } }, countdown > 0 && `(${countdown})`))));
};
//#endregion
export { VertifyEmail as default };
