'use strict';

var React3 = require('react');
var clsx = require('clsx');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React3__default = /*#__PURE__*/_interopDefault(React3);

// src/auth/components/LoginForm.tsx
function useAuth(apiClient) {
  const [user, setUser] = React3.useState(null);
  const [isLoggedIn, setIsLoggedIn] = React3.useState(false);
  const [loading, setLoading] = React3.useState(false);
  const [checkingAuth, setCheckingAuth] = React3.useState(true);
  const [error, setError] = React3.useState(null);
  const checkAuthStatus = React3.useCallback(async () => {
    try {
      setCheckingAuth(true);
      setError(null);
      const isAuth = await apiClient.isAuthenticated();
      if (isAuth) {
        const response = await apiClient.getCurrentUser();
        if (response.success && response.data) {
          setUser(response.data);
          setIsLoggedIn(true);
        } else {
          await apiClient.clearUserData();
          setUser(null);
          setIsLoggedIn(false);
        }
      } else {
        setUser(null);
        setIsLoggedIn(false);
      }
    } catch (err) {
      console.error("\u68C0\u67E5\u767B\u5F55\u72B6\u6001\u5931\u8D25:", err);
      setError(err instanceof Error ? err.message : "\u68C0\u67E5\u767B\u5F55\u72B6\u6001\u5931\u8D25");
      setUser(null);
      setIsLoggedIn(false);
    } finally {
      setCheckingAuth(false);
    }
  }, [apiClient]);
  const login = React3.useCallback(
    async (email, password) => {
      setLoading(true);
      setError(null);
      try {
        const response = await apiClient.login(email, password);
        if (response.success && response.data) {
          setUser(response.data.user);
          setIsLoggedIn(true);
          return { success: true };
        } else {
          const errorMsg = response.error || "\u767B\u5F55\u5931\u8D25";
          setError(errorMsg);
          return { success: false, error: errorMsg };
        }
      } catch (err) {
        const errorMsg = err instanceof Error ? err.message : "\u767B\u5F55\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5";
        setError(errorMsg);
        return { success: false, error: errorMsg };
      } finally {
        setLoading(false);
      }
    },
    [apiClient]
  );
  const register = React3.useCallback(
    async (email, password, username) => {
      setLoading(true);
      setError(null);
      try {
        const response = await apiClient.register(email, password, username);
        if (response.success && response.data) {
          setUser(response.data.user);
          setIsLoggedIn(true);
          return { success: true };
        } else {
          const errorMsg = response.error || "\u6CE8\u518C\u5931\u8D25";
          setError(errorMsg);
          return { success: false, error: errorMsg };
        }
      } catch (err) {
        const errorMsg = err instanceof Error ? err.message : "\u6CE8\u518C\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5";
        setError(errorMsg);
        return { success: false, error: errorMsg };
      } finally {
        setLoading(false);
      }
    },
    [apiClient]
  );
  const logout = React3.useCallback(async () => {
    setLoading(true);
    setError(null);
    try {
      await apiClient.logout();
      setUser(null);
      setIsLoggedIn(false);
    } catch (err) {
      console.error("\u767B\u51FA\u5931\u8D25:", err);
      setError(err instanceof Error ? err.message : "\u767B\u51FA\u5931\u8D25");
      setUser(null);
      setIsLoggedIn(false);
    } finally {
      setLoading(false);
    }
  }, [apiClient]);
  const clearError = React3.useCallback(() => {
    setError(null);
  }, []);
  React3.useEffect(() => {
    checkAuthStatus();
  }, [checkAuthStatus]);
  return {
    // 状态
    user,
    isLoggedIn,
    loading,
    checkingAuth,
    error,
    // 操作方法
    login,
    register,
    logout,
    refresh: checkAuthStatus,
    clearError
  };
}

