import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import { Header } from "./components/Header.js";
import EntryEmail from "./components/EntryEmail.js";
import GoLogin from "./components/GoLogin.js";
import VertifyEmail from "./components/VertifyEmail.js";
import VerifyingEmailLink from "./components/VerifyingLink.js";
import { SRStepKey, SRStepStatus, SRVerifyCode } from "./types.js";
import VerifyError from "./components/VerifyError.js";
import EntryInfo from "./components/EntryInfo.js";
import locales_default from "./locales.js";
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useState } from "react";
import { Form } from "antd";
import { locales } from "@pisell/utils";
import { PisellToast } from "@pisell/materials";
import "./index.less";
//#region src/plus/saasRegister/index.tsx
const SaasRegister = (props, ref) => {
	var _context$engine, _step$params7;
	const context = useEngineContext();
	locales.init(locales_default, (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || "en");
	const [form] = Form.useForm();
	const [step, setStep] = useState({ key: SRStepKey.ENTRY_EMAIL });
	const init = (emailToken) => {
		if (emailToken) setStep({
			key: SRStepKey.VERIFYING_EMAIL,
			params: { emailToken }
		});
	};
	useImperativeHandle(ref, () => ({ init }));
	useEffect(() => {
		var _props$onStepChange;
		(_props$onStepChange = props.onStepChange) === null || _props$onStepChange === void 0 || _props$onStepChange.call(props, step);
	}, [step]);
	useEffect(() => {
		var _step$params, _step$params3;
		if ((_step$params = step.params) === null || _step$params === void 0 ? void 0 : _step$params.documentTitle) {
			var _step$params2;
			document.title = (_step$params2 = step.params) === null || _step$params2 === void 0 ? void 0 : _step$params2.documentTitle;
		}
		if ((_step$params3 = step.params) === null || _step$params3 === void 0 ? void 0 : _step$params3.documentDescription) {
			const metaDescription = document.querySelector("meta[name=\"description\"]");
			if (metaDescription) {
				var _step$params4;
				metaDescription.setAttribute("content", (_step$params4 = step.params) === null || _step$params4 === void 0 ? void 0 : _step$params4.documentDescription);
			} else {
				var _step$params5;
				const meta = document.createElement("meta");
				meta.name = "description";
				meta.content = (_step$params5 = step.params) === null || _step$params5 === void 0 ? void 0 : _step$params5.documentDescription;
				document.head.appendChild(meta);
			}
		}
	}, [step]);
	const handleVerifyEmail = function() {
		var _ref = _asyncToGenerator(function* (result) {
			if (result) setStep({ key: SRStepKey.WAIT_VERIFY_EMAIL });
			else setStep({
				key: SRStepKey.VERIFY_FAILED,
				status: SRStepStatus.FAILED_EMAIL_REGISTERED
			});
		});
		return function handleVerifyEmail(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const handleEditEmail = () => {
		setStep({ key: SRStepKey.ENTRY_EMAIL });
	};
	const handleSendEmail = function() {
		var _ref2 = _asyncToGenerator(function* () {
			var _props$onSendEmail;
			const email = form.getFieldValue("email");
			yield (_props$onSendEmail = props.onSendEmail) === null || _props$onSendEmail === void 0 ? void 0 : _props$onSendEmail.call(props, email);
		});
		return function handleSendEmail() {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleVerifyLink = function() {
		var _ref3 = _asyncToGenerator(function* () {
			try {
				var _step$params6, _props$onVerifyLink;
				const emailToken = (_step$params6 = step.params) === null || _step$params6 === void 0 ? void 0 : _step$params6.emailToken;
				const result = yield (_props$onVerifyLink = props.onVerifyLink) === null || _props$onVerifyLink === void 0 ? void 0 : _props$onVerifyLink.call(props, emailToken);
				console.log("result>>>>>>>", result);
				if ((result === null || result === void 0 ? void 0 : result.code) === SRVerifyCode.SUCCESS) setStep({
					key: SRStepKey.ENTRY_INFO,
					params: {
						emailToken,
						documentTitle: locales.getText("pisell-saas-register-entry-info-document-title"),
						documentDescription: locales.getText("pisell-saas-register-entry-info-document-description")
					}
				});
			} catch (error) {
				if ((error === null || error === void 0 ? void 0 : error.code) === SRVerifyCode.LINK_EXPIRED) setStep({
					key: SRStepKey.VERIFY_FAILED,
					status: SRStepStatus.FAILED_LINK_EXPIRED
				});
				else if ((error === null || error === void 0 ? void 0 : error.code) === SRVerifyCode.EMAIL_REGISTERED) setStep({
					key: SRStepKey.VERIFY_FAILED,
					status: SRStepStatus.FAILED_LINK_EMAIL_REGISTERED
				});
				else PisellToast({
					content: error === null || error === void 0 ? void 0 : error.message,
					duration: 2
				});
				console.error("handleVerifyLink_failed:", error);
			}
		});
		return function handleVerifyLink() {
			return _ref3.apply(this, arguments);
		};
	}();
	const handleBackToRegister = () => {
		setStep({ key: SRStepKey.ENTRY_EMAIL });
	};
	const customHeaderProps = useMemo(() => {
		if (step.key === SRStepKey.ENTRY_INFO) return {
			showLogo: false,
			showWelcomeMessage: false,
			showTitle: true,
			title: locales.getText("pisell-saas-register-entry-info-title"),
			showSubtitle: true,
			subtitle: locales.getText("pisell-saas-register-entry-info-subtitle")
		};
		return {};
	}, [step.key]);
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-register-main" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-register-container" }, /* @__PURE__ */ React.createElement(Header, _objectSpread2(_objectSpread2({}, props), customHeaderProps)), /* @__PURE__ */ React.createElement(Form, {
		form,
		preserve: true
	}, step.key === SRStepKey.ENTRY_EMAIL && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(EntryEmail, _objectSpread2(_objectSpread2({}, props), {}, {
		form,
		handleVerifyEmail
	})), /* @__PURE__ */ React.createElement(GoLogin, props)), step.key === SRStepKey.WAIT_VERIFY_EMAIL && /* @__PURE__ */ React.createElement(VertifyEmail, {
		email: form.getFieldValue("email"),
		onSendEmail: handleSendEmail,
		onEditEmail: handleEditEmail
	}), step.key === SRStepKey.VERIFYING_EMAIL && /* @__PURE__ */ React.createElement(VerifyingEmailLink, { onVerifyLink: handleVerifyLink }), step.key === SRStepKey.VERIFY_FAILED && /* @__PURE__ */ React.createElement(VerifyError, {
		status: step.status,
		onBackToRegister: handleBackToRegister,
		onGoLogin: props === null || props === void 0 ? void 0 : props.onGoLogin
	}), step.key === SRStepKey.ENTRY_INFO && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(EntryInfo, _objectSpread2(_objectSpread2({}, props), {}, {
		form,
		emailToken: (_step$params7 = step.params) === null || _step$params7 === void 0 ? void 0 : _step$params7.emailToken
	})), /* @__PURE__ */ React.createElement(GoLogin, props)))));
};
var saasRegister_default = forwardRef(SaasRegister);
//#endregion
export { saasRegister_default as default };
