"use client";
import { useRouter } from "next/navigation";
import { authClient } from "../lib/auth-client.js";
import { useOrigin } from "./useOrigin.js";
import { useLastAuthMethod } from "./useLastAuthMethod.js";
function useAuth() {
  var _a;
  const router = useRouter();
  const session = authClient.useSession();
  const origin = useOrigin();
  const { saveAuthMethod } = useLastAuthMethod();
  const handleSignIn = async ({ email, password, redirectTo }) => {
    const { data, error } = await authClient.signIn.email({
      email,
      password
    });
    if (error) {
      throw new Error(error.message || "Error al iniciar sesi\xF3n");
    }
    if (data) {
      saveAuthMethod("email");
      router.push(redirectTo || "/dashboard");
    }
    return data;
  };
  const handleSignUp = async ({ email, password, firstName, lastName }) => {
    const { data, error } = await authClient.signUp.email({
      email,
      password,
      name: `${firstName || ""} ${lastName || ""}`.trim() || email.split("@")[0],
      firstName,
      lastName
    });
    if (error) {
      throw new Error(error.message || "Failed to create account");
    }
    return data;
  };
  const handleSignOut = async () => {
    if (typeof window !== "undefined") {
      localStorage.removeItem("activeTeamId");
    }
    await authClient.signOut();
    router.push("/login");
  };
  const handleGoogleSignIn = async (redirectTo) => {
    const callbackURL = redirectTo ? `${redirectTo}${redirectTo.includes("?") ? "&" : "?"}auth_method=google` : "/dashboard?auth_method=google";
    await authClient.signIn.social({
      provider: "google",
      callbackURL
    });
  };
  const handleResetPassword = async (email) => {
    try {
      const { data, error } = await authClient.requestPasswordReset({
        email,
        redirectTo: `${origin}/reset-password`
      });
      if (error) {
        return {
          success: false,
          data: void 0,
          error: error.message || "Failed to send password reset email"
        };
      }
      return {
        success: true,
        data,
        error: void 0
      };
    } catch (error) {
      return {
        success: false,
        data: void 0,
        error: error instanceof Error ? error.message : "An unexpected error occurred"
      };
    }
  };
  const handleUpdatePassword = async (newPassword, token) => {
    try {
      if (token) {
        const { data, error } = await authClient.resetPassword({
          newPassword,
          token
        });
        if (error) {
          return {
            success: false,
            data: void 0,
            error: error.message || "Failed to reset password"
          };
        }
        return {
          success: true,
          data,
          error: void 0
        };
      }
      return {
        success: false,
        data: void 0,
        error: "Password change for authenticated users not yet implemented"
      };
    } catch (error) {
      return {
        success: false,
        data: void 0,
        error: error instanceof Error ? error.message : "An unexpected error occurred"
      };
    }
  };
  const handleChangePassword = async (currentPassword, newPassword, revokeOtherSessions = false) => {
    try {
      const { data, error } = await authClient.changePassword({
        currentPassword,
        newPassword,
        revokeOtherSessions
      });
      if (error) {
        return {
          success: false,
          data: void 0,
          error: error.message || "Failed to change password"
        };
      }
      return {
        success: true,
        data,
        error: void 0
      };
    } catch (error) {
      return {
        success: false,
        data: void 0,
        error: error instanceof Error ? error.message : "An unexpected error occurred"
      };
    }
  };
  const handleResendVerificationEmail = async (email) => {
    try {
      const { data, error } = await authClient.sendVerificationEmail({
        email
      });
      if (error) {
        return {
          success: false,
          data: void 0,
          error: error.message || "Failed to resend verification email"
        };
      }
      return {
        success: true,
        data,
        error: void 0
      };
    } catch (error) {
      return {
        success: false,
        data: void 0,
        error: error instanceof Error ? error.message : "An unexpected error occurred"
      };
    }
  };
  return {
    user: (_a = session.data) == null ? void 0 : _a.user,
    session: session.data,
    isLoading: session.isPending,
    signIn: handleSignIn,
    signUp: handleSignUp,
    signOut: handleSignOut,
    googleSignIn: handleGoogleSignIn,
    resetPassword: handleResetPassword,
    updatePassword: handleUpdatePassword,
    changePassword: handleChangePassword,
    resendVerificationEmail: handleResendVerificationEmail,
    isSigningIn: false,
    // BetterAuth doesn't provide this directly
    isSigningUp: false,
    // BetterAuth doesn't provide this directly
    signInError: null,
    signUpError: null
  };
}
export {
  useAuth
};
