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 { mergeData } from "./utils.js";
import React, { useEffect, useState } from "react";
import { Button, Form, Input } from "antd";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/plus/saasLogin/index.tsx
const SaasLogin = (props) => {
	var _context$engine, _data$logoSize, _data$logoSize2, _data$logoSize3;
	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 [loading, setLoading] = useState(false);
	const [data, setData] = useState({});
	useEffect(() => {
		setData(_objectSpread2({}, mergeData(locales, props)));
	}, [props, locales]);
	const emailRules = [{
		required: true,
		message: locales.getText("pisell-saas-login-email-error")
	}, {
		type: "email",
		message: locales.getText("pisell-saas-login-email-error-valid")
	}];
	/**
	* 处理邮箱登录
	*/
	const handleEmailLogin = function() {
		var _ref = _asyncToGenerator(function* () {
			try {
				var _props$onLogin;
				setLoading(true);
				const values = yield form.validateFields();
				yield props === null || props === void 0 || (_props$onLogin = props.onLogin) === null || _props$onLogin === void 0 ? void 0 : _props$onLogin.call(props, _objectSpread2(_objectSpread2({}, values), {}, { loginMethod: "email" }));
			} catch (error) {
				console.error("Validation failed:", error);
			} finally {
				setLoading(false);
			}
		});
		return function handleEmailLogin() {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-login-main" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-login-container" }, (data === null || data === void 0 ? void 0 : data.showLogo) && /* @__PURE__ */ React.createElement("div", {
		className: `pisell-saas-login-logo ${data.logoPosition}`,
		style: {
			width: (data === null || data === void 0 || (_data$logoSize = data.logoSize) === null || _data$logoSize === void 0 ? void 0 : _data$logoSize.width) || 48,
			height: (data === null || data === void 0 || (_data$logoSize2 = data.logoSize) === null || _data$logoSize2 === void 0 ? void 0 : _data$logoSize2.height) || 48
		}
	}, (data === null || data === void 0 ? void 0 : data.logo) && /* @__PURE__ */ React.createElement("img", {
		src: data === null || data === void 0 ? void 0 : data.logo,
		alt: "logo",
		style: { height: (data === null || data === void 0 || (_data$logoSize3 = data.logoSize) === null || _data$logoSize3 === void 0 ? void 0 : _data$logoSize3.height) || 48 }
	})), (data === null || data === void 0 ? void 0 : data.showWelcomeMessage) && /* @__PURE__ */ React.createElement("h1", {
		className: "pisell-saas-login-welcome-message",
		style: { textAlign: data === null || data === void 0 ? void 0 : data.welcomeMessageAlign }
	}, (data === null || data === void 0 ? void 0 : data.welcomeMessage) || ""), (data === null || data === void 0 ? void 0 : data.showTitle) && /* @__PURE__ */ React.createElement("h1", {
		className: "pisell-saas-login-title",
		style: { textAlign: data === null || data === void 0 ? void 0 : data.titleAlign }
	}, (data === null || data === void 0 ? void 0 : data.title) || ""), (data === null || data === void 0 ? void 0 : data.showSubtitle) && /* @__PURE__ */ React.createElement("p", {
		className: "pisell-saas-login-subtitle",
		style: { textAlign: data === null || data === void 0 ? void 0 : data.subtitleAlign }
	}, (data === null || data === void 0 ? void 0 : data.subtitle) || ""), /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-login-form" }, /* @__PURE__ */ React.createElement(Form, { form }, /* @__PURE__ */ React.createElement("div", { className: "form-item-label" }, (data === null || data === void 0 ? void 0 : data.emailLabel) || ""), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "email",
		rules: emailRules
	}, /* @__PURE__ */ React.createElement(Input, {
		placeholder: (data === null || data === void 0 ? void 0 : data.emailPlaceholder) || "",
		size: "large",
		className: "pisell-saas-login-input"
	})), /* @__PURE__ */ React.createElement("div", { className: "form-item-label" }, (data === null || data === void 0 ? void 0 : data.passwordLabel) || ""), /* @__PURE__ */ React.createElement(Form.Item, {
		name: "password",
		rules: [{
			required: true,
			message: locales.getText("pisell-saas-login-password-required")
		}]
	}, /* @__PURE__ */ React.createElement(Input.Password, {
		placeholder: (data === null || data === void 0 ? void 0 : data.passwordPlaceholder) || "",
		size: "large",
		className: "pisell-saas-login-input"
	})), /* @__PURE__ */ React.createElement(Button, {
		type: "primary",
		block: true,
		size: "large",
		loading,
		onClick: handleEmailLogin,
		className: "pisell-saas-login-button"
	}, (data === null || data === void 0 ? void 0 : data.buttonText) || ""))), /* @__PURE__ */ React.createElement("div", { className: "pisell-saas-login-footer" }, (data === null || data === void 0 ? void 0 : data.showRegister) && /* @__PURE__ */ React.createElement("p", {
		className: "pisell-saas-login-signup",
		style: { textAlign: data === null || data === void 0 ? void 0 : data.registerAlign }
	}, (data === null || data === void 0 ? void 0 : data.showRegisterText) ? data === null || data === void 0 ? void 0 : data.registerText : "", (data === null || data === void 0 ? void 0 : data.showRegisterLinkText) && /* @__PURE__ */ React.createElement("a", {
		onClick: props === null || props === void 0 ? void 0 : props.onGoRegister,
		className: "pisell-saas-login-signup-link"
	}, (data === null || data === void 0 ? void 0 : data.registerLinkText) || "")))));
};
//#endregion
export { SaasLogin as default };
