'use client';
import { useMemo as r, useCallback as z } from "react";
import { useAuth as I } from "../provider/auth-provider.js";
import { useConfig as U } from "../provider/config-provider.js";
function h() {
  const i = I(), { userType: t, features: e } = U(), s = r(() => i.user?.id || null, [i.user]), o = r(() => i.user?.primaryEmailAddress || null, [i.user]), u = r(() => i.user && (i.user.username || `${i.user.firstName || ""} ${i.user.lastName || ""}`.trim() || i.user.primaryEmailAddress) || null, [i.user]), a = r(
    () => i.activeOrganization?.id || null,
    [i.activeOrganization]
  ), g = r(
    () => i.activeOrganization?.name || null,
    [i.activeOrganization]
  ), c = r(
    () => !!i.activeOrganization,
    [i.activeOrganization]
  ), d = r(() => !i.activeOrganization || !i.user ? !1 : i.organizationMemberships.some(
    (n) => n.organization.id === i.activeOrganization?.id
  ), [i.activeOrganization, i.user, i.organizationMemberships]), m = r(() => {
    if (!i.activeOrganization || !i.user) return !1;
    const n = i.organizationMemberships.find(
      (l) => l.organization.id === i.activeOrganization?.id
    );
    return n?.role === "admin" || n?.role === "owner";
  }, [i.activeOrganization, i.user, i.organizationMemberships]), f = r(
    () => i.isLoaded && i.isSignedIn,
    [i.isLoaded, i.isSignedIn]
  ), O = r(() => i.user ? !i.user.emailVerified || e.mfa && !i.user.mfaEnabled : !1, [i.user, e.mfa]), v = r(() => i.user ? e.mfa && !i.user.mfaEnabled : !1, [i.user, e.mfa]), A = z(async (n) => {
    if (n.strategy === "oauth" && !e.oauth)
      throw new Error("OAuth authentication is not enabled for this organization");
    if (n.strategy === "passkey" && !e.passkeys)
      throw new Error("Passkey authentication is not enabled for this organization");
    if (n.strategy === "sso" && !e.sso)
      throw new Error("SSO authentication is not enabled for this organization");
    if (n.strategy === "magic_link" && !e.magicLink)
      throw new Error("Magic link authentication is not enabled for this organization");
    return i.signIn(n);
  }, [i.signIn, e]), w = z(async (n) => {
    if (!e.signUp)
      throw new Error("User registration is not enabled for this organization");
    return i.signUp(n);
  }, [i.signUp, e]), b = z((n) => {
    const l = n || window.location.href;
    try {
      return new URL(l).searchParams.get("email");
    } catch {
      return null;
    }
  }, []), p = z((n) => {
    const l = n || window.location.href;
    try {
      return new URL(l).searchParams.get("token");
    } catch {
      return null;
    }
  }, []);
  return {
    // Core authentication state
    isLoaded: i.isLoaded,
    isLoading: i.isLoading,
    isSignedIn: i.isSignedIn,
    user: i.user,
    session: i.session,
    // Organization context
    organization: i.organization,
    organizationMemberships: i.organizationMemberships,
    activeOrganization: i.activeOrganization,
    // Error state
    error: i.error,
    // Authentication methods
    signIn: A,
    signUp: w,
    signOut: i.signOut,
    resendVerification: i.resendVerification,
    verifyIdentity: i.verifyIdentity,
    // Recovery methods
    requestPasswordReset: i.requestPasswordReset,
    resetPassword: i.resetPassword,
    validateToken: i.validateToken,
    extractEmailFromUrl: b,
    extractTokenFromUrl: p,
    // Session management
    createSession: i.createSession,
    setActive: i.setActive,
    // Organization management
    setActiveOrganization: i.setActiveOrganization,
    switchOrganization: i.switchOrganization,
    // User management
    updateUser: i.updateUser,
    deleteUser: i.deleteUser,
    // Utility methods
    reload: i.reload,
    // Convenience properties
    userId: s,
    userEmail: o,
    userName: u,
    organizationId: a,
    organizationName: g,
    userType: t,
    // Permission helpers
    hasOrganization: c,
    isOrganizationMember: d,
    isOrganizationAdmin: m,
    // Status helpers
    isAuthenticated: f,
    requiresVerification: O,
    requiresMFA: v
  };
}
function L() {
  const {
    isLoaded: i,
    isLoading: t,
    isSignedIn: e,
    user: s,
    session: o,
    organization: u,
    activeOrganization: a,
    error: g,
    userId: c,
    userEmail: d,
    userName: m,
    organizationId: f,
    organizationName: O,
    userType: v,
    hasOrganization: A,
    isOrganizationMember: w,
    isOrganizationAdmin: b,
    isAuthenticated: p,
    requiresVerification: n,
    requiresMFA: l
  } = h();
  return {
    isLoaded: i,
    isLoading: t,
    isSignedIn: e,
    user: s,
    session: o,
    organization: u,
    activeOrganization: a,
    error: g,
    userId: c,
    userEmail: d,
    userName: m,
    organizationId: f,
    organizationName: O,
    userType: v,
    hasOrganization: A,
    isOrganizationMember: w,
    isOrganizationAdmin: b,
    isAuthenticated: p,
    requiresVerification: n,
    requiresMFA: l
  };
}
function S() {
  const {
    signIn: i,
    signUp: t,
    signOut: e,
    createSession: s,
    setActive: o,
    setActiveOrganization: u,
    switchOrganization: a,
    updateUser: g,
    deleteUser: c,
    reload: d
  } = h();
  return {
    signIn: i,
    signUp: t,
    signOut: e,
    createSession: s,
    setActive: o,
    setActiveOrganization: u,
    switchOrganization: a,
    updateUser: g,
    deleteUser: c,
    reload: d
  };
}
function q() {
  const {
    organization: i,
    organizationMemberships: t,
    activeOrganization: e,
    organizationId: s,
    organizationName: o,
    hasOrganization: u,
    isOrganizationMember: a,
    isOrganizationAdmin: g,
    setActiveOrganization: c,
    switchOrganization: d
  } = h();
  return {
    organization: i,
    organizationMemberships: t,
    activeOrganization: e,
    organizationId: s,
    organizationName: o,
    hasOrganization: u,
    isOrganizationMember: a,
    isOrganizationAdmin: g,
    setActiveOrganization: c,
    switchOrganization: d
  };
}
function N() {
  const {
    isLoaded: i,
    isLoading: t,
    isSignedIn: e,
    isAuthenticated: s,
    requiresVerification: o,
    requiresMFA: u,
    error: a
  } = h();
  return {
    isLoaded: i,
    isLoading: t,
    isSignedIn: e,
    isAuthenticated: s,
    requiresVerification: o,
    requiresMFA: u,
    hasError: !!a,
    error: a,
    status: t ? "loading" : e ? "signed-in" : "signed-out"
  };
}
export {
  h as useAuth,
  S as useAuthActions,
  q as useAuthOrganization,
  L as useAuthState,
  N as useAuthStatus
};
//# sourceMappingURL=use-auth.js.map