// src/auth/components/LoginForm.tsx
function LoginForm({ apiClient, onSuccess, onError, children }) {
  const { login, loading, error: authError, clearError } = useAuth(apiClient);
  const [email, setEmail] = React3.useState("");
  const [password, setPassword] = React3.useState("");
  const handleSubmit = async (e) => {
    if (e) {
      e.preventDefault();
    }
    clearError();
    const result = await login(email, password);
    if (result.success) {
      const currentUser = apiClient.getUser();
      if (currentUser) {
        onSuccess?.(currentUser);
      }
    } else {
      onError?.(result.error || "\u767B\u5F55\u5931\u8D25");
    }
  };
  const state = {
    email,
    password,
    loading,
    error: authError,
    handleEmailChange: setEmail,
    handlePasswordChange: setPassword,
    handleSubmit
  };
  return /* @__PURE__ */ React3__default.default.createElement(React3__default.default.Fragment, null, children(state));
}
function RegisterForm({
  apiClient,
  onSuccess,
  onError,
  children
}) {
  const { register, loading, error: authError, clearError } = useAuth(apiClient);
  const [email, setEmail] = React3.useState("");
  const [password, setPassword] = React3.useState("");
  const [username, setUsername] = React3.useState("");
  const handleSubmit = async (e) => {
    if (e) {
      e.preventDefault();
    }
    clearError();
    if (!email || !password || !username) {
      const errorMsg = "\u8BF7\u586B\u5199\u6240\u6709\u5B57\u6BB5";
      onError?.(errorMsg);
      return;
    }
    if (password.length < 6) {
      const errorMsg = "\u5BC6\u7801\u957F\u5EA6\u81F3\u5C11\u4E3A 6 \u4F4D";
      onError?.(errorMsg);
      return;
    }
    const result = await register(email, password, username);
    if (result.success) {
      const currentUser = apiClient.getUser();
      if (currentUser) {
        onSuccess?.(currentUser);
      }
    } else {
      onError?.(result.error || "\u6CE8\u518C\u5931\u8D25");
    }
  };
  const state = {
    email,
    password,
    username,
    loading,
    error: authError,
    handleEmailChange: setEmail,
    handlePasswordChange: setPassword,
    handleUsernameChange: setUsername,
    handleSubmit
  };
  return /* @__PURE__ */ React3__default.default.createElement(React3__default.default.Fragment, null, children(state));
}
var DefaultContainer = ({ children }) => /* @__PURE__ */ React3__default.default.createElement("div", { className: "min-h-screen bg-gradient-to-br from-indigo-100 via-purple-50 to-pink-100 flex items-center justify-center px-4 py-12 relative overflow-hidden" }, /* @__PURE__ */ React3__default.default.createElement("div", { className: "absolute inset-0 overflow-hidden pointer-events-none" }, /* @__PURE__ */ React3__default.default.createElement(
  "div",
  {
    className: "absolute -top-40 -right-40 w-80 h-80 bg-purple-300 rounded-full mix-blend-multiply filter blur-xl opacity-70",
    style: {
      animation: "blob 7s infinite"
    }
  }
), /* @__PURE__ */ React3__default.default.createElement(
  "div",
  {
    className: "absolute -bottom-40 -left-40 w-80 h-80 bg-indigo-300 rounded-full mix-blend-multiply filter blur-xl opacity-70",
    style: {
      animation: "blob 7s infinite",
      animationDelay: "2s"
    }
  }
), /* @__PURE__ */ React3__default.default.createElement(
  "div",
  {
    className: "absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-80 h-80 bg-pink-300 rounded-full mix-blend-multiply filter blur-xl opacity-70",
    style: {
      animation: "blob 7s infinite",
      animationDelay: "4s"
    }
  }
)), /* @__PURE__ */ React3__default.default.createElement("div", { className: "relative z-[10]" }, children), /* @__PURE__ */ React3__default.default.createElement("style", { dangerouslySetInnerHTML: { __html: `
      @keyframes blob {
        0%, 100% { transform: translate(0, 0) scale(1); }
        25% { transform: translate(20px, -50px) scale(1.1); }
        50% { transform: translate(-20px, 20px) scale(0.9); }
        75% { transform: translate(50px, 50px) scale(1.05); }
      }
    ` } }));
