var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/pro/Login2.0/components/ForgotPasswordFlow.tsx
var ForgotPasswordFlow_exports = {};
__export(ForgotPasswordFlow_exports, {
  default: () => ForgotPasswordFlow_default
});
module.exports = __toCommonJS(ForgotPasswordFlow_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_LoginCodeInput = __toESM(require("./LoginCodeInput"));
var import_SetPassword = __toESM(require("../steps/SetPassword"));
var import_PhoneInput = __toESM(require("./PhoneInput"));
var import_phoneValidation = require("./phoneValidation");
var import_iconfont = __toESM(require("@pisell/materials/src/components/iconfont"));
function ForgotPasswordFlow({
  initialEmail = "",
  accountType,
  onSendCode,
  onVerifyCode,
  onResetPassword,
  onSendResetLink,
  onBack,
  loading = false,
  countdown = 0,
  countries = []
}) {
  const [form] = import_antd.Form.useForm();
  const [step, setStep] = (0, import_react.useState)("confirm_email");
  const initialAccountString = typeof initialEmail === "object" && initialEmail && "phone" in initialEmail ? `+${initialEmail.country_calling_code}${initialEmail.phone}` : initialEmail || "";
  const [account, setAccount] = (0, import_react.useState)(initialAccountString);
  const [originalAccountData, setOriginalAccountData] = (0, import_react.useState)(initialEmail || "");
  const [verificationCode, setVerificationCode] = (0, import_react.useState)("");
  const handleSendResetLink = async () => {
    try {
      const values = await form.validateFields();
      const emailToSend = values.account || account;
      if (!emailToSend) {
        import_antd.message.error(import_utils.locales.getText("pisell-login2-email-required"));
        return;
      }
      await (onSendResetLink == null ? void 0 : onSendResetLink(emailToSend));
      onBack == null ? void 0 : onBack();
    } catch (error) {
    }
  };
  const handleSendCode = async () => {
    try {
      const values = await form.validateFields();
      const accountToSend = values.account || originalAccountData;
      if (!accountToSend) {
        const errorMessage = accountType === "email" ? import_utils.locales.getText("pisell-login2-email-required") : import_utils.locales.getText("pisell-login2-phone-required");
        import_antd.message.error(errorMessage);
        return;
      }
      await (onSendCode == null ? void 0 : onSendCode(accountToSend));
      setOriginalAccountData(accountToSend);
      const accountForState = typeof accountToSend === "object" && "phone" in accountToSend ? `+${accountToSend.country_calling_code}${accountToSend.phone}` : accountToSend;
      setAccount(accountForState);
      setStep("input_code");
    } catch (error) {
    }
  };
  const handleVerifyCode = async (code) => {
    try {
      await (onVerifyCode == null ? void 0 : onVerifyCode(originalAccountData, code));
      setVerificationCode(code);
      setStep("set_password");
    } catch (error) {
    }
  };
  const handleSetPassword = async (password, confirmPassword) => {
    try {
      await (onResetPassword == null ? void 0 : onResetPassword(originalAccountData, password, verificationCode));
      import_antd.message.success(import_utils.locales.getText("pisell-forgot-password-success"));
      onBack == null ? void 0 : onBack();
    } catch (error) {
    }
  };
  const handleBackStep = () => {
    if (step === "input_code") {
      setStep("confirm_email");
    } else if (step === "set_password") {
      setStep("input_code");
    } else {
      onBack == null ? void 0 : onBack();
    }
  };
  if (step === "confirm_email") {
    const isEmail = accountType === "email";
    const accountLabel = isEmail ? import_utils.locales.getText("pisell-login2-email-label") : import_utils.locales.getText("pisell-login2-phone-label");
    const accountPlaceholder = isEmail ? import_utils.locales.getText("pisell-login2-email-placeholder") : import_utils.locales.getText("pisell-login2-phone-placeholder");
    const accountRules = isEmail ? [
      { required: true, message: import_utils.locales.getText("pisell-login2-email-required") },
      { type: "email", message: import_utils.locales.getText("pisell-login2-email-invalid") }
    ] : (0, import_phoneValidation.createPhoneValidationRules)(import_utils.locales.getText, true);
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "register-step-container" }, /* @__PURE__ */ import_react.default.createElement("button", { className: "back-button-top", onClick: onBack, "aria-label": "Go back" }, /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: "pisell2-arrow-left" })), /* @__PURE__ */ import_react.default.createElement("div", { className: "register-step-content" }, /* @__PURE__ */ import_react.default.createElement("h2", { className: "register-step-title" }, import_utils.locales.getText("pisell-forgot-password-title")), /* @__PURE__ */ import_react.default.createElement("p", { className: "register-step-desc" }, accountType === "email" ? import_utils.locales.getText("pisell-forgot-password-desc") : import_utils.locales.getText("pisell-forgot-password-desc-phone")), /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, layout: "vertical", size: "large", initialValues: { account: initialEmail } }, /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { name: "account", rules: accountRules, initialValue: initialEmail, style: { marginBottom: "12px" } }, accountType === "email" ? /* @__PURE__ */ import_react.default.createElement(
      import_materials.Input,
      {
        size: "large",
        placeholder: accountPlaceholder,
        className: "login2-input"
      }
    ) : /* @__PURE__ */ import_react.default.createElement(
      import_PhoneInput.default,
      {
        size: "large",
        placeholder: accountPlaceholder,
        className: "login2-input",
        countries,
        valueFormat: "object"
      }
    )), /* @__PURE__ */ import_react.default.createElement(
      import_materials.Button,
      {
        type: "primary",
        size: "large",
        block: true,
        loading,
        onClick: handleSendCode,
        className: "register-submit-btn"
      },
      import_utils.locales.getText("pisell-forgot-password-send-code")
    ), accountType === "email" && /* @__PURE__ */ import_react.default.createElement(
      import_materials.Button,
      {
        type: "primary",
        size: "large",
        block: true,
        loading,
        onClick: handleSendResetLink,
        className: "register-submit-btn",
        style: { marginTop: "12px" }
      },
      import_utils.locales.getText("pisell-forgot-password-send-link")
    ), /* @__PURE__ */ import_react.default.createElement(
      import_materials.Button,
      {
        type: "default",
        size: "large",
        block: true,
        onClick: onBack,
        className: "register-back-btn"
      },
      import_utils.locales.getText("pisell-login2-back")
    ))));
  }
  if (step === "input_code") {
    return /* @__PURE__ */ import_react.default.createElement(
      import_LoginCodeInput.default,
      {
        account,
        accountType,
        codeLength: 4,
        onSubmit: handleVerifyCode,
        onResend: () => onSendCode == null ? void 0 : onSendCode(account),
        onBack: handleBackStep,
        countdown,
        loading,
        submitButtonText: import_utils.locales.getText("pisell-forgot-password-verify")
      }
    );
  }
  if (step === "set_password") {
    return /* @__PURE__ */ import_react.default.createElement(
      import_SetPassword.default,
      {
        flowData: {
          currentStep: "set_password",
          accountType,
          account,
          verificationMethod: "email_code",
          verificationCode,
          password: "",
          extraData: {}
        },
        onSubmit: handleSetPassword,
        onBack: handleBackStep,
        loading
      }
    );
  }
  return null;
}
var ForgotPasswordFlow_default = ForgotPasswordFlow;
