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_locales = require("./locales.js");
const require_Header = require("./shared/Header.js");
const require_usePasswordValidationRules = require("./hooks/usePasswordValidationRules.js");
const require_SetPassword = require("./steps/SetPassword.js");
const require_PhoneInput = require("./components/PhoneInput.js");
const require_phoneValidation = require("./components/phoneValidation.js");
const require_LegalTerms = require("./components/LegalTerms.js");
const require_OAuthButtonGroup = require("./components/OAuthButtonGroup.js");
const require_registerFlow = require("./shared/registerFlow.js");
const require_useRegisterFlow = require("./hooks/useRegisterFlow.js");
const require_useRegistrationMethods = require("./hooks/useRegistrationMethods.js");
const require_WaitEmailLink = require("./steps/WaitEmailLink.js");
const require_EmailAlreadyRegistered = require("./steps/EmailAlreadyRegistered.js");
const require_CodeInput = require("./steps/CodeInput.js");
const require_VerificationSuccess = require("./steps/VerificationSuccess.js");
const require_VerifyingEmailLink = require("./steps/VerifyingEmailLink.js");
const require_EmailLinkExpired = require("./steps/EmailLinkExpired.js");
const require_EmailLinkAlreadyUsed = require("./steps/EmailLinkAlreadyUsed.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");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let ahooks = require("ahooks");
require("./index.less");
require("./steps/steps.less");
//#region src/pro/Login2.0/Register2.tsx
/**
* 注册 2.0 组件
* 支持邮箱、手机、三方注册
* 组件宽度固定 360px
*/
const Register2 = ({ config, visible = true, onClose, className, style, onRegister, onOAuthLogin, onSendVerificationCode, onEmailLinkExpired, onGoToLogin, onError }) => {
	var _context$appHelper, _utils$getStore, _utils$getStore$getSt, _config$ui, _config$emailLinkVeri3, _config$ui4, _config$ui5, _config$ui6, _config$ui7, _config$ui8, _config$ui9;
	const [form] = antd.Form.useForm();
	const [loading, setLoading] = (0, react.useState)(false);
	const [countdown, setCountdown] = (0, react.useState)(0);
	const [countries, setCountries] = (0, react.useState)([]);
	const [codeError, setCodeError] = (0, react.useState)("");
	const pisellOS = require_useEngineContext.usePisellOS();
	const registerAndLogin = pisellOS === null || pisellOS === void 0 ? void 0 : pisellOS.getModule("registerAndLogin");
	const context = require_useEngineContext.default();
	const utils = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const shopId = ((_utils$getStore = utils.getStore) === null || _utils$getStore === void 0 || (_utils$getStore = _utils$getStore.call(utils)) === null || _utils$getStore === void 0 || (_utils$getStore$getSt = _utils$getStore.getState) === null || _utils$getStore$getSt === void 0 || (_utils$getStore$getSt = _utils$getStore$getSt.call(_utils$getStore)) === null || _utils$getStore$getSt === void 0 || (_utils$getStore$getSt = _utils$getStore$getSt.global) === null || _utils$getStore$getSt === void 0 || (_utils$getStore$getSt = _utils$getStore$getSt.shop) === null || _utils$getStore$getSt === void 0 ? void 0 : _utils$getStore$getSt.id) || 9;
	const [emailLinkVerifyStatus, setEmailLinkVerifyStatus] = (0, react.useState)(null);
	const [verifiedEmail, setVerifiedEmail] = (0, react.useState)("");
	const normalizedRegistrationMethods = (0, react.useMemo)(() => {
		var _config$email, _config$phone, _config$oauth, _config$oauth2, _config$oauth3, _config$oauth4, _config$oauth5, _config$oauth6;
		if (config.registrationMethods && config.registrationMethods.length > 0) return config.registrationMethods;
		const methods = [];
		if ((_config$email = config.email) === null || _config$email === void 0 ? void 0 : _config$email.enable_email) methods.push({
			type: "email",
			verificationMethod: config.email.registration_verification ? config.email.registration_verification : "verification_code"
		});
		if ((_config$phone = config.phone) === null || _config$phone === void 0 ? void 0 : _config$phone.enable_phone_number) methods.push({
			type: "phone",
			verificationMethod: "verification_code"
		});
		if (((_config$oauth = config.oauth) === null || _config$oauth === void 0 || (_config$oauth = _config$oauth.google) === null || _config$oauth === void 0 ? void 0 : _config$oauth.enabled) && ((_config$oauth2 = config.oauth) === null || _config$oauth2 === void 0 || (_config$oauth2 = _config$oauth2.google) === null || _config$oauth2 === void 0 ? void 0 : _config$oauth2.client_id)) methods.push({
			type: "google",
			clientId: config.oauth.google.client_id
		});
		if (((_config$oauth3 = config.oauth) === null || _config$oauth3 === void 0 || (_config$oauth3 = _config$oauth3.facebook) === null || _config$oauth3 === void 0 ? void 0 : _config$oauth3.enabled) && ((_config$oauth4 = config.oauth) === null || _config$oauth4 === void 0 || (_config$oauth4 = _config$oauth4.facebook) === null || _config$oauth4 === void 0 ? void 0 : _config$oauth4.app_id)) methods.push({
			type: "facebook",
			appId: config.oauth.facebook.app_id
		});
		if (((_config$oauth5 = config.oauth) === null || _config$oauth5 === void 0 || (_config$oauth5 = _config$oauth5.apple) === null || _config$oauth5 === void 0 ? void 0 : _config$oauth5.enabled) && ((_config$oauth6 = config.oauth) === null || _config$oauth6 === void 0 || (_config$oauth6 = _config$oauth6.apple) === null || _config$oauth6 === void 0 ? void 0 : _config$oauth6.client_id)) methods.push({
			type: "apple",
			clientId: config.oauth.apple.client_id
		});
		return methods;
	}, [
		config.registrationMethods,
		config.email,
		config.phone,
		config.oauth
	]);
	const getDefaultAuthMethod = () => {
		var _config$phone2, _config$email2, _config$email3, _config$phone3;
		if (normalizedRegistrationMethods.length > 0) {
			if (config.defaultAuthMethod) {
				if (normalizedRegistrationMethods.find((m) => m.type === config.defaultAuthMethod)) return config.defaultAuthMethod;
			}
			return normalizedRegistrationMethods[0].type;
		}
		if (config.defaultAuthMethod) return config.defaultAuthMethod;
		if (((_config$phone2 = config.phone) === null || _config$phone2 === void 0 ? void 0 : _config$phone2.enable_phone_number) && !((_config$email2 = config.email) === null || _config$email2 === void 0 ? void 0 : _config$email2.enable_email)) return "phone";
		if (((_config$email3 = config.email) === null || _config$email3 === void 0 ? void 0 : _config$email3.enable_email) && !((_config$phone3 = config.phone) === null || _config$phone3 === void 0 ? void 0 : _config$phone3.enable_phone_number)) return "email";
		return "email";
	};
	const [currentAuthMethod, setCurrentAuthMethod] = (0, react.useState)(() => getDefaultAuthMethod());
	const getRegistrationMethodConfig = (0, react.useCallback)((type) => {
		return normalizedRegistrationMethods.find((method) => method.type === type);
	}, [normalizedRegistrationMethods]);
	const getCurrentVerificationMethod = (0, react.useCallback)(() => {
		const methodConfig = getRegistrationMethodConfig(currentAuthMethod);
		if (methodConfig && "verificationMethod" in methodConfig) return methodConfig.verificationMethod;
		if (currentAuthMethod === "email") {
			var _config$email4;
			return ((_config$email4 = config.email) === null || _config$email4 === void 0 ? void 0 : _config$email4.registration_verification) || "verification_code";
		} else if (currentAuthMethod === "phone") return "verification_code";
		return "verification_code";
	}, [
		currentAuthMethod,
		getRegistrationMethodConfig,
		config.email,
		config.phone
	]);
	const passwordValidationRules = require_usePasswordValidationRules.default((_config$ui = config.ui) === null || _config$ui === void 0 ? void 0 : _config$ui.passwordRules, {
		translate: _pisell_utils.locales.getText,
		fallbackMinLength: 6,
		messageKeys: {
			required: "pisell-register2-password-required",
			minLength: "pisell-register2-password-min-length",
			requireUppercase: "pisell-set-password-require-uppercase",
			requireLowercase: "pisell-set-password-require-lowercase",
			requireNumber: "pisell-set-password-require-number",
			requireSpecialCharacters: "pisell-set-password-require-special",
			summary: "pisell-set-password-must-contain"
		}
	});
	const isRegistrationMethodEnabled = (0, react.useCallback)((type) => {
		return normalizedRegistrationMethods.some((method) => method.type === type);
	}, [normalizedRegistrationMethods]);
	const { shouldShowDivider, hasEmailAndPhone } = require_useRegistrationMethods.useRegistrationMethods(isRegistrationMethodEnabled);
	const handleStepChange = (0, react.useCallback)(() => {}, []);
	const handleComplete = (0, react.useCallback)((data) => {
		var _data$extraData;
		const apiResponse = (_data$extraData = data.extraData) === null || _data$extraData === void 0 ? void 0 : _data$extraData.apiResponse;
		onRegister === null || onRegister === void 0 || onRegister(apiResponse || (data === null || data === void 0 ? void 0 : data.data), data.accountType, {
			account: data.account,
			password: data.password || ""
		}, config.channel);
	}, [onRegister, config.channel]);
	const { flowData, currentStepDef, goToStep, updateFlowData, resetFlow, handleError } = require_useRegisterFlow.useRegisterFlow({
		accountType: currentAuthMethod,
		emailConfig: config.email,
		phoneConfig: config.phone,
		initialAccount: "",
		onStepChange: handleStepChange,
		onComplete: handleComplete
	});
	(0, react.useEffect)(() => {
		(function() {
			var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
				try {
					if (registerAndLogin === null || registerAndLogin === void 0 ? void 0 : registerAndLogin.getCountries) setCountries((yield registerAndLogin.getCountries()) || []);
				} catch (error) {
					console.error("Failed to fetch countries:", error);
				}
			});
			return function fetchCountries() {
				return _ref.apply(this, arguments);
			};
		})()();
	}, [registerAndLogin]);
	(0, react.useEffect)(() => {
		(function() {
			var _ref2 = require_asyncToGenerator._asyncToGenerator(function* () {
				var _config$emailLinkVeri, _config$emailLinkVeri2;
				if (!((_config$emailLinkVeri = config.emailLinkVerification) === null || _config$emailLinkVeri === void 0 ? void 0 : _config$emailLinkVeri.enabled) || !((_config$emailLinkVeri2 = config.emailLinkVerification) === null || _config$emailLinkVeri2 === void 0 ? void 0 : _config$emailLinkVeri2.code)) return;
				setEmailLinkVerifyStatus("verifying");
				try {
					const result = yield registerAndLogin.checkEmailLinkCode({ code: config.emailLinkVerification.code });
					if (result.code === 200 && result.data.valid) {
						updateFlowData({
							emailLinkCode: config.emailLinkVerification.code,
							account: result.data.email || "",
							accountType: "email",
							verificationMethod: require_registerFlow.VerificationMethod.EMAIL_LINK
						});
						setVerifiedEmail(result.data.email);
						setEmailLinkVerifyStatus("success");
						goToStep(require_registerFlow.RegisterStep.SET_PASSWORD);
					} else if (result.code === 50011) setEmailLinkVerifyStatus("expired");
					else if (!result.data.valid) setEmailLinkVerifyStatus("already_used");
					else {
						setEmailLinkVerifyStatus(null);
						antd.message.error(result.message || "验证失败，请稍后重试");
					}
				} catch (error) {
					console.error("Email link verification failed:", error);
					antd.message.error("验证失败，请稍后重试");
					setEmailLinkVerifyStatus(null);
				}
			});
			return function verifyEmailLink() {
				return _ref2.apply(this, arguments);
			};
		})()();
	}, [config.emailLinkVerification, registerAndLogin]);
	(0, react.useEffect)(() => {
		if (!visible) {
			form.setFieldsValue({ password: "" });
			form.setFieldsValue({ confirmPassword: "" });
		}
	}, [visible, form]);
	(0, react.useEffect)(() => {
		const newAuthMethod = getDefaultAuthMethod();
		if (newAuthMethod !== currentAuthMethod) {
			setCurrentAuthMethod(newAuthMethod);
			form.resetFields(["account"]);
		}
	}, [normalizedRegistrationMethods, config.defaultAuthMethod]);
	(0, react.useEffect)(() => {
		var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4;
		_pisell_utils.locales.init(require_locales.default, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getApp) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.locales) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getLocale) === null || _context$appHelper$ut3 === void 0 ? void 0 : _context$appHelper$ut3.call(_context$appHelper$ut2)) || ((_context$appHelper$ut4 = context.appHelper.utils) === null || _context$appHelper$ut4 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut4.storage) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.get("umi_locale")) || "en");
	}, []);
	(0, react.useEffect)(() => {
		if (countdown > 0) {
			const timer = setTimeout(() => {
				setCountdown(countdown - 1);
			}, 1e3);
			return () => clearTimeout(timer);
		}
	}, [countdown]);
	const handleSendCode = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
			try {
				const account = (yield form.validateFields(["account"])).account;
				setLoading(true);
				const type = currentAuthMethod === "email" ? "email" : "phone";
				let res;
				if (currentAuthMethod === "email") if (getCurrentVerificationMethod() === "verification_link") res = yield registerAndLogin.sendEmailRegisterLink(account);
				else res = yield registerAndLogin.sendEmailVerificationCode({
					type: "email",
					target: account,
					purpose: "register"
				});
				else res = yield registerAndLogin.sendSmsRegisterCode(account);
				const accountForCallback = currentAuthMethod === "phone" && typeof account === "object" ? `+${account.country_calling_code}${account.phone}` : account;
				yield onSendVerificationCode === null || onSendVerificationCode === void 0 ? void 0 : onSendVerificationCode(accountForCallback, type, config.channel);
				if (res.code === 200) {
					updateFlowData({
						account,
						accountType: currentAuthMethod
					});
					const verificationMethod = getCurrentVerificationMethod();
					if (verificationMethod === "verification_link") {
						goToStep(require_registerFlow.RegisterStep.WAIT_EMAIL_LINK, {
							account,
							accountType: currentAuthMethod,
							verificationMethod: require_registerFlow.VerificationMethod.EMAIL_LINK
						});
						setCountdown(60);
					} else if (verificationMethod === "verification_code") {
						goToStep(require_registerFlow.RegisterStep.INPUT_VERIFICATION_CODE, {
							account,
							accountType: currentAuthMethod
						});
						setCountdown(60);
						antd.message.success(_pisell_utils.locales.getText("pisell-login2-code-sent"));
					}
				} else if (res.code === 5002) goToStep(require_registerFlow.RegisterStep.EMAIL_ALREADY_REGISTERED, {
					account,
					accountType: currentAuthMethod
				});
				else antd.message.error(res.message);
			} catch (error) {
				if (error.errorFields) return;
				handleError("SEND_CODE_FAILED", error.message || "Failed to send verification code");
				antd.message.error(error === null || error === void 0 ? void 0 : error.message);
			} finally {
				setLoading(false);
			}
		});
		return function handleSendCode() {
			return _ref3.apply(this, arguments);
		};
	}();
	const handleRegister = function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* () {
			try {
				setLoading(true);
				const values = yield form.validateFields();
				const registerData = {
					account: values.account,
					password: values.password
				};
				yield onRegister === null || onRegister === void 0 ? void 0 : onRegister(registerData, currentAuthMethod, config.channel);
				antd.message.success(_pisell_utils.locales.getText("pisell-register2-register-success"));
			} catch (error) {
				if (error.errorFields) return;
				antd.message.error((error === null || error === void 0 ? void 0 : error.message) || "Registration failed");
			} finally {
				setLoading(false);
			}
		});
		return function handleRegister() {
			return _ref4.apply(this, arguments);
		};
	}();
	const handleFormSubmit = (e) => {
		e === null || e === void 0 || e.preventDefault();
		if (isEmailNoVerification) handleRegister();
		else if (needsVerification) handleSendCode();
		else handleRegister();
	};
	const { run: handleOAuthClick } = (0, ahooks.useDebounceFn)(function() {
		var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (provider) {
			try {
				setLoading(true);
				if (provider === "facebook") {
					var _config$registrationM;
					const fbConfig = config === null || config === void 0 || (_config$registrationM = config.registrationMethods) === null || _config$registrationM === void 0 ? void 0 : _config$registrationM.find((n) => n.type === "facebook");
					const token = fbConfig === null || fbConfig === void 0 ? void 0 : fbConfig.appId;
					if (!token) throw new Error("Facebook app_id not configured");
					const res = yield registerAndLogin.loginWithFacebook(token);
					if (res.data) yield onRegister === null || onRegister === void 0 ? void 0 : onRegister(res.data, "facebook", {
						account: "",
						password: ""
					}, config.channel);
				} else if (provider === "apple") {
					var _config$registrationM2;
					const appleConfig = config === null || config === void 0 || (_config$registrationM2 = config.registrationMethods) === null || _config$registrationM2 === void 0 ? void 0 : _config$registrationM2.find((n) => n.type === "apple");
					const token = appleConfig === null || appleConfig === void 0 ? void 0 : appleConfig.clientId;
					if (token) {
						const res = yield registerAndLogin.loginWithApple(token);
						if (res.data) yield onRegister === null || onRegister === void 0 ? void 0 : onRegister(res.data, "apple", {
							account: "",
							password: ""
						}, config.channel);
					} else throw new Error("Apple client_id not configured");
				} else if (provider === "google") {
					var _config$registrationM3;
					const googleConfig = config === null || config === void 0 || (_config$registrationM3 = config.registrationMethods) === null || _config$registrationM3 === void 0 ? void 0 : _config$registrationM3.find((n) => n.type === "google");
					const token = googleConfig === null || googleConfig === void 0 ? void 0 : googleConfig.clientId;
					if (token) {
						const res = yield registerAndLogin.loginWithGoogle(token);
						if (res.data) yield onRegister === null || onRegister === void 0 ? void 0 : onRegister(res.data, "google", {
							account: "",
							password: ""
						}, config.channel);
					} else throw new Error("Google client_id not configured");
				}
				yield onOAuthLogin === null || onOAuthLogin === void 0 ? void 0 : onOAuthLogin(provider, config.channel);
			} catch (error) {
				if (error.message === "facebook_login_cancel") return antd.message.error(_pisell_utils.locales.getText("pisell-register2-facebook-login-canceled"));
				console.error(`${provider} login error:`, error);
				antd.message.error((error === null || error === void 0 ? void 0 : error.message) || `${provider} login failed`);
				onError === null || onError === void 0 || onError(error, provider);
			} finally {
				setLoading(false);
			}
		});
		return function handleOAuthClickInternal(_x) {
			return _ref5.apply(this, arguments);
		};
	}(), {
		wait: 200,
		leading: true,
		trailing: false
	});
	const oauthProviders = (0, react.useMemo)(() => {
		const providers = [];
		if (isRegistrationMethodEnabled("google")) providers.push({
			key: "google",
			label: _pisell_utils.locales.getText("pisell-register2-continue-with-google"),
			icon: /* @__PURE__ */ react.default.createElement("span", { className: "oauth-icon" }, "G"),
			onClick: () => handleOAuthClick("google"),
			className: "oauth-button google"
		});
		if (isRegistrationMethodEnabled("facebook")) providers.push({
			key: "facebook",
			label: _pisell_utils.locales.getText("pisell-register2-continue-with-facebook"),
			icon: /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
				type: "pisell2-facebook",
				className: "oauth-icon"
			}),
			onClick: () => handleOAuthClick("facebook"),
			className: "oauth-button facebook"
		});
		if (isRegistrationMethodEnabled("apple")) providers.push({
			key: "apple",
			label: _pisell_utils.locales.getText("pisell-register2-continue-with-apple"),
			icon: /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
				type: "pisell2-apple",
				className: "oauth-icon"
			}),
			onClick: () => handleOAuthClick("apple"),
			className: "oauth-button apple"
		});
		return providers;
	}, [handleOAuthClick, isRegistrationMethodEnabled]);
	const handleResendCode = function() {
		var _ref6 = require_asyncToGenerator._asyncToGenerator(function* () {
			if (countdown > 0) return;
			try {
				setLoading(true);
				const verificationMethod = getCurrentVerificationMethod();
				let res;
				if (currentAuthMethod === "email") if (verificationMethod === "verification_link") res = yield registerAndLogin.resendEmailRegisterLink();
				else res = yield registerAndLogin.sendEmailVerificationCode({
					type: "email",
					target: flowData.account,
					purpose: "register"
				});
				else res = yield registerAndLogin.sendSmsRegisterCode(flowData.account);
				if (res.code === 200) {
					setCountdown(60);
					let successMessage = "";
					if (currentAuthMethod === "email") if (verificationMethod === "verification_link") successMessage = _pisell_utils.locales.getText("pisell-register2-email-link-resent");
					else successMessage = _pisell_utils.locales.getText("pisell-register2-email-code-resent");
					else successMessage = _pisell_utils.locales.getText("pisell-register2-phone-code-resent");
					antd.message.success(successMessage);
				} else {
					let errorMessage = "";
					if (currentAuthMethod === "email") if (verificationMethod === "verification_link") errorMessage = _pisell_utils.locales.getText("pisell-register2-email-link-resend-failed");
					else errorMessage = _pisell_utils.locales.getText("pisell-register2-email-code-resend-failed");
					else errorMessage = _pisell_utils.locales.getText("pisell-register2-phone-code-resend-failed");
					antd.message.error(res.message || errorMessage);
				}
			} catch (error) {
				let errorMessage = "";
				const verificationMethod = getCurrentVerificationMethod();
				if (currentAuthMethod === "email") if (verificationMethod === "verification_link") errorMessage = _pisell_utils.locales.getText("pisell-register2-email-link-resend-failed");
				else errorMessage = _pisell_utils.locales.getText("pisell-register2-email-code-resend-failed");
				else errorMessage = _pisell_utils.locales.getText("pisell-register2-phone-code-resend-failed");
				antd.message.error(errorMessage);
			} finally {
				setLoading(false);
			}
		});
		return function handleResendCode() {
			return _ref6.apply(this, arguments);
		};
	}();
	const handleVerifyCode = function() {
		var _ref7 = require_asyncToGenerator._asyncToGenerator(function* (code) {
			try {
				setLoading(true);
				setCodeError("");
				const type = currentAuthMethod === "email" ? "email" : "phone";
				let res;
				if (type === "email") res = yield registerAndLogin.checkEmailCode({
					email: flowData.account,
					shop_id: shopId,
					code,
					action: "customer_registration_code"
				});
				else {
					const phoneData = flowData.account;
					res = yield registerAndLogin.checkMobileCode({
						phone: phoneData.phone,
						country_calling_code: phoneData.country_calling_code,
						code,
						action: "register"
					});
				}
				if (res.code === 200 && res.data.valid) {
					updateFlowData({ verificationCode: code });
					goToStep(require_registerFlow.RegisterStep.VERIFICATION_SUCCESS);
				} else setCodeError((res === null || res === void 0 ? void 0 : res.message) || _pisell_utils.locales.getText("pisell-register2-invalid-verification-code"));
			} catch (error) {
				console.error(error);
				setCodeError((error === null || error === void 0 ? void 0 : error.message) || _pisell_utils.locales.getText("pisell-register2-verification-failed"));
			} finally {
				setLoading(false);
			}
		});
		return function handleVerifyCode(_x2) {
			return _ref7.apply(this, arguments);
		};
	}();
	const handleSetPassword = function() {
		var _ref8 = require_asyncToGenerator._asyncToGenerator(function* (password, confirmPassword) {
			try {
				setLoading(true);
				let res;
				if (flowData.emailLinkCode) res = yield registerAndLogin.verifyEmailRegistrationLink({
					code: flowData.emailLinkCode,
					password
				});
				else if (currentAuthMethod === "email") res = yield registerAndLogin.emailCodeRegister({
					email: flowData.account,
					code: flowData.verificationCode,
					password
				});
				else {
					const phoneData = flowData.account;
					res = yield registerAndLogin.phoneCodeRegister({
						phone: phoneData.phone,
						code: flowData.verificationCode,
						password,
						country_calling_code: phoneData.country_calling_code
					});
				}
				if (res.code === 200) {
					updateFlowData({
						password,
						extraData: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, flowData.extraData), {}, { apiResponse: res.data })
					});
					goToStep(require_registerFlow.RegisterStep.COMPLETE);
					antd.message.success(_pisell_utils.locales.getText("pisell-register2-register-success"));
				} else {
					const error = new Error(res.message || "Registration failed");
					onError === null || onError === void 0 || onError(error, currentAuthMethod);
					antd.message.error(res.message || _pisell_utils.locales.getText("pisell-register2-registration-failed"));
				}
			} catch (error) {
				antd.message.error(error.message, _pisell_utils.locales.getText("pisell-register2-failed-to-complete"));
				console.error(error);
			} finally {
				setLoading(false);
			}
		});
		return function handleSetPassword(_x3, _x4) {
			return _ref8.apply(this, arguments);
		};
	}();
	const handleBackToInitialForm = (0, react.useCallback)(() => {
		setCodeError("");
		goToStep(require_registerFlow.RegisterStep.INITIAL_FORM, { verificationMethod: flowData.verificationMethod });
	}, [goToStep, flowData]);
	const handleEditAccount = () => {
		if (flowData.account) form.setFieldsValue({ account: flowData.account });
		handleBackToInitialForm();
	};
	const handleGoToLogin = () => {
		if (onGoToLogin) onGoToLogin();
	};
	const handleSwitchAuthMethod = (method) => {
		setCurrentAuthMethod(method);
		form.resetFields(["account"]);
		resetFlow();
	};
	const handleBackToRegistration = () => {
		setEmailLinkVerifyStatus(null);
		setVerifiedEmail("");
		form.resetFields();
		resetFlow();
		onEmailLinkExpired === null || onEmailLinkExpired === void 0 || onEmailLinkExpired();
	};
	const renderStepPage = () => {
		switch (flowData.currentStep) {
			case require_registerFlow.RegisterStep.WAIT_EMAIL_LINK: return /* @__PURE__ */ react.default.createElement(require_WaitEmailLink.default, {
				flowData,
				onResend: handleResendCode,
				onBack: handleBackToInitialForm,
				onEditEmail: handleEditAccount,
				countdown,
				verificationMethod: flowData.verificationMethod
			});
			case require_registerFlow.RegisterStep.EMAIL_ALREADY_REGISTERED: return /* @__PURE__ */ react.default.createElement(require_EmailAlreadyRegistered.default, {
				flowData,
				onGoToLogin: handleGoToLogin,
				onBack: handleBackToInitialForm
			});
			case require_registerFlow.RegisterStep.INPUT_VERIFICATION_CODE: return /* @__PURE__ */ react.default.createElement(require_CodeInput.default, {
				flowData,
				codeLength: 4,
				onSubmit: handleVerifyCode,
				onResend: handleResendCode,
				onBack: handleBackToInitialForm,
				countdown,
				loading,
				error: codeError,
				onClearError: () => setCodeError("")
			});
			case require_registerFlow.RegisterStep.VERIFICATION_SUCCESS: return /* @__PURE__ */ react.default.createElement(require_VerificationSuccess.default, { flowData });
			case require_registerFlow.RegisterStep.SET_PASSWORD:
				var _config$ui2;
				return /* @__PURE__ */ react.default.createElement(require_SetPassword.default, {
					flowData,
					onSubmit: handleSetPassword,
					onBack: () => goToStep(require_registerFlow.RegisterStep.INPUT_VERIFICATION_CODE),
					loading,
					passwordRules: (_config$ui2 = config.ui) === null || _config$ui2 === void 0 ? void 0 : _config$ui2.passwordRules,
					scenario: "create"
				});
			case require_registerFlow.RegisterStep.COMPLETE: return /* @__PURE__ */ react.default.createElement("div", { className: "register-step-container" }, /* @__PURE__ */ react.default.createElement("div", { className: "register-step-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "success-icon-wrapper" }, /* @__PURE__ */ react.default.createElement("svg", {
				width: "64",
				height: "64",
				viewBox: "0 0 64 64",
				fill: "none"
			}, /* @__PURE__ */ react.default.createElement("circle", {
				cx: "32",
				cy: "32",
				r: "32",
				fill: "#10B981",
				fillOpacity: "0.1"
			}), /* @__PURE__ */ react.default.createElement("path", {
				d: "M22 32L28 38L42 24",
				stroke: "#10B981",
				strokeWidth: "3",
				strokeLinecap: "round",
				strokeLinejoin: "round"
			}))), /* @__PURE__ */ react.default.createElement("h2", { className: "register-step-title" }, _pisell_utils.locales.getText("pisell-register2-complete-title")), /* @__PURE__ */ react.default.createElement("p", { className: "register-step-desc" }, _pisell_utils.locales.getText("pisell-register2-redirecting"))));
			default: return null;
		}
	};
	const handleSetPasswordSubmit = function() {
		var _ref9 = require_asyncToGenerator._asyncToGenerator(function* (password, confirmPassword) {
			try {
				setLoading(true);
				const res = yield registerAndLogin.verifyEmailRegistrationLink({
					code: config.emailLinkVerification.code,
					password
				});
				if (res.code === 200) {
					antd.message.success(_pisell_utils.locales.getText("pisell-register2-register-success"));
					yield onRegister === null || onRegister === void 0 ? void 0 : onRegister(res.data, "email", {
						account: verifiedEmail,
						password
					}, config.channel);
				} else antd.message.error(res.message || _pisell_utils.locales.getText("pisell-register2-registration-failed"));
			} catch (error) {
				antd.message.error((error === null || error === void 0 ? void 0 : error.message) || _pisell_utils.locales.getText("pisell-register2-failed-to-complete"));
			} finally {
				setLoading(false);
			}
		});
		return function handleSetPasswordSubmit(_x5, _x6) {
			return _ref9.apply(this, arguments);
		};
	}();
	if (!visible) return null;
	const { ui } = config;
	if ((_config$emailLinkVeri3 = config.emailLinkVerification) === null || _config$emailLinkVeri3 === void 0 ? void 0 : _config$emailLinkVeri3.enabled) {
		if (emailLinkVerifyStatus === "verifying") return /* @__PURE__ */ react.default.createElement("div", {
			className: (0, classnames.default)("login2-container", "register-flow-container", className),
			style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style), {}, { "--theme-color": (ui === null || ui === void 0 ? void 0 : ui.themeColor) || "#7F56D9" })
		}, /* @__PURE__ */ react.default.createElement(require_VerifyingEmailLink.default, null));
		if (emailLinkVerifyStatus === "expired") return /* @__PURE__ */ react.default.createElement("div", {
			className: (0, classnames.default)("login2-container", "register-flow-container", className),
			style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style), {}, { "--theme-color": (ui === null || ui === void 0 ? void 0 : ui.themeColor) || "#7F56D9" })
		}, /* @__PURE__ */ react.default.createElement(require_EmailLinkExpired.default, {
			onBackToRegistration: handleBackToRegistration,
			onGoToLogin
		}));
		if (emailLinkVerifyStatus === "already_used") return /* @__PURE__ */ react.default.createElement("div", {
			className: (0, classnames.default)("login2-container", "register-flow-container", className),
			style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style), {}, { "--theme-color": (ui === null || ui === void 0 ? void 0 : ui.themeColor) || "#7F56D9" })
		}, /* @__PURE__ */ react.default.createElement(require_EmailLinkAlreadyUsed.default, { onGoToLogin }));
		if (emailLinkVerifyStatus === "success") {
			var _config$ui3;
			return /* @__PURE__ */ react.default.createElement("div", {
				className: (0, classnames.default)("login2-container", "register-flow-container", className),
				style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style), {}, { "--theme-color": (ui === null || ui === void 0 ? void 0 : ui.themeColor) || "#7F56D9" })
			}, /* @__PURE__ */ react.default.createElement(require_SetPassword.default, {
				flowData: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, flowData), {}, { account: verifiedEmail }),
				onSubmit: handleSetPasswordSubmit,
				loading,
				passwordRules: (_config$ui3 = config.ui) === null || _config$ui3 === void 0 ? void 0 : _config$ui3.passwordRules,
				scenario: "create"
			}));
		}
	}
	const isEmailVerificationLink = currentAuthMethod === "email" && getCurrentVerificationMethod() === "verification_link";
	const isEmailVerificationCode = currentAuthMethod === "email" && getCurrentVerificationMethod() === "verification_code";
	const isEmailNoVerification = currentAuthMethod === "email" && getCurrentVerificationMethod() === "none";
	const isPhoneVerificationCode = currentAuthMethod === "phone" && getCurrentVerificationMethod() === "verification_code";
	const needsVerification = isEmailVerificationLink || isEmailVerificationCode || isPhoneVerificationCode;
	if (flowData.currentStep !== require_registerFlow.RegisterStep.INITIAL_FORM) {
		const showHeader = flowData.currentStep !== require_registerFlow.RegisterStep.INPUT_VERIFICATION_CODE && flowData.currentStep !== require_registerFlow.RegisterStep.WAIT_EMAIL_LINK && flowData.currentStep !== require_registerFlow.RegisterStep.SET_PASSWORD && flowData.currentStep !== require_registerFlow.RegisterStep.EMAIL_ALREADY_REGISTERED;
		return /* @__PURE__ */ react.default.createElement("div", {
			className: (0, classnames.default)("login2-container", "register-flow-container", className),
			style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style), {}, { "--theme-color": (ui === null || ui === void 0 ? void 0 : ui.themeColor) || "#7F56D9" })
		}, showHeader && /* @__PURE__ */ react.default.createElement(require_Header.default, { ui }), renderStepPage());
	}
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("login2-container", "register2-container", className),
		style: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style), {}, { "--theme-color": (ui === null || ui === void 0 ? void 0 : ui.themeColor) || "#7F56D9" })
	}, /* @__PURE__ */ react.default.createElement(require_Header.default, { ui }), /* @__PURE__ */ react.default.createElement("div", { className: "login2-content" }, (isRegistrationMethodEnabled("email") || isRegistrationMethodEnabled("phone")) && /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		layout: "vertical",
		className: "login2-form",
		size: "large"
	}, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: currentAuthMethod === "email" ? ((_config$ui4 = config.ui) === null || _config$ui4 === void 0 || (_config$ui4 = _config$ui4.formLabels) === null || _config$ui4 === void 0 || (_config$ui4 = _config$ui4.email) === null || _config$ui4 === void 0 ? void 0 : _config$ui4.show) !== false ? ((_config$ui5 = config.ui) === null || _config$ui5 === void 0 || (_config$ui5 = _config$ui5.formLabels) === null || _config$ui5 === void 0 || (_config$ui5 = _config$ui5.email) === null || _config$ui5 === void 0 ? void 0 : _config$ui5.text) || _pisell_utils.locales.getText("pisell-login2-email-label") : void 0 : ((_config$ui6 = config.ui) === null || _config$ui6 === void 0 || (_config$ui6 = _config$ui6.formLabels) === null || _config$ui6 === void 0 || (_config$ui6 = _config$ui6.phone) === null || _config$ui6 === void 0 ? void 0 : _config$ui6.show) !== false ? ((_config$ui7 = config.ui) === null || _config$ui7 === void 0 || (_config$ui7 = _config$ui7.formLabels) === null || _config$ui7 === void 0 || (_config$ui7 = _config$ui7.phone) === null || _config$ui7 === void 0 ? void 0 : _config$ui7.text) || _pisell_utils.locales.getText("pisell-login2-phone-label") : void 0,
		name: "account",
		rules: currentAuthMethod === "email" ? [{
			required: true,
			message: _pisell_utils.locales.getText("pisell-register2-email-required")
		}, {
			type: "email",
			message: _pisell_utils.locales.getText("pisell-register2-email-invalid")
		}] : require_phoneValidation.createPhoneValidationRules(_pisell_utils.locales.getText, true),
		validateTrigger: ["onBlur", "onSubmit"],
		className: currentAuthMethod === "phone" ? "phone-input-form-item" : ""
	}, currentAuthMethod === "phone" ? /* @__PURE__ */ react.default.createElement(require_PhoneInput.default, {
		size: "large",
		placeholder: _pisell_utils.locales.getText("pisell-register2-phone-placeholder"),
		className: "login2-phone-input",
		countries,
		valueFormat: "object",
		onKeyDown: (e) => {
			if (e.key === "Enter" && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
				e.preventDefault();
				handleFormSubmit(e);
			}
		}
	}) : /* @__PURE__ */ react.default.createElement(_pisell_materials.Input, {
		size: "large",
		placeholder: _pisell_utils.locales.getText("pisell-register2-email-placeholder"),
		className: "login2-email-input",
		autoComplete: "email",
		onKeyDown: (e) => {
			if (e.key === "Enter" && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
				e.preventDefault();
				handleFormSubmit(e);
			}
		}
	})), (!needsVerification || isEmailNoVerification) && /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: ((_config$ui8 = config.ui) === null || _config$ui8 === void 0 || (_config$ui8 = _config$ui8.formLabels) === null || _config$ui8 === void 0 || (_config$ui8 = _config$ui8.password) === null || _config$ui8 === void 0 ? void 0 : _config$ui8.show) !== false ? ((_config$ui9 = config.ui) === null || _config$ui9 === void 0 || (_config$ui9 = _config$ui9.formLabels) === null || _config$ui9 === void 0 || (_config$ui9 = _config$ui9.password) === null || _config$ui9 === void 0 ? void 0 : _config$ui9.text) || _pisell_utils.locales.getText("pisell-register2-password-label") : void 0,
		name: "password",
		rules: passwordValidationRules,
		validateTrigger: ["onBlur", "onSubmit"]
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Input.Password, {
		size: "large",
		placeholder: _pisell_utils.locales.getText("pisell-register2-password-placeholder"),
		className: "login2-password-input",
		autoComplete: "new-password",
		onKeyDown: (e) => {
			if (e.key === "Enter" && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
				e.preventDefault();
				handleFormSubmit(e);
			}
		}
	})), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "primary",
		size: "large",
		block: true,
		htmlType: "button",
		loading,
		onClick: handleFormSubmit,
		className: "login2-submit-button"
	}, isEmailNoVerification ? _pisell_utils.locales.getText("pisell-common-continue") : needsVerification ? _pisell_utils.locales.getText("pisell-common-continue") : _pisell_utils.locales.getText("pisell-common-sign-up"))), shouldShowDivider && /* @__PURE__ */ react.default.createElement("div", { className: "divider" }, /* @__PURE__ */ react.default.createElement("span", null, _pisell_utils.locales.getText("pisell-common-or"))), /* @__PURE__ */ react.default.createElement("div", { className: "oauth-section" }, hasEmailAndPhone && /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "default",
		size: "large",
		block: true,
		onClick: () => handleSwitchAuthMethod(currentAuthMethod === "email" ? "phone" : "email"),
		className: "login2-switch-button",
		icon: currentAuthMethod === "email" ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
			type: "pisell2-phone",
			className: "oauth-icon"
		}) : /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
			type: "pisell2-mail-01",
			className: "oauth-icon"
		}),
		style: { marginBottom: oauthProviders.length ? 12 : 0 }
	}, currentAuthMethod === "email" ? _pisell_utils.locales.getText("pisell-register2-with-phone") : _pisell_utils.locales.getText("pisell-register2-with-email")), /* @__PURE__ */ react.default.createElement(require_OAuthButtonGroup.default, {
		providers: oauthProviders,
		wrapperClassName: "oauth-buttons-container",
		buttonClassName: "oauth-button",
		layout: "vertical",
		gap: 12
	}))), /* @__PURE__ */ react.default.createElement("div", { className: "login2-footer-text" }, _pisell_utils.locales.getText("pisell-register2-have-account"), " ", /* @__PURE__ */ react.default.createElement("a", {
		className: "login2-link",
		onClick: onGoToLogin
	}, _pisell_utils.locales.getText("pisell-register2-login-link"))), /* @__PURE__ */ react.default.createElement(require_LegalTerms.default, { config: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config.legalTerms), {}, { mode: "register" }) }));
};
//#endregion
exports.default = Register2;
