const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_LoginCodeInput = require("./LoginCodeInput.js");
const require_SetPassword = require("../steps/SetPassword.js");
const require_PhoneInput = require("./PhoneInput.js");
const require_phoneValidation = require("./phoneValidation.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 _pisell_materials_src_components_iconfont = require("@pisell/materials/src/components/iconfont");
_pisell_materials_src_components_iconfont = require_runtime.__toESM(_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] = antd.Form.useForm();
	const [step, setStep] = (0, react.useState)("confirm_email");
	const [account, setAccount] = (0, react.useState)(typeof initialEmail === "object" && initialEmail && "phone" in initialEmail ? `+${initialEmail.country_calling_code}${initialEmail.phone}` : initialEmail || "");
	const [originalAccountData, setOriginalAccountData] = (0, react.useState)(initialEmail || "");
	const [verificationCode, setVerificationCode] = (0, react.useState)("");
	const handleSendResetLink = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			try {
				const emailToSend = (yield form.validateFields()).account || account;
				if (!emailToSend) {
					antd.message.error(_pisell_utils.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 = require_asyncToGenerator._asyncToGenerator(function* () {
			try {
				const accountToSend = (yield form.validateFields()).account || originalAccountData;
				if (!accountToSend) {
					const errorMessage = accountType === "email" ? _pisell_utils.locales.getText("pisell-login2-email-required") : _pisell_utils.locales.getText("pisell-login2-phone-required");
					antd.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 = require_asyncToGenerator._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 = require_asyncToGenerator._asyncToGenerator(function* (password, confirmPassword) {
			try {
				yield onResetPassword === null || onResetPassword === void 0 ? void 0 : onResetPassword(originalAccountData, password, verificationCode);
				antd.message.success(_pisell_utils.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 ? _pisell_utils.locales.getText("pisell-login2-email-label") : _pisell_utils.locales.getText("pisell-login2-phone-label");
		const accountPlaceholder = isEmail ? _pisell_utils.locales.getText("pisell-login2-email-placeholder") : _pisell_utils.locales.getText("pisell-login2-phone-placeholder");
		const accountRules = isEmail ? [{
			required: true,
			message: _pisell_utils.locales.getText("pisell-login2-email-required")
		}, {
			type: "email",
			message: _pisell_utils.locales.getText("pisell-login2-email-invalid")
		}] : require_phoneValidation.createPhoneValidationRules(_pisell_utils.locales.getText, true);
		return /* @__PURE__ */ react.default.createElement("div", { className: "register-step-container" }, /* @__PURE__ */ react.default.createElement("button", {
			className: "back-button-top",
			onClick: onBack,
			"aria-label": "Go back"
		}, /* @__PURE__ */ react.default.createElement(_pisell_materials_src_components_iconfont.default, { type: "pisell2-arrow-left" })), /* @__PURE__ */ react.default.createElement("div", { className: "register-step-content" }, /* @__PURE__ */ react.default.createElement("h2", { className: "register-step-title" }, _pisell_utils.locales.getText("pisell-forgot-password-title")), /* @__PURE__ */ react.default.createElement("p", { className: "register-step-desc" }, accountType === "email" ? _pisell_utils.locales.getText("pisell-forgot-password-desc") : _pisell_utils.locales.getText("pisell-forgot-password-desc-phone")), /* @__PURE__ */ react.default.createElement(antd.Form, {
			form,
			layout: "vertical",
			size: "large",
			initialValues: { account: initialEmail }
		}, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			name: "account",
			rules: accountRules,
			initialValue: initialEmail,
			style: { marginBottom: "12px" }
		}, accountType === "email" ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Input, {
			size: "large",
			placeholder: accountPlaceholder,
			className: "login2-input"
		}) : /* @__PURE__ */ react.default.createElement(require_PhoneInput.default, {
			size: "large",
			placeholder: accountPlaceholder,
			className: "login2-input",
			countries,
			valueFormat: "object"
		})), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "primary",
			size: "large",
			block: true,
			loading,
			onClick: handleSendCode,
			className: "register-submit-btn"
		}, _pisell_utils.locales.getText("pisell-forgot-password-send-code")), accountType === "email" && /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "primary",
			size: "large",
			block: true,
			loading,
			onClick: handleSendResetLink,
			className: "register-submit-btn",
			style: { marginTop: "12px" }
		}, _pisell_utils.locales.getText("pisell-forgot-password-send-link")), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
			type: "default",
			size: "large",
			block: true,
			onClick: onBack,
			className: "register-back-btn"
		}, _pisell_utils.locales.getText("pisell-login2-back")))));
	}
	if (step === "input_code") return /* @__PURE__ */ react.default.createElement(require_LoginCodeInput.default, {
		account,
		accountType,
		codeLength: 4,
		onSubmit: handleVerifyCode,
		onResend: () => onSendCode === null || onSendCode === void 0 ? void 0 : onSendCode(account),
		onBack: handleBackStep,
		countdown,
		loading,
		submitButtonText: _pisell_utils.locales.getText("pisell-forgot-password-verify")
	});
	if (step === "set_password") return /* @__PURE__ */ react.default.createElement(require_SetPassword.default, {
		flowData: {
			currentStep: "set_password",
			accountType,
			account,
			verificationMethod: "email_code",
			verificationCode,
			password: "",
			extraData: {}
		},
		onSubmit: handleSetPassword,
		onBack: handleBackStep,
		loading
	});
	return null;
}
//#endregion
exports.default = ForgotPasswordFlow;
