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 locales_default from "./locales.js";
import Login2 from "./Login2.js";
import Register2 from "./Register2.js";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { isMobile, locales } from "@pisell/utils";
import { PisellModal } from "@pisell/materials";
//#region src/pro/Login2.0/AuthModal.tsx
/**
* 认证模态框组件
* 包装 Login2 和 Register2 组件，提供统一的配置接口和页面切换功能
*/
const AuthModal = () => {
	var _useEngineContext$eng;
	const context = useEngineContext();
	const { config, channel, modal = false, visible = true, onClose, className, style, defaultView = "login", emailLinkVerification, onLoginSuccess, onRegisterSuccess, onOAuthLogin, onSendVerificationCode, onForgotPassword, onEmailLinkExpired, onError } = ((_useEngineContext$eng = useEngineContext().engine) === null || _useEngineContext$eng === void 0 ? void 0 : _useEngineContext$eng.props) || {};
	const [currentView, setCurrentView] = useState(config.isEmailLinkRegister ? "register" : defaultView);
	const [localesInitialized, setLocalesInitialized] = useState(false);
	useEffect(() => {
		var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4;
		locales.init(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");
		setLocalesInitialized(true);
	}, []);
	useCallback(() => {
		if (currentView === "login") return locales.getText("pisell-modal-login-title");
		else return locales.getText("pisell-modal-register-title");
	}, [currentView]);
	const login2Config = useMemo(() => {
		var _config$auth, _authMethods$email, _authMethods$phone, _authMethods$google, _authMethods$facebook, _authMethods$apple, _authMethods$guest, _config$page, _config$page2, _config$page3, _config$page4, _config$page5, _config$page6, _config$logoSize, _config$logoSize2, _config$page7;
		if (!localesInitialized) return {
			loginMethods: [],
			channel,
			emailLinkVerification,
			legalTerms: {
				enabled: false,
				terms: []
			},
			ui: {
				title: {
					show: false,
					text: "",
					align: "center"
				},
				themeColor: "#7F56D9",
				formLabels: {
					email: { show: false },
					phone: { show: false },
					password: { show: false },
					verificationCode: { show: false }
				}
			}
		};
		const loginMethods = [];
		const authMethods = (config === null || config === void 0 || (_config$auth = config.auth) === null || _config$auth === void 0 ? void 0 : _config$auth.method) || {};
		const enabledMethods = [];
		if ((_authMethods$email = authMethods.email) === null || _authMethods$email === void 0 ? void 0 : _authMethods$email.enabled) enabledMethods.push({
			type: "email",
			sort: authMethods.email.sort || 0,
			config: authMethods.email
		});
		if ((_authMethods$phone = authMethods.phone) === null || _authMethods$phone === void 0 ? void 0 : _authMethods$phone.enabled) enabledMethods.push({
			type: "phone",
			sort: authMethods.phone.sort || 0,
			config: authMethods.phone
		});
		if ((_authMethods$google = authMethods.google) === null || _authMethods$google === void 0 ? void 0 : _authMethods$google.enabled) enabledMethods.push({
			type: "google",
			sort: authMethods.google.sort || 0,
			config: authMethods.google
		});
		if ((_authMethods$facebook = authMethods.facebook) === null || _authMethods$facebook === void 0 ? void 0 : _authMethods$facebook.enabled) enabledMethods.push({
			type: "facebook",
			sort: authMethods.facebook.sort || 0,
			config: authMethods.facebook
		});
		if ((_authMethods$apple = authMethods.apple) === null || _authMethods$apple === void 0 ? void 0 : _authMethods$apple.enabled) enabledMethods.push({
			type: "apple",
			sort: authMethods.apple.sort || 0,
			config: authMethods.apple
		});
		if ((_authMethods$guest = authMethods.guest) === null || _authMethods$guest === void 0 ? void 0 : _authMethods$guest.enabled) enabledMethods.push({
			type: "guest",
			sort: authMethods.guest.sort || 0,
			config: authMethods.guest
		});
		enabledMethods.sort((a, b) => a.sort - b.sort);
		enabledMethods.forEach(({ type, config: methodConfig }) => {
			if (type === "email") {
				var _methodConfig$setting, _loginMethod$password, _loginMethod$code, _loginMethod$link;
				const verificationMethods = [];
				const loginMethod = (methodConfig === null || methodConfig === void 0 || (_methodConfig$setting = methodConfig.setting) === null || _methodConfig$setting === void 0 ? void 0 : _methodConfig$setting.login_method) || { "password": {
					enabled: true,
					sort: 1
				} };
				const enabledVerificationMethods = [];
				if (loginMethod === null || loginMethod === void 0 || (_loginMethod$password = loginMethod.password) === null || _loginMethod$password === void 0 ? void 0 : _loginMethod$password.enabled) {
					var _loginMethod$password2;
					enabledVerificationMethods.push({
						type: "password",
						sort: ((_loginMethod$password2 = loginMethod.password) === null || _loginMethod$password2 === void 0 ? void 0 : _loginMethod$password2.sort) || 0
					});
				}
				if (loginMethod === null || loginMethod === void 0 || (_loginMethod$code = loginMethod.code) === null || _loginMethod$code === void 0 ? void 0 : _loginMethod$code.enabled) enabledVerificationMethods.push({
					type: "verification_code",
					sort: loginMethod.code.sort || 0
				});
				if (loginMethod === null || loginMethod === void 0 || (_loginMethod$link = loginMethod.link) === null || _loginMethod$link === void 0 ? void 0 : _loginMethod$link.enabled) enabledVerificationMethods.push({
					type: "verification_code",
					sort: loginMethod.link.sort || 0
				});
				enabledVerificationMethods.sort((a, b) => a.sort - b.sort);
				verificationMethods.push(...enabledVerificationMethods.map((v) => v.type));
				if (verificationMethods.length > 0) loginMethods.push({
					type: "email",
					verificationMethods,
					stepByStep: true
				});
			} else if (type === "phone") {
				var _methodConfig$setting2, _loginMethod$password3, _loginMethod$code2;
				const verificationMethods = [];
				const loginMethod = (methodConfig === null || methodConfig === void 0 || (_methodConfig$setting2 = methodConfig.setting) === null || _methodConfig$setting2 === void 0 ? void 0 : _methodConfig$setting2.login_method) || {};
				const enabledVerificationMethods = [];
				if (loginMethod === null || loginMethod === void 0 || (_loginMethod$password3 = loginMethod.password) === null || _loginMethod$password3 === void 0 ? void 0 : _loginMethod$password3.enabled) enabledVerificationMethods.push({
					type: "password",
					sort: loginMethod.password.sort || 0
				});
				if (loginMethod === null || loginMethod === void 0 || (_loginMethod$code2 = loginMethod.code) === null || _loginMethod$code2 === void 0 ? void 0 : _loginMethod$code2.enabled) enabledVerificationMethods.push({
					type: "verification_code",
					sort: loginMethod.code.sort || 0
				});
				enabledVerificationMethods.sort((a, b) => a.sort - b.sort);
				verificationMethods.push(...enabledVerificationMethods.map((v) => v.type));
				if (verificationMethods.length > 0) loginMethods.push({
					type: "phone",
					verificationMethods,
					stepByStep: true
				});
			} else if (type === "google") loginMethods.push({ type: "google" });
			else if (type === "facebook") {
				var _methodConfig$setting3;
				loginMethods.push({
					type: "facebook",
					appId: (methodConfig === null || methodConfig === void 0 || (_methodConfig$setting3 = methodConfig.setting) === null || _methodConfig$setting3 === void 0 ? void 0 : _methodConfig$setting3.appid) || ""
				});
			} else if (type === "apple") {
				var _methodConfig$setting4;
				loginMethods.push({
					type: "apple",
					clientId: (methodConfig === null || methodConfig === void 0 || (_methodConfig$setting4 = methodConfig.setting) === null || _methodConfig$setting4 === void 0 ? void 0 : _methodConfig$setting4.identifier) || ""
				});
			} else if (type === "guest") loginMethods.push({ type: "guest" });
		});
		return {
			loginMethods,
			channel,
			emailLinkVerification: config.isForGetPasswordLink ? {
				enabled: true,
				code: config.linkCode
			} : emailLinkVerification,
			legalTerms: {
				enabled: (config === null || config === void 0 || (_config$page = config.page) === null || _config$page === void 0 || (_config$page = _config$page.login) === null || _config$page === void 0 ? void 0 : _config$page.same_as_register) ? (config === null || config === void 0 || (_config$page2 = config.page) === null || _config$page2 === void 0 || (_config$page2 = _config$page2.register) === null || _config$page2 === void 0 || (_config$page2 = _config$page2.terms) === null || _config$page2 === void 0 ? void 0 : _config$page2.length) > 0 : (config === null || config === void 0 || (_config$page3 = config.page) === null || _config$page3 === void 0 || (_config$page3 = _config$page3.login) === null || _config$page3 === void 0 || (_config$page3 = _config$page3.terms) === null || _config$page3 === void 0 ? void 0 : _config$page3.length) > 0,
				terms: (config === null || config === void 0 || (_config$page4 = config.page) === null || _config$page4 === void 0 || (_config$page4 = _config$page4.login) === null || _config$page4 === void 0 ? void 0 : _config$page4.same_as_register) ? config === null || config === void 0 || (_config$page5 = config.page) === null || _config$page5 === void 0 || (_config$page5 = _config$page5.register) === null || _config$page5 === void 0 ? void 0 : _config$page5.terms : config === null || config === void 0 || (_config$page6 = config.page) === null || _config$page6 === void 0 || (_config$page6 = _config$page6.login) === null || _config$page6 === void 0 ? void 0 : _config$page6.terms
			},
			ui: {
				title: {
					show: true,
					text: locales.getText("pisell-modal-login-title"),
					align: "center"
				},
				logo: (config === null || config === void 0 ? void 0 : config.logo) ? {
					show: (config === null || config === void 0 ? void 0 : config.logo) ? true : false,
					url: config === null || config === void 0 ? void 0 : config.logo,
					position: "center",
					size: {
						width: (config === null || config === void 0 || (_config$logoSize = config.logoSize) === null || _config$logoSize === void 0 ? void 0 : _config$logoSize.width) || 48,
						height: (config === null || config === void 0 || (_config$logoSize2 = config.logoSize) === null || _config$logoSize2 === void 0 ? void 0 : _config$logoSize2.height) || 48
					}
				} : void 0,
				themeColor: (config === null || config === void 0 || (_config$page7 = config.page) === null || _config$page7 === void 0 || (_config$page7 = _config$page7.login) === null || _config$page7 === void 0 || (_config$page7 = _config$page7.ui) === null || _config$page7 === void 0 ? void 0 : _config$page7.themeColor) || "#7F56D9",
				formLabels: {
					email: { show: false },
					phone: { show: false },
					password: { show: false },
					verificationCode: { show: false }
				}
			}
		};
	}, [
		config,
		channel,
		emailLinkVerification,
		localesInitialized
	]);
	const register2Config = useMemo(() => {
		var _config$auth2, _authMethods$email2, _authMethods$phone2, _authMethods$google2, _authMethods$facebook2, _authMethods$apple2, _config$page8, _config$page9, _config$logoSize3, _config$logoSize4, _config$page10, _config$auth3;
		if (!localesInitialized) return {
			registrationMethods: [],
			channel,
			emailLinkVerification,
			legalTerms: {
				enabled: false,
				terms: []
			},
			ui: {
				title: {
					show: false,
					text: "",
					align: "center"
				},
				themeColor: "#7F56D9",
				formLabels: {
					email: { show: false },
					phone: { show: false },
					password: { show: false },
					verificationCode: { show: false }
				}
			}
		};
		const registrationMethods = [];
		const authMethods = ((_config$auth2 = config.auth) === null || _config$auth2 === void 0 ? void 0 : _config$auth2.method) || {};
		const enabledMethods = [];
		if (((_authMethods$email2 = authMethods.email) === null || _authMethods$email2 === void 0 ? void 0 : _authMethods$email2.enabled) && authMethods.email.setting.register_enabled) enabledMethods.push({
			type: "email",
			sort: authMethods.email.sort,
			config: authMethods.email
		});
		if (((_authMethods$phone2 = authMethods.phone) === null || _authMethods$phone2 === void 0 ? void 0 : _authMethods$phone2.enabled) && authMethods.phone.setting.register_enabled) enabledMethods.push({
			type: "phone",
			sort: authMethods.phone.sort,
			config: authMethods.phone
		});
		if ((_authMethods$google2 = authMethods.google) === null || _authMethods$google2 === void 0 ? void 0 : _authMethods$google2.enabled) enabledMethods.push({
			type: "google",
			sort: authMethods.google.sort,
			config: authMethods.google
		});
		if ((_authMethods$facebook2 = authMethods.facebook) === null || _authMethods$facebook2 === void 0 ? void 0 : _authMethods$facebook2.enabled) enabledMethods.push({
			type: "facebook",
			sort: authMethods.facebook.sort,
			config: authMethods.facebook
		});
		if ((_authMethods$apple2 = authMethods.apple) === null || _authMethods$apple2 === void 0 ? void 0 : _authMethods$apple2.enabled) enabledMethods.push({
			type: "apple",
			sort: authMethods.apple.sort,
			config: authMethods.apple
		});
		enabledMethods.sort((a, b) => a.sort - b.sort);
		enabledMethods.forEach(({ type, config: methodConfig }) => {
			if (type === "email") {
				const verificationMethod = methodConfig.setting.register_method === "link" ? "verification_link" : "verification_code";
				registrationMethods.push({
					type: "email",
					verificationMethod
				});
			} else if (type === "phone") registrationMethods.push({
				type: "phone",
				verificationMethod: "verification_code"
			});
			else if (type === "google") registrationMethods.push({ type: "google" });
			else if (type === "facebook") {
				var _methodConfig$setting5;
				registrationMethods.push({
					type: "facebook",
					appId: (methodConfig === null || methodConfig === void 0 || (_methodConfig$setting5 = methodConfig.setting) === null || _methodConfig$setting5 === void 0 ? void 0 : _methodConfig$setting5.appid) || ""
				});
			} else if (type === "apple") {
				var _methodConfig$setting6;
				registrationMethods.push({
					type: "apple",
					clientId: (methodConfig === null || methodConfig === void 0 || (_methodConfig$setting6 = methodConfig.setting) === null || _methodConfig$setting6 === void 0 ? void 0 : _methodConfig$setting6.identifier) || ""
				});
			}
		});
		const transformPasswordRules = (passwordConfig) => {
			if (!passwordConfig) return;
			return {
				minLength: passwordConfig.min_length,
				requireLowercase: passwordConfig.require_lowercase_letters,
				requireNumbers: passwordConfig.require_numbers,
				requireSpecialCharacters: passwordConfig.require_special_characters,
				requireUppercase: passwordConfig.require_uppercase_letters
			};
		};
		return {
			registrationMethods,
			channel,
			emailLinkVerification: config.isEmailLinkRegister ? {
				enabled: true,
				code: config.linkCode
			} : emailLinkVerification,
			legalTerms: {
				enabled: ((_config$page8 = config.page) === null || _config$page8 === void 0 || (_config$page8 = _config$page8.register) === null || _config$page8 === void 0 || (_config$page8 = _config$page8.terms) === null || _config$page8 === void 0 ? void 0 : _config$page8.length) > 0,
				terms: (_config$page9 = config.page) === null || _config$page9 === void 0 || (_config$page9 = _config$page9.register) === null || _config$page9 === void 0 ? void 0 : _config$page9.terms
			},
			ui: {
				title: {
					show: true,
					text: locales.getText("pisell-modal-register-title"),
					align: "center"
				},
				logo: config.logo ? {
					show: config.logo ? true : false,
					url: config.logo,
					position: "center",
					size: {
						width: (config === null || config === void 0 || (_config$logoSize3 = config.logoSize) === null || _config$logoSize3 === void 0 ? void 0 : _config$logoSize3.width) || 48,
						height: (config === null || config === void 0 || (_config$logoSize4 = config.logoSize) === null || _config$logoSize4 === void 0 ? void 0 : _config$logoSize4.height) || 48
					}
				} : void 0,
				themeColor: ((_config$page10 = config.page) === null || _config$page10 === void 0 || (_config$page10 = _config$page10.register) === null || _config$page10 === void 0 || (_config$page10 = _config$page10.ui) === null || _config$page10 === void 0 ? void 0 : _config$page10.themeColor) || "#7F56D9",
				passwordRules: transformPasswordRules((_config$auth3 = config.auth) === null || _config$auth3 === void 0 ? void 0 : _config$auth3.password),
				formLabels: {
					email: { show: false },
					phone: { show: false },
					password: { show: false },
					verificationCode: { show: false }
				}
			}
		};
	}, [
		config,
		channel,
		emailLinkVerification,
		localesInitialized
	]);
	const handleGoToRegister = useCallback(() => {
		setCurrentView("register");
	}, []);
	const handleGoToLogin = useCallback(() => {
		setCurrentView("login");
	}, []);
	const handleError = useCallback((error, context) => {
		if (onError) onError(error, context);
	}, [onError]);
	const login2Events = {
		onLogin: function() {
			var _ref = _asyncToGenerator(function* (data, method, loginData, channel) {
				yield onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(data, method, channel);
				if (modal && onClose) onClose();
			});
			return function onLogin(_x, _x2, _x3, _x4) {
				return _ref.apply(this, arguments);
			};
		}(),
		onOAuthLogin,
		onSendVerificationCode,
		onForgotPassword,
		onSwitchToRegister: handleGoToRegister,
		onError: (error, method) => {
			handleError(error, {
				action: "login",
				method,
				channel
			});
		}
	};
	const register2Events = {
		onRegister: function() {
			var _ref2 = _asyncToGenerator(function* (_, method, data, channel) {
				yield onRegisterSuccess === null || onRegisterSuccess === void 0 ? void 0 : onRegisterSuccess(_, method, channel);
				yield onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(_, method, channel);
				if (modal && onClose) onClose();
			});
			return function onRegister(_x5, _x6, _x7, _x8) {
				return _ref2.apply(this, arguments);
			};
		}(),
		onOAuthLogin,
		onSendVerificationCode,
		onEmailLinkExpired,
		onGoToLogin: handleGoToLogin,
		onError: (error, method) => {
			handleError(error, {
				action: "register",
				method,
				channel
			});
		}
	};
	const renderContent = () => {
		if (currentView === "login") return /* @__PURE__ */ React.createElement(Login2, _objectSpread2({
			config: login2Config,
			visible: true,
			onClose
		}, login2Events));
		else return /* @__PURE__ */ React.createElement(Register2, _objectSpread2({
			config: register2Config,
			visible: true,
			onClose
		}, register2Events));
	};
	if (modal) return /* @__PURE__ */ React.createElement(PisellModal, {
		open: visible,
		onCancel: onClose,
		header: false,
		footer: null,
		centered: true,
		className,
		destroyOnClose: true,
		platform: isMobile ? "h5" : "pc"
	}, renderContent());
	return /* @__PURE__ */ React.createElement("div", {
		className,
		style
	}, visible && renderContent());
};
//#endregion
export { AuthModal as default };
