'use strict';

var react = require('react');

// src/auth/hooks/useAuth.ts
function useAuth(apiClient) {
  const [user, setUser] = react.useState(null);
  const [isLoggedIn, setIsLoggedIn] = react.useState(false);
  const [loading, setLoading] = react.useState(false);
  const [checkingAuth, setCheckingAuth] = react.useState(true);
  const [error, setError] = react.useState(null);
  const checkAuthStatus = react.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 = react.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 = react.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 = react.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 = react.useCallback(() => {
    setError(null);
  }, []);
  react.useEffect(() => {
    checkAuthStatus();
  }, [checkAuthStatus]);
  return {
    // 状态
    user,
    isLoggedIn,
    loading,
    checkingAuth,
    error,
    // 操作方法
    login,
    register,
    logout,
    refresh: checkAuthStatus,
    clearError
  };
}
function useAuthForm(initialValues) {
  const [values, setValues] = react.useState(initialValues);
  const [errors, setErrors] = react.useState({});
  const [touched, setTouched] = react.useState({});
  const handleChange = react.useCallback(
    (field, value) => {
      setValues((prev) => ({ ...prev, [field]: value }));
      if (errors[field]) {
        setErrors((prev) => {
          const newErrors = { ...prev };
          delete newErrors[field];
          return newErrors;
        });
      }
    },
    [errors]
  );
  const handleBlur = react.useCallback((field) => {
    setTouched((prev) => ({ ...prev, [field]: true }));
  }, []);
  const validate = react.useCallback(
    (validationRules) => {
      const newErrors = {};
      Object.keys(validationRules).forEach((key) => {
        const field = key;
        const rule = validationRules[field];
        if (rule) {
          const error = rule(values[field]);
          if (error) {
            newErrors[field] = error;
          }
        }
      });
      setErrors(newErrors);
      return Object.keys(newErrors).length === 0;
    },
    [values]
  );
  const reset = react.useCallback(() => {
    setValues(initialValues);
    setErrors({});
    setTouched({});
  }, [initialValues]);
  return {
    values,
    errors,
    touched,
    handleChange,
    handleBlur,
    validate,
    reset,
    setValues,
    setErrors
  };
}

exports.useAuth = useAuth;
exports.useAuthForm = useAuthForm;
//# sourceMappingURL=chunk-ROEYW4A7.js.map
//# sourceMappingURL=chunk-ROEYW4A7.js.map