import { useNostr } from "@nostrify/react";
import { type NLoginType, NUser, useNostrLogin } from "@nostrify/react/login";
import { useCallback, useMemo } from "react";

import { useAuthor } from "./useAuthor.ts";

export function useCurrentUser() {
  const { nostr } = useNostr();
  const { logins } = useNostrLogin();

  const loginToUser = useCallback(
    (login: NLoginType): NUser => {
      switch (login.type) {
        case "nsec": // Nostr login with secret key
          return NUser.fromNsecLogin(login);
        case "bunker": // Nostr login with NIP-46 "bunker://" URI
          return NUser.fromBunkerLogin(login, nostr);
        case "extension": // Nostr login with NIP-07 browser extension
          return NUser.fromExtensionLogin(login);
        // Other login types can be defined here
        default:
          throw new Error(`Unsupported login type: ${login.type}`);
      }
    },
    [nostr],
  );

  const users = useMemo(() => {
    const users: NUser[] = [];

    for (const login of logins) {
      try {
        const user = loginToUser(login);
        users.push(user);
      } catch (error) {
        console.warn("Skipped invalid login", login.id, error);
      }
    }

    return users;
  }, [logins, loginToUser]);

  const user = users[0] as NUser | undefined;

  // The current user's kind 0 profile is served from useAuthor, which
  // may resolve instantly if pre-cached by useFeed. Otherwise it fetches
  // from relays in the background.
  const author = useAuthor(user?.pubkey);

  return {
    user,
    users,
    ...author.data,
    isLoading: author.isLoading,
  };
}