var DefaultCard = ({ children }) => /* @__PURE__ */ React3__default.default.createElement("div", { className: "w-full max-w-md bg-white/95 backdrop-blur-xl rounded-2xl shadow-2xl border border-white/20 overflow-hidden" }, children);
var DefaultInput = (props) => /* @__PURE__ */ React3__default.default.createElement(
  "input",
  {
    ...props,
    className: "w-full px-4 py-3 bg-gray-50 border border-gray-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent transition-all duration-200 placeholder-gray-400 text-gray-900 hover:border-gray-300"
  }
);
var DefaultButton = ({ children, disabled, type, ...props }) => /* @__PURE__ */ React3__default.default.createElement(
  "button",
  {
    type: type || "button",
    disabled,
    ...props,
    className: clsx.clsx("w-full bg-gradient-to-r from-blue-50 to-indigo-50 border-2 border-dashed border-blue-300 text-blue-700 hover:from-blue-100 hover:to-indigo-100 hover:border-blue-400 px-4 py-2.5 rounded-xl text-sm font-medium transition-all duration-200 transform hover:scale-105 active:scale-95", disabled ? "opacity-60 cursor-not-allowed hover:scale-100 hover:shadow-none" : "block", "}"),
    style: {
      backgroundSize: "200% 100%",
      backgroundPosition: disabled ? "0% 0%" : "0% 0%",
      display: "block"
    },
    onMouseEnter: (e) => {
      if (!disabled) {
        e.currentTarget.style.backgroundPosition = "100% 0%";
      }
    },
    onMouseLeave: (e) => {
      if (!disabled) {
        e.currentTarget.style.backgroundPosition = "0% 0%";
      }
    }
  },
  children
);
var DefaultAlert = ({ children }) => /* @__PURE__ */ React3__default.default.createElement("div", { className: "bg-red-50 border-l-4 border-red-500 text-red-700 px-4 py-3 rounded-lg relative flex items-start space-x-3 shadow-sm" }, /* @__PURE__ */ React3__default.default.createElement("svg", { className: "w-5 h-5 text-red-500 mt-0.5 flex-shrink-0", fill: "currentColor", viewBox: "0 0 20 20" }, /* @__PURE__ */ React3__default.default.createElement("path", { fillRule: "evenodd", d: "M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z", clipRule: "evenodd" })), /* @__PURE__ */ React3__default.default.createElement("div", { className: "flex-1 text-sm" }, children));
var DefaultBadge = ({
  children,
  variant,
  className = ""
}) => /* @__PURE__ */ React3__default.default.createElement(
  "span",
  {
    className: clsx.clsx("inline-flex items-center px-3 py-1.5 rounded-full text-xs font-semibold shadow-sm", variant === "dev" ? "bg-gradient-to-r from-orange-400 to-amber-500 text-white border-0" : "bg-gray-100 text-gray-700 border border-gray-200", className)
  },
  children
);
function AdminLoginPage(props) {
  const {
    apiClient,
    onLoginSuccess,
    onLoginError,
    checkPermission,
    permissionDeniedMessage = "\u8BBF\u95EE\u88AB\u62D2\u7EDD\uFF1A\u9700\u8981\u7BA1\u7406\u5458\u6743\u9650",
    devConfig,
    texts = {},
    analytics,
    components,
    classNames = {}
  } = props;
  const Container = components?.Container || DefaultContainer;
  const Card = components?.Card || DefaultCard;
  const Input = components?.Input || DefaultInput;
  const Button = components?.Button || DefaultButton;
  const Alert = components?.Alert || DefaultAlert;
  components?.Badge || DefaultBadge;
  const t = {
    appName: texts.appName || "LyricNote",
    appDescription: texts.appDescription || "\u667A\u80FD\u6B4C\u8BCD\u7BA1\u7406\u5E73\u53F0",
    pageTitle: texts.pageTitle || "\u7BA1\u7406\u5458\u767B\u5F55",
    pageSubtitle: texts.pageSubtitle || "\u8BF7\u4F7F\u7528\u7BA1\u7406\u5458\u8D26\u6237\u767B\u5F55\u7CFB\u7EDF",
    emailLabel: texts.emailLabel || "\u7BA1\u7406\u5458\u90AE\u7BB1",
    emailPlaceholder: texts.emailPlaceholder || "admin@example.com",
    passwordLabel: texts.passwordLabel || "\u5BC6\u7801",
    passwordPlaceholder: texts.passwordPlaceholder || "\u8BF7\u8F93\u5165\u5BC6\u7801",
    loginButton: texts.loginButton || "\u767B\u5F55\u7BA1\u7406\u540E\u53F0",
    loggingInButton: texts.loggingInButton || "\u767B\u5F55\u4E2D...",
    fillTestAccount: texts.fillTestAccount || "\u586B\u5145\u6D4B\u8BD5\u8D26\u6237",
    copyCredentials: texts.copyCredentials || "\u590D\u5236",
    devModeLabel: texts.devModeLabel || "\u5F00\u53D1\u73AF\u5883",
    testAccountInfo: texts.testAccountInfo || "\u6D4B\u8BD5\u8D26\u6237\u5DF2\u586B\u5145\uFF0C\u53EF\u76F4\u63A5\u767B\u5F55",
    testAccountFilled: texts.testAccountFilled || "\u5DF2\u81EA\u52A8\u586B\u5145\u6D4B\u8BD5\u8D26\u6237\u4FE1\u606F",
    footer: texts.footer || "\u4EC5\u9650\u7BA1\u7406\u5458\u8BBF\u95EE \u2022 \u7CFB\u7EDF\u5B89\u5168\u4FDD\u62A4"
  };
  const [email, setEmail] = React3.useState("");
  const [password, setPassword] = React3.useState("");
  const [showPassword, setShowPassword] = React3.useState(false);
  const [autoFilled, setAutoFilled] = React3.useState(false);
  const [localError, setLocalError] = React3.useState("");
  const isDevelopment = devConfig?.enabled ?? (typeof window !== "undefined" && (process.env.NODE_ENV === "development" || window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1"));
  const testEmail = devConfig?.testEmail || "admin@example.com";
  const testPassword = devConfig?.testPassword || "admin123";
  const autoFillDelay = devConfig?.autoFillDelay || 2e3;
  const { user, isLoggedIn, loading, error: authError, login, clearError } = useAuth(apiClient);
  const error = authError || localError;
  React3.useEffect(() => {
    if (typeof window !== "undefined" && analytics?.trackPageView) {
      analytics.trackPageView({
        pageName: "admin_login_page",
        pageUrl: window.location.pathname
      });
    }
  }, [analytics]);
  React3.useEffect(() => {
    if (isDevelopment && !email && !password) {
      const timer = setTimeout(() => {
        if (!email && !password) {
          setEmail(testEmail);
          setPassword(testPassword);
          setAutoFilled(true);
        }
      }, autoFillDelay);
      return () => clearTimeout(timer);
    }
    return;
  }, [isDevelopment, email, password, testEmail, testPassword, autoFillDelay]);
  React3.useEffect(() => {
    if (user && isLoggedIn) {
      if (checkPermission && !checkPermission(user)) {
        setLocalError(permissionDeniedMessage);
        analytics?.trackPermissionDenied?.(user);
        return;
      }
      analytics?.trackLoginSuccess?.(user);
      analytics?.setUser?.({
        userId: user.id,
        email: user.email,
        role: user.role
      });
      onLoginSuccess?.(user);
    }
  }, [user, isLoggedIn, checkPermission, permissionDeniedMessage, analytics, onLoginSuccess]);
  const handleSubmit = async (e) => {
    e.preventDefault();
    setLocalError("");
    clearError();
    try {
      const result = await login(email, password);
      if (!result.success) {
        const errorMsg = result.error || "\u767B\u5F55\u5931\u8D25";
        setLocalError(errorMsg);
        analytics?.trackLoginFailed?.(errorMsg, email);
        onLoginError?.(errorMsg);
      }
    } catch (err) {
      const errorMsg = "\u767B\u5F55\u5931\u8D25\uFF1A\u7F51\u7EDC\u9519\u8BEF\u6216\u670D\u52A1\u5668\u9519\u8BEF";
      setLocalError(errorMsg);
      analytics?.trackLoginFailed?.(errorMsg, email);
      onLoginError?.(errorMsg);
    }
  };
  const fillTestAccount = () => {
    setEmail(testEmail);
    setPassword(testPassword);
    setLocalError("");
    setAutoFilled(true);
  };
  return /* @__PURE__ */ React3__default.default.createElement(Container, null, /* @__PURE__ */ React3__default.default.createElement("div", { className: "w-full max-w-md" }, /* @__PURE__ */ React3__default.default.createElement(Card, null, /* @__PURE__ */ React3__default.default.createElement("div", { className: "p-8" }, /* @__PURE__ */ React3__default.default.createElement("div", { className: "pb-8" }, /* @__PURE__ */ React3__default.default.createElement("h2", { className: "text-2xl font-bold text-center text-gray-800" }, t.pageTitle)), /* @__PURE__ */ React3__default.default.createElement("form", { onSubmit: handleSubmit, className: "space-y-5" }, error && /* @__PURE__ */ React3__default.default.createElement(Alert, null, error), /* @__PURE__ */ React3__default.default.createElement("div", { className: "space-y-2" }, /* @__PURE__ */ React3__default.default.createElement("label", { htmlFor: "email", className: "text-sm font-semibold text-gray-700 block" }, t.emailLabel), /* @__PURE__ */ React3__default.default.createElement(
    Input,
    {
      id: "email",
      type: "email",
      value: email,
      onChange: (e) => setEmail(e.target.value),
      placeholder: t.emailPlaceholder,
      required: true,
      disabled: loading
    }
  )), /* @__PURE__ */ React3__default.default.createElement("div", { className: "space-y-2" }, /* @__PURE__ */ React3__default.default.createElement("label", { htmlFor: "password", className: "text-sm font-semibold text-gray-700 block" }, t.passwordLabel), /* @__PURE__ */ React3__default.default.createElement("div", { className: "relative" }, /* @__PURE__ */ React3__default.default.createElement(
    Input,
    {
      id: "password",
      type: showPassword ? "text" : "password",
      value: password,
      onChange: (e) => setPassword(e.target.value),
      placeholder: t.passwordPlaceholder,
      required: true,
      disabled: loading
    }
  ), /* @__PURE__ */ React3__default.default.createElement(
    "button",
    {
      type: "button",
      onClick: () => setShowPassword(!showPassword),
      className: "absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600 transition-colors p-1",
      disabled: loading,
      title: showPassword ? "\u9690\u85CF\u5BC6\u7801" : "\u663E\u793A\u5BC6\u7801"
    },
    showPassword ? /* @__PURE__ */ React3__default.default.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", className: "h-5 w-5", viewBox: "0 0 20 20", fill: "currentColor" }, /* @__PURE__ */ React3__default.default.createElement("path", { fillRule: "evenodd", d: "M3.707 2.293a1 1 0 00-1.414 1.414l14 14a1 1 0 001.414-1.414l-1.473-1.473A10.014 10.014 0 0019.542 10C18.268 5.943 14.478 3 10 3a9.958 9.958 0 00-4.512 1.074l-1.78-1.781zm4.261 4.26l1.514 1.515a2.003 2.003 0 012.45 2.45l1.514 1.514a4 4 0 00-5.478-5.478z", clipRule: "evenodd" }), /* @__PURE__ */ React3__default.default.createElement("path", { d: "M12.454 16.697L9.75 13.992a4 4 0 01-3.742-3.741L2.335 6.578A9.98 9.98 0 00.458 10c1.274 4.057 5.065 7 9.542 7 .847 0 1.669-.105 2.454-.303z" })) : /* @__PURE__ */ React3__default.default.createElement("svg", { xmlns: "http://www.w3.org/2000/svg", className: "h-5 w-5", viewBox: "0 0 20 20", fill: "currentColor" }, /* @__PURE__ */ React3__default.default.createElement("path", { d: "M10 12a2 2 0 100-4 2 2 0 000 4z" }), /* @__PURE__ */ React3__default.default.createElement("path", { fillRule: "evenodd", d: "M.458 10C1.732 5.943 5.522 3 10 3s8.268 2.943 9.542 7c-1.274 4.057-5.064 7-9.542 7S1.732 14.057.458 10zM14 10a4 4 0 11-8 0 4 4 0 018 0z", clipRule: "evenodd" }))
  ))), isDevelopment && /* @__PURE__ */ React3__default.default.createElement("div", { className: "pt-2" }, /* @__PURE__ */ React3__default.default.createElement(
    "button",
    {
      type: "button",
      onClick: fillTestAccount,
      className: "w-full bg-gradient-to-r from-blue-50 to-indigo-50 border-2 border-dashed border-blue-300 text-blue-700 hover:from-blue-100 hover:to-indigo-100 hover:border-blue-400 px-4 py-2.5 rounded-xl text-sm font-medium transition-all duration-200 transform hover:scale-105 active:scale-95",
      disabled: loading
    },
    t.fillTestAccount
  )), /* @__PURE__ */ React3__default.default.createElement("div", { className: "pt-3" }, /* @__PURE__ */ React3__default.default.createElement(Button, { type: "submit", disabled: loading }, loading ? t.loggingInButton : t.loginButton)))))));
}

exports.AdminLoginPage = AdminLoginPage;
exports.LoginForm = LoginForm;
exports.RegisterForm = RegisterForm;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map