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/AuthModal.tsx
var AuthModal_exports = {};
__export(AuthModal_exports, {
  default: () => AuthModal_default
});
module.exports = __toCommonJS(AuthModal_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_locales = __toESM(require("./locales"));
var import_Login2 = __toESM(require("./Login2"));
var import_Register2 = __toESM(require("./Register2"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var AuthModal = () => {
  var _a;
  const context = (0, import_useEngineContext.default)();
  const engineProps = ((_a = (0, import_useEngineContext.default)().engine) == null ? void 0 : _a.props) || {};
  const {
    config,
    channel,
    modal = false,
    visible = true,
    onClose,
    className,
    style,
    defaultView = "login",
    emailLinkVerification,
    onLoginSuccess,
    onRegisterSuccess,
    onOAuthLogin,
    onSendVerificationCode,
    onForgotPassword,
    onEmailLinkExpired,
    onError
  } = engineProps;
  const [currentView, setCurrentView] = (0, import_react.useState)(
    config.isEmailLinkRegister ? "register" : defaultView
  );
  const [localesInitialized, setLocalesInitialized] = (0, import_react.useState)(false);
  (0, import_react.useEffect)(() => {
    var _a2, _b, _c, _d, _e, _f, _g;
    import_utils.locales.init(
      import_locales.default,
      ((_e = (_d = (_c = (_b = (_a2 = context.appHelper.utils) == null ? void 0 : _a2.getApp) == null ? void 0 : _b.call(_a2)) == null ? void 0 : _c.locales) == null ? void 0 : _d.getLocale) == null ? void 0 : _e.call(_d)) || ((_g = (_f = context.appHelper.utils) == null ? void 0 : _f.storage) == null ? void 0 : _g.get("umi_locale")) || "en"
    );
    setLocalesInitialized(true);
  }, []);
  const getModalTitle = (0, import_react.useCallback)(() => {
    if (currentView === "login") {
      return import_utils.locales.getText("pisell-modal-login-title");
    } else {
      return import_utils.locales.getText("pisell-modal-register-title");
    }
  }, [currentView]);
  const login2Config = (0, import_react.useMemo)(() => {
    var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
    if (!localesInitialized) {
      return {
        loginMethods: [],
        channel,
        emailLinkVerification,
        legalTerms: { enabled: false, terms: [] },
        ui: {
          title: { show: false, text: "", align: "center" },
          themeColor: "#7F56D9",
          formLabels: {
            email: { show: false },
            phone: { show: false },
            password: { show: false },
            verificationCode: { show: false }
          }
        }
      };
    }
    const loginMethods = [];
    const authMethods = ((_a2 = config == null ? void 0 : config.auth) == null ? void 0 : _a2.method) || {};
    const enabledMethods = [];
    if ((_b = authMethods.email) == null ? void 0 : _b.enabled) {
      enabledMethods.push({
        type: "email",
        sort: authMethods.email.sort || 0,
        // 添加默认值保护
        config: authMethods.email
      });
    }
    if ((_c = authMethods.phone) == null ? void 0 : _c.enabled) {
      enabledMethods.push({
        type: "phone",
        sort: authMethods.phone.sort || 0,
        // 添加默认值保护
        config: authMethods.phone
      });
    }
    if ((_d = authMethods.google) == null ? void 0 : _d.enabled) {
      enabledMethods.push({
        type: "google",
        sort: authMethods.google.sort || 0,
        // 添加默认值保护
        config: authMethods.google
      });
    }
    if ((_e = authMethods.facebook) == null ? void 0 : _e.enabled) {
      enabledMethods.push({
        type: "facebook",
        sort: authMethods.facebook.sort || 0,
        // 添加默认值保护
        config: authMethods.facebook
      });
    }
    if ((_f = authMethods.apple) == null ? void 0 : _f.enabled) {
      enabledMethods.push({
        type: "apple",
        sort: authMethods.apple.sort || 0,
        // 添加默认值保护
        config: authMethods.apple
      });
    }
    if ((_g = authMethods.guest) == null ? void 0 : _g.enabled) {
      enabledMethods.push({
        type: "guest",
        sort: authMethods.guest.sort || 0,
        // 添加默认值保护
        config: authMethods.guest
      });
    }
    enabledMethods.sort((a, b) => a.sort - b.sort);
    enabledMethods.forEach(({ type, config: methodConfig }) => {
      var _a3, _b2, _c2, _d2, _e2, _f2, _g2, _h2, _i2, _j2;
      if (type === "email") {
        const verificationMethods = [];
        const loginMethod = ((_a3 = methodConfig == null ? void 0 : methodConfig.setting) == null ? void 0 : _a3.login_method) || {
          "password": {
            enabled: true,
            sort: 1
          }
        };
        const enabledVerificationMethods = [];
        if ((_b2 = loginMethod == null ? void 0 : loginMethod.password) == null ? void 0 : _b2.enabled) {
          enabledVerificationMethods.push({
            type: "password",
            sort: ((_c2 = loginMethod.password) == null ? void 0 : _c2.sort) || 0
            // 添加默认值保护
          });
        }
        if ((_d2 = loginMethod == null ? void 0 : loginMethod.code) == null ? void 0 : _d2.enabled) {
          enabledVerificationMethods.push({
            type: "verification_code",
            sort: loginMethod.code.sort || 0
            // 添加默认值保护
          });
        }
        if ((_e2 = loginMethod == null ? void 0 : loginMethod.link) == null ? void 0 : _e2.enabled) {
          enabledVerificationMethods.push({
            type: "verification_code",
            // link 映射为 verification_code
            sort: loginMethod.link.sort || 0
            // 添加默认值保护
          });
        }
        enabledVerificationMethods.sort((a, b) => a.sort - b.sort);
        verificationMethods.push(...enabledVerificationMethods.map((v) => v.type));
        if (verificationMethods.length > 0) {
          loginMethods.push({
            type: "email",
            verificationMethods,
            stepByStep: true
          });
        }
      } else if (type === "phone") {
        const verificationMethods = [];
        const loginMethod = ((_f2 = methodConfig == null ? void 0 : methodConfig.setting) == null ? void 0 : _f2.login_method) || {};
        const enabledVerificationMethods = [];
        if ((_g2 = loginMethod == null ? void 0 : loginMethod.password) == null ? void 0 : _g2.enabled) {
          enabledVerificationMethods.push({
            type: "password",
            sort: loginMethod.password.sort || 0
            // 添加默认值保护
          });
        }
        if ((_h2 = loginMethod == null ? void 0 : loginMethod.code) == null ? void 0 : _h2.enabled) {
          enabledVerificationMethods.push({
            type: "verification_code",
            sort: loginMethod.code.sort || 0
            // 添加默认值保护
          });
        }
        enabledVerificationMethods.sort((a, b) => a.sort - b.sort);
        verificationMethods.push(...enabledVerificationMethods.map((v) => v.type));
        if (verificationMethods.length > 0) {
          loginMethods.push({
            type: "phone",
            verificationMethods,
            stepByStep: true
          });
        }
      } else if (type === "google") {
        loginMethods.push({
          type: "google"
        });
      } else if (type === "facebook") {
        loginMethods.push({
          type: "facebook",
          appId: ((_i2 = methodConfig == null ? void 0 : methodConfig.setting) == null ? void 0 : _i2.appid) || ""
        });
      } else if (type === "apple") {
        loginMethods.push({
          type: "apple",
          clientId: ((_j2 = methodConfig == null ? void 0 : methodConfig.setting) == null ? void 0 : _j2.identifier) || ""
        });
      } else if (type === "guest") {
        loginMethods.push({
          type: "guest"
        });
      }
    });
    return {
      loginMethods,
      channel,
      emailLinkVerification: config.isForGetPasswordLink ? {
        enabled: true,
        code: config.linkCode
      } : emailLinkVerification,
      legalTerms: {
        enabled: ((_i = (_h = config == null ? void 0 : config.page) == null ? void 0 : _h.login) == null ? void 0 : _i.same_as_register) ? ((_l = (_k = (_j = config == null ? void 0 : config.page) == null ? void 0 : _j.register) == null ? void 0 : _k.terms) == null ? void 0 : _l.length) > 0 : ((_o = (_n = (_m = config == null ? void 0 : config.page) == null ? void 0 : _m.login) == null ? void 0 : _n.terms) == null ? void 0 : _o.length) > 0,
        terms: ((_q = (_p = config == null ? void 0 : config.page) == null ? void 0 : _p.login) == null ? void 0 : _q.same_as_register) ? (_s = (_r = config == null ? void 0 : config.page) == null ? void 0 : _r.register) == null ? void 0 : _s.terms : (_u = (_t = config == null ? void 0 : config.page) == null ? void 0 : _t.login) == null ? void 0 : _u.terms
      },
      ui: {
        title: {
          show: true,
          text: import_utils.locales.getText("pisell-modal-login-title"),
          align: "center"
        },
        logo: (config == null ? void 0 : config.logo) ? {
          show: (config == null ? void 0 : config.logo) ? true : false,
          url: config == null ? void 0 : config.logo,
          // 直接从 config.logo 读取
          position: "center",
          size: {
            width: ((_v = config == null ? void 0 : config.logoSize) == null ? void 0 : _v.width) || 48,
            height: ((_w = config == null ? void 0 : config.logoSize) == null ? void 0 : _w.height) || 48
          }
        } : void 0,
        themeColor: ((_z = (_y = (_x = config == null ? void 0 : config.page) == null ? void 0 : _x.login) == null ? void 0 : _y.ui) == null ? void 0 : _z.themeColor) || "#7F56D9",
        formLabels: {
          email: { show: false },
          phone: { show: false },
          password: { show: false },
          verificationCode: { show: false }
        }
      }
    };
  }, [config, channel, emailLinkVerification, localesInitialized]);
  const register2Config = (0, import_react.useMemo)(() => {
    var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
    if (!localesInitialized) {
      return {
        registrationMethods: [],
        channel,
        emailLinkVerification,
        legalTerms: { enabled: false, terms: [] },
        ui: {
          title: { show: false, text: "", align: "center" },
          themeColor: "#7F56D9",
          formLabels: {
            email: { show: false },
            phone: { show: false },
            password: { show: false },
            verificationCode: { show: false }
          }
        }
      };
    }
    const registrationMethods = [];
    const authMethods = ((_a2 = config.auth) == null ? void 0 : _a2.method) || {};
    const enabledMethods = [];
    if (((_b = authMethods.email) == null ? void 0 : _b.enabled) && authMethods.email.setting.register_enabled) {
      enabledMethods.push({
        type: "email",
        sort: authMethods.email.sort,
        config: authMethods.email
      });
    }
    if (((_c = authMethods.phone) == null ? void 0 : _c.enabled) && authMethods.phone.setting.register_enabled) {
      enabledMethods.push({
        type: "phone",
        sort: authMethods.phone.sort,
        config: authMethods.phone
      });
    }
    if ((_d = authMethods.google) == null ? void 0 : _d.enabled) {
      enabledMethods.push({
        type: "google",
        sort: authMethods.google.sort,
        config: authMethods.google
      });
    }
    if ((_e = authMethods.facebook) == null ? void 0 : _e.enabled) {
      enabledMethods.push({
        type: "facebook",
        sort: authMethods.facebook.sort,
        config: authMethods.facebook
      });
    }
    if ((_f = authMethods.apple) == null ? void 0 : _f.enabled) {
      enabledMethods.push({
        type: "apple",
        sort: authMethods.apple.sort,
        config: authMethods.apple
      });
    }
    enabledMethods.sort((a, b) => a.sort - b.sort);
    enabledMethods.forEach(({ type, config: methodConfig }) => {
      var _a3, _b2;
      if (type === "email") {
        const verificationMethod = methodConfig.setting.register_method === "link" ? "verification_link" : "verification_code";
        registrationMethods.push({
          type: "email",
          verificationMethod
        });
      } else if (type === "phone") {
        registrationMethods.push({
          type: "phone",
          verificationMethod: "verification_code"
        });
      } else if (type === "google") {
        registrationMethods.push({
          type: "google"
        });
      } else if (type === "facebook") {
        registrationMethods.push({
          type: "facebook",
          appId: ((_a3 = methodConfig == null ? void 0 : methodConfig.setting) == null ? void 0 : _a3.appid) || ""
        });
      } else if (type === "apple") {
        registrationMethods.push({
          type: "apple",
          clientId: ((_b2 = methodConfig == null ? void 0 : methodConfig.setting) == null ? void 0 : _b2.identifier) || ""
        });
      }
    });
    const transformPasswordRules = (passwordConfig) => {
      if (!passwordConfig) {
        return void 0;
      }
      return {
        minLength: passwordConfig.min_length,
        requireLowercase: passwordConfig.require_lowercase_letters,
        requireNumbers: passwordConfig.require_numbers,
        requireSpecialCharacters: passwordConfig.require_special_characters,
        requireUppercase: passwordConfig.require_uppercase_letters
      };
    };
    return {
      registrationMethods,
      channel,
      emailLinkVerification: config.isEmailLinkRegister ? {
        enabled: true,
        code: config.linkCode
      } : emailLinkVerification,
      legalTerms: {
        enabled: ((_i = (_h = (_g = config.page) == null ? void 0 : _g.register) == null ? void 0 : _h.terms) == null ? void 0 : _i.length) > 0,
        terms: (_k = (_j = config.page) == null ? void 0 : _j.register) == null ? void 0 : _k.terms
      },
      ui: {
        title: {
          show: true,
          text: import_utils.locales.getText("pisell-modal-register-title"),
          align: "center"
        },
        logo: config.logo ? {
          show: config.logo ? true : false,
          url: config.logo,
          // 直接从 config.logo 读取
          position: "center",
          size: {
            width: ((_l = config == null ? void 0 : config.logoSize) == null ? void 0 : _l.width) || 48,
            height: ((_m = config == null ? void 0 : config.logoSize) == null ? void 0 : _m.height) || 48
          }
        } : void 0,
        themeColor: ((_p = (_o = (_n = config.page) == null ? void 0 : _n.register) == null ? void 0 : _o.ui) == null ? void 0 : _p.themeColor) || "#7F56D9",
        passwordRules: transformPasswordRules((_q = config.auth) == null ? void 0 : _q.password),
        // 传递密码规则
        formLabels: {
          email: { show: false },
          phone: { show: false },
          password: { show: false },
          verificationCode: { show: false }
        }
      }
    };
  }, [config, channel, emailLinkVerification, localesInitialized]);
  const handleGoToRegister = (0, import_react.useCallback)(() => {
    setCurrentView("register");
  }, []);
  const handleGoToLogin = (0, import_react.useCallback)(() => {
    setCurrentView("login");
  }, []);
  const handleError = (0, import_react.useCallback)((error, context2) => {
    if (onError) {
      onError(error, context2);
    }
  }, [onError]);
  const login2Events = {
    onLogin: async (data, method, loginData, channel2) => {
      await (onLoginSuccess == null ? void 0 : onLoginSuccess(data, method, channel2));
      if (modal && onClose) {
        onClose();
      }
    },
    onOAuthLogin,
    onSendVerificationCode,
    onForgotPassword,
    onSwitchToRegister: handleGoToRegister,
    onError: (error, method) => {
      handleError(error, {
        action: "login",
        method,
        channel
      });
    }
  };
  const register2Events = {
    onRegister: async (_, method, data, channel2) => {
      await (onRegisterSuccess == null ? void 0 : onRegisterSuccess(_, method, channel2));
      await (onLoginSuccess == null ? void 0 : onLoginSuccess(_, method, channel2));
      if (modal && onClose) {
        onClose();
      }
    },
    onOAuthLogin,
    onSendVerificationCode,
    onEmailLinkExpired,
    onGoToLogin: handleGoToLogin,
    onError: (error, method) => {
      handleError(error, {
        action: "register",
        method,
        channel
      });
    }
  };
  const renderContent = () => {
    if (currentView === "login") {
      return /* @__PURE__ */ import_react.default.createElement(
        import_Login2.default,
        {
          config: login2Config,
          visible: true,
          onClose,
          ...login2Events
        }
      );
    } else {
      return /* @__PURE__ */ import_react.default.createElement(
        import_Register2.default,
        {
          config: register2Config,
          visible: true,
          onClose,
          ...register2Events
        }
      );
    }
  };
  if (modal) {
    return /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellModal,
      {
        open: visible,
        onCancel: onClose,
        header: false,
        footer: null,
        centered: true,
        className,
        destroyOnClose: true,
        platform: import_utils.isMobile ? "h5" : "pc"
      },
      renderContent()
    );
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { className, style }, visible && renderContent());
};
var AuthModal_default = AuthModal;
