import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import LoginCodeInput from "./LoginCodeInput.js";
import SetPassword from "../steps/SetPassword.js";
import PhoneInput from "./PhoneInput.js";
import { createPhoneValidationRules } from "./phoneValidation.js";
import React, { useState } from "react";
import { Form, message } from "antd";
import { locales } from "@pisell/utils";
import { Button as Button$1, Input as Input$1 } from "@pisell/materials";
import IconFont from "@pisell/materials/src/components/iconfont";
//#region src/pro/Login2.0/components/ForgotPasswordFlow.tsx
/**
* 忘记密码流程组件
* 包含三个步骤：
* 1. 确认邮箱 (ConfirmEmail)
* 2. 输入验证码 (CodeInput)
* 3. 设置新密码 (SetPassword)
*/
function ForgotPasswordFlow({ initialEmail = "", accountType, onSendCode, onVerifyCode, onResetPassword, onSendResetLink, onBack, loading = false, countdown = 0, countries = [] }) {
	const [form] = Form.useForm();
	const [step, setStep] = useState("confirm_email");
	const [account, setAccount] = useState(typeof initialEmail === "object" && initialEmail && "phone" in initialEmail ? `+${initialEmail.country_calling_code}${initialEmail.phone}` : initialEmail || "");
	const [originalAccountData, setOriginalAccountData] = useState(initialEmail || "");
	const [verificationCode, setVerificationCode] = useState("");
	const handleSendResetLink = function() {
		var _ref = _asyncToGenerator(function* () {
			try {
				const emailToSend = (yield form.validateFields()).account || account;
				if (!emailToSend) {
					message.error(locales.getText("pisell-login2-email-required"));
					return;
				}
				yield onSendResetLink === null || onSendResetLink === void 0 ? void 0 : onSendResetLink(emailToSend);
				onBack === null || onBack === void 0 || onBack();
			} catch (error) {}
		});
		return function handleSendResetLink() {
			return _ref.apply(this, arguments);
		};
	}();
	const handleSendCode = function() {
		var _ref2 = _asyncToGenerator(function* () {
			try {
				const accountToSend = (yield form.validateFields()).account || originalAccountData;
				if (!accountToSend) {
					const errorMessage = accountType === "email" ? locales.getText("pisell-login2-email-required") : locales.getText("pisell-login2-phone-required");
					message.error(errorMessage);
					return;
				}
				yield onSendCode === null || onSendCode === void 0 ? void 0 : onSendCode(accountToSend);
				setOriginalAccountData(accountToSend);
				setAccount(typeof accountToSend === "object" && "phone" in accountToSend ? `+${accountToSend.country_calling_code}${accountToSend.phone}` : accountToSend);
				setStep("input_code");
			} catch (error) {}
		});
		return function handleSendCode() {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleVerifyCode = function() {
		var _ref3 = _asyncToGenerator(function* (code) {
			try {
				yield onVerifyCode === null || onVerifyCode === void 0 ? void 0 : onVerifyCode(originalAccountData, code);
				setVerificationCode(code);
				setStep("set_password");
			} catch (error) {}
		});
		return function handleVerifyCode(_x) {
			return _ref3.apply(this, arguments);
		};
	}();
	const handleSetPassword = function() {
		var _ref4 = _asyncToGenerator(function* (password, confirmPassword) {
			try {
				yield onResetPassword === null || onResetPassword === void 0 ? void 0 : onResetPassword(originalAccountData, password, verificationCode);
				message.success(locales.getText("pisell-forgot-password-success"));
				onBack === null || onBack === void 0 || onBack();
			} catch (error) {}
		});
		return function handleSetPassword(_x2, _x3) {
			return _ref4.apply(this, arguments);
		};
	}();
	const handleBackStep = () => {
		if (step === "input_code") setStep("confirm_email");
		else if (step === "set_password") setStep("input_code");
		else onBack === null || onBack === void 0 || onBack();
	};
	if (step === "confirm_email") {
		const isEmail = accountType === "email";
		isEmail ? locales.getText("pisell-login2-email-label") : locales.getText("pisell-login2-phone-label");
		const accountPlaceholder = isEmail ? locales.getText("pisell-login2-email-placeholder") : locales.getText("pisell-login2-phone-placeholder");
		const accountRules = isEmail ? [{
			required: true,
			message: locales.getText("pisell-login2-email-required")
		}, {
			type: "email",
			message: locales.getText("pisell-login2-email-invalid")
		}] : createPhoneValidationRules(locales.getText, true);
		return /* @__PURE__ */ React.createElement("div", { className: "register-step-container" }, /* @__PURE__ */ React.createElement("button", {
			className: "back-button-top",
			onClick: onBack,
			"aria-label": "Go back"
		}, /* @__PURE__ */ React.createElement(IconFont, { type: "pisell2-arrow-left" })), /* @__PURE__ */ React.createElement("div", { className: "register-step-content" }, /* @__PURE__ */ React.createElement("h2", { className: "register-step-title" }, locales.getText("pisell-forgot-password-title")), /* @__PURE__ */ React.createElement("p", { className: "register-step-desc" }, accountType === "email" ? locales.getText("pisell-forgot-password-desc") : locales.getText("pisell-forgot-password-desc-phone")), /* @__PURE__ */ React.createElement(Form, {
			form,
			layout: "vertical",
			size: "large",
			initialValues: { account: initialEmail }
		}, /* @__PURE__ */ React.createElement(Form.Item, {
			name: "account",
			rules: accountRules,
			initialValue: initialEmail,
			style: { marginBottom: "12px" }
		}, accountType === "email" ? /* @__PURE__ */ React.createElement(Input$1, {
			size: "large",
			placeholder: accountPlaceholder,
			className: "login2-input"
		}) : /* @__PURE__ */ React.createElement(PhoneInput, {
			size: "large",
			placeholder: accountPlaceholder,
			className: "login2-input",
			countries,
			valueFormat: "object"
		})), /* @__PURE__ */ React.createElement(Button$1, {
			type: "primary",
			size: "large",
			block: true,
			loading,
			onClick: handleSendCode,
			className: "register-submit-btn"
		}, locales.getText("pisell-forgot-password-send-code")), accountType === "email" && /* @__PURE__ */ React.createElement(Button$1, {
			type: "primary",
			size: "large",
			block: true,
			loading,
			onClick: handleSendResetLink,
			className: "register-submit-btn",
			style: { marginTop: "12px" }
		}, locales.getText("pisell-forgot-password-send-link")), /* @__PURE__ */ React.createElement(Button$1, {
			type: "default",
			size: "large",
			block: true,
			onClick: onBack,
			className: "register-back-btn"
		}, locales.getText("pisell-login2-back")))));
	}
	if (step === "input_code") return /* @__PURE__ */ React.createElement(LoginCodeInput, {
		account,
		accountType,
		codeLength: 4,
		onSubmit: handleVerifyCode,
		onResend: () => onSendCode === null || onSendCode === void 0 ? void 0 : onSendCode(account),
		onBack: handleBackStep,
		countdown,
		loading,
		submitButtonText: locales.getText("pisell-forgot-password-verify")
	});
	if (step === "set_password") return /* @__PURE__ */ React.createElement(SetPassword, {
		flowData: {
			currentStep: "set_password",
			accountType,
			account,
			verificationMethod: "email_code",
			verificationCode,
			password: "",
			extraData: {}
		},
		onSubmit: handleSetPassword,
		onBack: handleBackStep,
		loading
	});
	return null;
}
//#endregion
export { ForgotPasswordFlow as default };
