const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_Header = require("./components/Header.js");
const require_EntryEmail = require("./components/EntryEmail.js");
const require_GoLogin = require("./components/GoLogin.js");
const require_VertifyEmail = require("./components/VertifyEmail.js");
const require_VerifyingLink = require("./components/VerifyingLink.js");
const require_types = require("./types.js");
const require_VerifyError = require("./components/VerifyError.js");
const require_EntryInfo = require("./components/EntryInfo.js");
const require_locales = require("./locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/plus/saasRegister/index.tsx
const SaasRegister = (props, ref) => {
	var _context$engine, _step$params7;
	const context = require_useEngineContext.default();
	_pisell_utils.locales.init(require_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] = antd.Form.useForm();
	const [step, setStep] = (0, react.useState)({ key: require_types.SRStepKey.ENTRY_EMAIL });
	const init = (emailToken) => {
		if (emailToken) setStep({
			key: require_types.SRStepKey.VERIFYING_EMAIL,
			params: { emailToken }
		});
	};
	(0, react.useImperativeHandle)(ref, () => ({ init }));
	(0, react.useEffect)(() => {
		var _props$onStepChange;
		(_props$onStepChange = props.onStepChange) === null || _props$onStepChange === void 0 || _props$onStepChange.call(props, step);
	}, [step]);
	(0, react.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 = require_asyncToGenerator._asyncToGenerator(function* (result) {
			if (result) setStep({ key: require_types.SRStepKey.WAIT_VERIFY_EMAIL });
			else setStep({
				key: require_types.SRStepKey.VERIFY_FAILED,
				status: require_types.SRStepStatus.FAILED_EMAIL_REGISTERED
			});
		});
		return function handleVerifyEmail(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const handleEditEmail = () => {
		setStep({ key: require_types.SRStepKey.ENTRY_EMAIL });
	};
	const handleSendEmail = function() {
		var _ref2 = require_asyncToGenerator._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 = require_asyncToGenerator._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) === require_types.SRVerifyCode.SUCCESS) setStep({
					key: require_types.SRStepKey.ENTRY_INFO,
					params: {
						emailToken,
						documentTitle: _pisell_utils.locales.getText("pisell-saas-register-entry-info-document-title"),
						documentDescription: _pisell_utils.locales.getText("pisell-saas-register-entry-info-document-description")
					}
				});
			} catch (error) {
				if ((error === null || error === void 0 ? void 0 : error.code) === require_types.SRVerifyCode.LINK_EXPIRED) setStep({
					key: require_types.SRStepKey.VERIFY_FAILED,
					status: require_types.SRStepStatus.FAILED_LINK_EXPIRED
				});
				else if ((error === null || error === void 0 ? void 0 : error.code) === require_types.SRVerifyCode.EMAIL_REGISTERED) setStep({
					key: require_types.SRStepKey.VERIFY_FAILED,
					status: require_types.SRStepStatus.FAILED_LINK_EMAIL_REGISTERED
				});
				else (0, _pisell_materials.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: require_types.SRStepKey.ENTRY_EMAIL });
	};
	const customHeaderProps = (0, react.useMemo)(() => {
		if (step.key === require_types.SRStepKey.ENTRY_INFO) return {
			showLogo: false,
			showWelcomeMessage: false,
			showTitle: true,
			title: _pisell_utils.locales.getText("pisell-saas-register-entry-info-title"),
			showSubtitle: true,
			subtitle: _pisell_utils.locales.getText("pisell-saas-register-entry-info-subtitle")
		};
		return {};
	}, [step.key]);
	return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-saas-register-main" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-saas-register-container" }, /* @__PURE__ */ react.default.createElement(require_Header.Header, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), customHeaderProps)), /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		preserve: true
	}, step.key === require_types.SRStepKey.ENTRY_EMAIL && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_EntryEmail.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, {
		form,
		handleVerifyEmail
	})), /* @__PURE__ */ react.default.createElement(require_GoLogin.default, props)), step.key === require_types.SRStepKey.WAIT_VERIFY_EMAIL && /* @__PURE__ */ react.default.createElement(require_VertifyEmail.default, {
		email: form.getFieldValue("email"),
		onSendEmail: handleSendEmail,
		onEditEmail: handleEditEmail
	}), step.key === require_types.SRStepKey.VERIFYING_EMAIL && /* @__PURE__ */ react.default.createElement(require_VerifyingLink.default, { onVerifyLink: handleVerifyLink }), step.key === require_types.SRStepKey.VERIFY_FAILED && /* @__PURE__ */ react.default.createElement(require_VerifyError.default, {
		status: step.status,
		onBackToRegister: handleBackToRegister,
		onGoLogin: props === null || props === void 0 ? void 0 : props.onGoLogin
	}), step.key === require_types.SRStepKey.ENTRY_INFO && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_EntryInfo.default, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, {
		form,
		emailToken: (_step$params7 = step.params) === null || _step$params7 === void 0 ? void 0 : _step$params7.emailToken
	})), /* @__PURE__ */ react.default.createElement(require_GoLogin.default, props)))));
};
var saasRegister_default = (0, react.forwardRef)(SaasRegister);
//#endregion
exports.default = saasRegister_default;
