import { useState as A, useMemo as s, useCallback as l } from "react";
import { FrankUser as J, FrankAuth as Q } from "@frank-auth/sdk";
import { useAuth as X } from "./use-auth.js";
import { useConfig as Y } from "../provider/config-provider.js";
import { undefined as Z } from "zod";
function g() {
  const { user: e, updateUser: w, deleteUser: h, reload: o, session: u, userType: y } = X(), { apiUrl: d, publishableKey: m } = Y(), [p, n] = A(!1), [P, c] = A(null), t = s(() => u?.accessToken ? new J({
    publishableKey: m,
    apiUrl: d,
    userType: y ?? "end_user"
  }, u.accessToken) : null, [m, d, u?.accessToken]), E = s(() => u?.accessToken ? new Q({
    publishableKey: m,
    apiUrl: d,
    userType: y ?? "end_user"
  }) : null, [m, d, u?.accessToken]), i = l((r) => {
    const a = {
      code: r.code || "UNKNOWN_ERROR",
      message: r.message || "An unknown error occurred",
      details: r.details,
      field: r.field
    };
    throw c(a), a;
  }, []), f = l(async (r) => {
    if (!t) throw new Error("User not authenticated");
    try {
      n(!0), c(null);
      const a = await t.updateProfile(r);
      return await o(), a;
    } catch (a) {
      return i(a);
    } finally {
      n(!1);
    }
  }, [t, o, i]), U = l(async (r) => {
    if (!t) throw new Error("User not authenticated");
    try {
      n(!0), c(null), await t.changePassword(r);
    } catch (a) {
      i(a);
    } finally {
      n(!1);
    }
  }, [t, i]), V = l(async () => {
    try {
      n(!0), c(null), await h();
    } catch (r) {
      i(r);
    } finally {
      n(!1);
    }
  }, [h, i]), N = l(async (r) => {
    try {
      n(!0), c(null), await f({ primaryEmailAddress: r });
    } catch (a) {
      i(a);
    } finally {
      n(!1);
    }
  }, [f, i]), k = l(async (r) => {
    if (!t) throw new Error("User not authenticated");
    try {
      n(!0), c(null);
      const a = {
        code: r,
        type: "email"
      };
      await E.verifyEmail(a), await o();
    } catch (a) {
      i(a);
    } finally {
      n(!1);
    }
  }, [t, o, i]), I = l(async () => {
    if (!t || !e?.primaryEmailAddress) throw new Error("User not authenticated");
    try {
      n(!0), c(null), await t.resendEmailVerification(e.primaryEmailAddress);
    } catch (r) {
      i(r);
    } finally {
      n(!1);
    }
  }, [t, e?.primaryEmailAddress, i]), v = l(async (r) => {
    try {
      n(!0), c(null), await f({ primaryPhoneNumber: r });
    } catch (a) {
      i(a);
    } finally {
      n(!1);
    }
  }, [f, i]), b = l(async (r) => {
    if (!t) throw new Error("User not authenticated");
    try {
      n(!0), c(null);
      const a = {
        code: r,
        type: "phone"
      };
      await t.verifyPhone(a), await o();
    } catch (a) {
      i(a);
    } finally {
      n(!1);
    }
  }, [t, o, i]), T = l(async () => {
    if (!t || !e?.primaryPhoneNumber) throw new Error("User not authenticated");
    try {
      n(!0), c(null), await t.resendPhoneVerification(e.primaryPhoneNumber);
    } catch (r) {
      i(r);
    } finally {
      n(!1);
    }
  }, [t, e?.primaryPhoneNumber, i]), L = l(async (r) => f({ profileImageUrl: r }), [f]), R = l(async () => f({ profileImageUrl: Z() }), [f]), S = l(async (r) => f({ unsafeMetadata: r }), [f]), M = s(() => e?.firstName || null, [e]), _ = s(() => e?.lastName || null, [e]), B = s(() => {
    if (!e) return null;
    const r = [e.firstName, e.lastName].filter(Boolean);
    return r.length > 0 ? r.join(" ") : null;
  }, [e]), q = s(() => e?.primaryEmailAddress || null, [e]), C = s(() => e?.primaryPhoneNumber || null, [e]), D = s(() => e?.profileImageUrl || null, [e]), F = s(() => e?.username || null, [e]), K = s(() => e?.emailVerified || !1, [e]), O = s(() => e?.phoneVerified || !1, [e]), j = s(() => !!e && !e.emailVerified, [e]), x = s(() => !!e && !!e.primaryPhoneNumber && !e.phoneVerified, [e]), W = s(() => e?.active || !1, [e]), z = s(() => e?.blocked || !1, [e]), G = s(() => e?.createdAt ? new Date(e.createdAt) : null, [e]), H = s(() => e?.lastSignInAt ? new Date(e.lastSignInAt) : null, [e]);
  return {
    // User state
    user: e,
    isLoaded: !!e,
    isLoading: p,
    error: P,
    // Profile management
    updateProfile: f,
    changePassword: U,
    deleteAccount: V,
    // Email management
    updateEmail: N,
    verifyEmail: k,
    resendEmailVerification: I,
    // Phone management
    updatePhone: v,
    verifyPhone: b,
    resendPhoneVerification: T,
    // Profile image
    updateProfileImage: L,
    removeProfileImage: R,
    // Metadata management
    updateMetadata: S,
    // Convenience properties
    firstName: M,
    lastName: _,
    fullName: B,
    email: q,
    phone: C,
    profileImageUrl: D,
    username: F,
    // Verification status
    isEmailVerified: K,
    isPhoneVerified: O,
    needsEmailVerification: j,
    needsPhoneVerification: x,
    // Account status
    isActive: W,
    isBlocked: z,
    createdAt: G,
    lastSignInAt: H
  };
}
function te() {
  const {
    user: e,
    firstName: w,
    lastName: h,
    fullName: o,
    email: u,
    phone: y,
    profileImageUrl: d,
    username: m,
    isEmailVerified: p,
    isPhoneVerified: n,
    isActive: P,
    isBlocked: c,
    createdAt: t,
    lastSignInAt: E
  } = g();
  return {
    user: e,
    firstName: w,
    lastName: h,
    fullName: o,
    email: u,
    phone: y,
    profileImageUrl: d,
    username: m,
    isEmailVerified: p,
    isPhoneVerified: n,
    isActive: P,
    isBlocked: c,
    createdAt: t,
    lastSignInAt: E
  };
}
function ie() {
  const {
    isEmailVerified: e,
    isPhoneVerified: w,
    needsEmailVerification: h,
    needsPhoneVerification: o,
    verifyEmail: u,
    verifyPhone: y,
    resendEmailVerification: d,
    resendPhoneVerification: m,
    isLoading: p,
    error: n
  } = g();
  return {
    isEmailVerified: e,
    isPhoneVerified: w,
    needsEmailVerification: h,
    needsPhoneVerification: o,
    verifyEmail: u,
    verifyPhone: y,
    resendEmailVerification: d,
    resendPhoneVerification: m,
    isLoading: p,
    error: n,
    needsVerification: h || o
  };
}
function se() {
  const {
    updateProfile: e,
    changePassword: w,
    deleteAccount: h,
    updateEmail: o,
    updatePhone: u,
    updateProfileImage: y,
    removeProfileImage: d,
    updateMetadata: m,
    isLoading: p,
    error: n
  } = g();
  return {
    updateProfile: e,
    changePassword: w,
    deleteAccount: h,
    updateEmail: o,
    updatePhone: u,
    updateProfileImage: y,
    removeProfileImage: d,
    updateMetadata: m,
    isLoading: p,
    error: n
  };
}
export {
  g as useUser,
  se as useUserActions,
  te as useUserProfile,
  ie as useUserVerification
};
//# sourceMappingURL=use-user.js.map
