import { getText } from "../../locales/index.js";
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 React, { useEffect, useState } from "react";
import { Button, Checkbox, Form, Input, Space } from "antd";
import { AppleOutlined, FacebookOutlined, GoogleOutlined } from "@ant-design/icons";
import "./index.less";
//#region src/components/login-and-register/index.tsx
/**
* Saas登录组件
* @param props - 组件属性
* @returns React组件
*/
const loginAndRegister = ({ showLogo = true, logo, logoPosition = "center", logoSize = {
	width: 48,
	height: 48
}, title = getText("Log in to your account"), showTitle = false, titleAlign = "center", subtitle = getText("pisell-login-and-register-subtit"), showSubtitle = false, subtitleAlign = "center", loginMethods = [
	"email",
	"google",
	"facebook",
	"apple"
], defaultLoginMethod = "email", emailLabel = getText("pisell-login-and-register-email-label"), passwordLabel = getText("pisell-login-and-register-password-label"), emailPlaceholder = getText("pisell-login-and-register-email-placeholder"), passwordPlaceholder = getText("pisell-login-and-register-password-placeholder"), emailRules = [{
	required: true,
	message: getText("pisell-login-and-register-email-error")
}, {
	type: "email",
	message: getText("pisell-login-and-register-email-error-valid")
}], buttonText = getText("Log in"), socialLoginTexts = {
	google: getText("pisell-login-and-register-google"),
	facebook: getText("pisell-login-and-register-facebook"),
	apple: getText("pisell-login-and-register-apple")
}, showFooter = true, showSignUp = true, signUpText = getText("pisell-login-and-register-sign-up"), signUpLink = "", signUpPosition = "center", otherLinks = [], otherLinksPosition = "center", showTabs = true, loginTabText = getText("pisell-login-and-register-login-tab"), registerTabText = getText("pisell-login-and-register-register-tab"), registerEmailPlaceholder = getText("pisell-login-and-register-register-email-placeholder"), registerPasswordPlaceholder = getText("pisell-login-and-register-register-password-placeholder"), registerButtonText = getText("pisell-login-and-register-register-button-text"), rememberMeText = getText("pisell-login-and-register-remember-me"), forgotPasswordText = getText("pisell-login-and-register-forgot-password"), showRememberMe = false, showForgotPassword = false, onLogin, onSocialLogin, onSignUp, onRegister, backgroundImage = "/login-illustration.png" }) => {
	const [form] = Form.useForm();
	const [loading, setLoading] = useState(false);
	const [activeTab, setActiveTab] = useState("login");
	useEffect(() => {
		document.body.id = "body";
	}, []);
	/**
	* 处理邮箱登录
	*/
	const handleEmailLogin = function() {
		var _ref = _asyncToGenerator(function* () {
			try {
				setLoading(true);
				const values = yield form.validateFields();
				yield onLogin === null || onLogin === void 0 ? void 0 : onLogin(_objectSpread2(_objectSpread2({}, values), {}, { loginMethod: "email" }));
			} catch (error) {
				console.error("Validation failed:", error);
			} finally {
				setLoading(false);
			}
		});
		return function handleEmailLogin() {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* 处理社交登录
	* @param type - 社交登录类型
	*/
	const handleSocialLogin = (type) => {
		onSocialLogin === null || onSocialLogin === void 0 || onSocialLogin(type);
	};
	/**
	* 处理注册提交
	*/
	const handleRegister = function() {
		var _ref2 = _asyncToGenerator(function* () {
			try {
				setLoading(true);
				const values = yield form.validateFields();
				yield onRegister === null || onRegister === void 0 ? void 0 : onRegister(values);
			} catch (error) {
				console.error("Validation failed:", error);
			} finally {
				setLoading(false);
			}
		});
		return function handleRegister() {
			return _ref2.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement("div", { className: "saas-login-main" }, /* @__PURE__ */ React.createElement("div", { className: "saas-login-container" }, showLogo && /* @__PURE__ */ React.createElement("div", { className: `saas-login-logo ${logoPosition}` }, logo && /* @__PURE__ */ React.createElement("img", {
		src: logo,
		alt: "logo"
	})), showTitle && /* @__PURE__ */ React.createElement("h1", {
		className: "saas-login-title",
		style: { textAlign: titleAlign }
	}, title), showSubtitle && /* @__PURE__ */ React.createElement("p", {
		className: "saas-login-subtitle",
		style: { textAlign: subtitleAlign }
	}, subtitle), showTabs && /* @__PURE__ */ React.createElement("div", { className: "saas-login-tabs" }, /* @__PURE__ */ React.createElement("div", {
		className: `tab ${activeTab === "login" ? "active" : ""}`,
		onClick: () => setActiveTab("login")
	}, loginTabText), /* @__PURE__ */ React.createElement("div", {
		className: `tab ${activeTab === "register" ? "active" : ""}`,
		onClick: () => setActiveTab("register")
	}, registerTabText)), /* @__PURE__ */ React.createElement("div", { className: "saas-login-form" }, activeTab === "login" ? /* @__PURE__ */ React.createElement(Form, { form }, /* @__PURE__ */ React.createElement("div", { className: "form-item-label" }, emailLabel), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "email",
		rules: emailRules
	}, /* @__PURE__ */ React.createElement(Input, {
		placeholder: emailPlaceholder,
		size: "large",
		className: "saas-login-input"
	})), /* @__PURE__ */ React.createElement("div", { className: "form-item-label" }, passwordLabel), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "password",
		rules: [{
			required: true,
			message: getText("pisell-login-and-register-register-password-error")
		}]
	}, /* @__PURE__ */ React.createElement(Input.Password, {
		placeholder: passwordPlaceholder,
		size: "large",
		className: "saas-login-input"
	})), (showRememberMe || showForgotPassword) && /* @__PURE__ */ React.createElement("div", { className: "saas-login-options" }, showRememberMe && /* @__PURE__ */ React.createElement(Checkbox, null, rememberMeText), showForgotPassword && /* @__PURE__ */ React.createElement("a", { className: "forgot-password" }, forgotPasswordText)), /* @__PURE__ */ React.createElement(Button, {
		type: "primary",
		block: true,
		size: "large",
		loading,
		onClick: handleEmailLogin,
		className: "saas-login-button"
	}, getText("pisell-login-and-register-login"))) : /* @__PURE__ */ React.createElement(Form, { form }, /* @__PURE__ */ React.createElement(Form.Item, {
		name: "email",
		rules: emailRules
	}, /* @__PURE__ */ React.createElement(Input, {
		placeholder: registerEmailPlaceholder,
		size: "large",
		className: "saas-login-input"
	})), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "password",
		rules: [{
			required: true,
			message: getText("pisell-login-and-register-register-password-error")
		}, {
			min: 6,
			message: getText("pisell-login-and-register-register-password-error-valid")
		}]
	}, /* @__PURE__ */ React.createElement(Input.Password, {
		placeholder: registerPasswordPlaceholder,
		size: "large",
		className: "saas-login-input"
	})), /* @__PURE__ */ React.createElement(Button, {
		type: "primary",
		block: true,
		size: "large",
		loading,
		onClick: handleRegister,
		className: "saas-login-button"
	}, registerButtonText)), /* @__PURE__ */ React.createElement(Space, {
		direction: "vertical",
		className: "saas-login-social"
	}, loginMethods.includes("google") && /* @__PURE__ */ React.createElement(Button, {
		block: true,
		icon: /* @__PURE__ */ React.createElement(GoogleOutlined, null),
		className: "saas-login-social-button google",
		onClick: () => handleSocialLogin("google")
	}, socialLoginTexts.google), loginMethods.includes("facebook") && /* @__PURE__ */ React.createElement(Button, {
		block: true,
		icon: /* @__PURE__ */ React.createElement(FacebookOutlined, null),
		className: "saas-login-social-button facebook",
		onClick: () => handleSocialLogin("facebook")
	}, socialLoginTexts.facebook), loginMethods.includes("apple") && /* @__PURE__ */ React.createElement(Button, {
		block: true,
		icon: /* @__PURE__ */ React.createElement(AppleOutlined, null),
		className: "saas-login-social-button apple",
		onClick: () => handleSocialLogin("apple")
	}, socialLoginTexts.apple))), showFooter && /* @__PURE__ */ React.createElement("div", { className: "saas-login-footer" }, showSignUp && /* @__PURE__ */ React.createElement("p", { className: "saas-login-signup" }, signUpText, signUpLink ? /* @__PURE__ */ React.createElement("a", { href: signUpLink }, signUpText) : /* @__PURE__ */ React.createElement("a", { onClick: onSignUp }, signUpText)), otherLinks.length > 0 && /* @__PURE__ */ React.createElement("div", { className: "saas-login-links" }, otherLinks.map((link, index) => /* @__PURE__ */ React.createElement("a", {
		key: index,
		href: link.url,
		target: "_blank",
		rel: "noopener noreferrer"
	}, link.text))))));
};
//#endregion
export { loginAndRegister as default };
