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/plus/saasRegister/index.tsx
var saasRegister_exports = {};
__export(saasRegister_exports, {
  default: () => saasRegister_default
});
module.exports = __toCommonJS(saasRegister_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_Header = require("./components/Header");
var import_EntryEmail = __toESM(require("./components/EntryEmail"));
var import_GoLogin = __toESM(require("./components/GoLogin"));
var import_VertifyEmail = __toESM(require("./components/VertifyEmail"));
var import_VerifyingLink = __toESM(require("./components/VerifyingLink"));
var import_VerifyError = __toESM(require("./components/VerifyError"));
var import_EntryInfo = __toESM(require("./components/EntryInfo"));
var import_locales = __toESM(require("./locales"));
var import_types = require("./types");
var import_index = require("./index.less");
var SaasRegister = (props, ref) => {
  var _a, _b, _c;
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(import_locales.default, ((_b = (_a = context == null ? void 0 : context.engine) == null ? void 0 : _a.props) == null ? void 0 : _b.locale) || "en");
  const [form] = import_antd.Form.useForm();
  const [step, setStep] = (0, import_react.useState)({
    key: import_types.SRStepKey.ENTRY_EMAIL
  });
  const init = (emailToken) => {
    if (emailToken) {
      setStep({
        key: import_types.SRStepKey.VERIFYING_EMAIL,
        params: { emailToken }
      });
    }
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    init
  }));
  (0, import_react.useEffect)(() => {
    var _a2;
    (_a2 = props.onStepChange) == null ? void 0 : _a2.call(props, step);
  }, [step]);
  (0, import_react.useEffect)(() => {
    var _a2, _b2, _c2, _d, _e;
    if ((_a2 = step.params) == null ? void 0 : _a2.documentTitle) {
      document.title = (_b2 = step.params) == null ? void 0 : _b2.documentTitle;
    }
    if ((_c2 = step.params) == null ? void 0 : _c2.documentDescription) {
      const metaDescription = document.querySelector(
        'meta[name="description"]'
      );
      if (metaDescription) {
        metaDescription.setAttribute(
          "content",
          (_d = step.params) == null ? void 0 : _d.documentDescription
        );
      } else {
        const meta = document.createElement("meta");
        meta.name = "description";
        meta.content = (_e = step.params) == null ? void 0 : _e.documentDescription;
        document.head.appendChild(meta);
      }
    }
  }, [step]);
  const handleVerifyEmail = async (result) => {
    if (result) {
      setStep({
        key: import_types.SRStepKey.WAIT_VERIFY_EMAIL
      });
    } else {
      setStep({
        key: import_types.SRStepKey.VERIFY_FAILED,
        status: import_types.SRStepStatus.FAILED_EMAIL_REGISTERED
      });
    }
  };
  const handleEditEmail = () => {
    setStep({
      key: import_types.SRStepKey.ENTRY_EMAIL
    });
  };
  const handleSendEmail = async () => {
    var _a2;
    const email = form.getFieldValue("email");
    await ((_a2 = props.onSendEmail) == null ? void 0 : _a2.call(props, email));
  };
  const handleVerifyLink = async () => {
    var _a2, _b2;
    try {
      const emailToken = (_a2 = step.params) == null ? void 0 : _a2.emailToken;
      const result = await ((_b2 = props.onVerifyLink) == null ? void 0 : _b2.call(props, emailToken));
      console.log("result>>>>>>>", result);
      if ((result == null ? void 0 : result.code) === import_types.SRVerifyCode.SUCCESS) {
        setStep({
          key: import_types.SRStepKey.ENTRY_INFO,
          params: {
            emailToken,
            documentTitle: import_utils.locales.getText(
              "pisell-saas-register-entry-info-document-title"
            ),
            documentDescription: import_utils.locales.getText(
              "pisell-saas-register-entry-info-document-description"
            )
          }
        });
      }
    } catch (error) {
      if ((error == null ? void 0 : error.code) === import_types.SRVerifyCode.LINK_EXPIRED) {
        setStep({
          key: import_types.SRStepKey.VERIFY_FAILED,
          status: import_types.SRStepStatus.FAILED_LINK_EXPIRED
        });
      } else if ((error == null ? void 0 : error.code) === import_types.SRVerifyCode.EMAIL_REGISTERED) {
        setStep({
          key: import_types.SRStepKey.VERIFY_FAILED,
          status: import_types.SRStepStatus.FAILED_LINK_EMAIL_REGISTERED
        });
      } else {
        (0, import_materials.PisellToast)({
          content: error == null ? void 0 : error.message,
          duration: 2
        });
      }
      console.error("handleVerifyLink_failed:", error);
    }
  };
  const handleBackToRegister = () => {
    setStep({
      key: import_types.SRStepKey.ENTRY_EMAIL
    });
  };
  const customHeaderProps = (0, import_react.useMemo)(() => {
    if (step.key === import_types.SRStepKey.ENTRY_INFO) {
      return {
        showLogo: false,
        showWelcomeMessage: false,
        showTitle: true,
        title: import_utils.locales.getText("pisell-saas-register-entry-info-title"),
        showSubtitle: true,
        subtitle: import_utils.locales.getText("pisell-saas-register-entry-info-subtitle")
      };
    }
    return {};
  }, [step.key]);
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-saas-register-main" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-saas-register-container" }, /* @__PURE__ */ import_react.default.createElement(import_Header.Header, { ...props, ...customHeaderProps }), /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, preserve: true }, step.key === import_types.SRStepKey.ENTRY_EMAIL && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_EntryEmail.default,
    {
      ...props,
      form,
      handleVerifyEmail
    }
  ), /* @__PURE__ */ import_react.default.createElement(import_GoLogin.default, { ...props })), step.key === import_types.SRStepKey.WAIT_VERIFY_EMAIL && /* @__PURE__ */ import_react.default.createElement(
    import_VertifyEmail.default,
    {
      email: form.getFieldValue("email"),
      onSendEmail: handleSendEmail,
      onEditEmail: handleEditEmail
    }
  ), step.key === import_types.SRStepKey.VERIFYING_EMAIL && /* @__PURE__ */ import_react.default.createElement(import_VerifyingLink.default, { onVerifyLink: handleVerifyLink }), step.key === import_types.SRStepKey.VERIFY_FAILED && /* @__PURE__ */ import_react.default.createElement(
    import_VerifyError.default,
    {
      status: step.status,
      onBackToRegister: handleBackToRegister,
      onGoLogin: props == null ? void 0 : props.onGoLogin
    }
  ), step.key === import_types.SRStepKey.ENTRY_INFO && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_EntryInfo.default,
    {
      ...props,
      form,
      emailToken: (_c = step.params) == null ? void 0 : _c.emailToken
    }
  ), /* @__PURE__ */ import_react.default.createElement(import_GoLogin.default, { ...props })))));
};
var saasRegister_default = (0, import_react.forwardRef)(SaasRegister);
