"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useUsersBySessions = exports.useUserProfiles = exports.useUserProfile = exports.useUserByUsername = exports.useUserById = exports.useCurrentUser = void 0;
var _reactQuery = require("@tanstack/react-query");
var _queryKeys = require("./queryKeys");
var _OxyContext = require("../../context/OxyContext");
/**
 * Get user profile by session ID
 */
const useUserProfile = (sessionId, options) => {
  const {
    oxyServices
  } = (0, _OxyContext.useOxy)();
  return (0, _reactQuery.useQuery)({
    queryKey: _queryKeys.queryKeys.users.profile(sessionId || ''),
    queryFn: async () => {
      if (!sessionId) {
        throw new Error('Session ID is required');
      }
      return await oxyServices.getUserBySession(sessionId);
    },
    enabled: options?.enabled !== false && !!sessionId,
    staleTime: 5 * 60 * 1000,
    // 5 minutes
    gcTime: 30 * 60 * 1000 // 30 minutes
  });
};

/**
 * Get multiple user profiles by session IDs (batch query)
 */
exports.useUserProfile = useUserProfile;
const useUserProfiles = (sessionIds, options) => {
  const {
    oxyServices
  } = (0, _OxyContext.useOxy)();
  return (0, _reactQuery.useQueries)({
    queries: sessionIds.map(sessionId => ({
      queryKey: _queryKeys.queryKeys.users.profile(sessionId),
      queryFn: async () => {
        const results = await oxyServices.getUsersBySessions([sessionId]);
        return results[0]?.user || null;
      },
      enabled: options?.enabled !== false && !!sessionId,
      staleTime: 5 * 60 * 1000,
      gcTime: 30 * 60 * 1000
    }))
  });
};

/**
 * Get current authenticated user
 */
exports.useUserProfiles = useUserProfiles;
const useCurrentUser = options => {
  const {
    oxyServices,
    activeSessionId,
    isAuthenticated
  } = (0, _OxyContext.useOxy)();
  return (0, _reactQuery.useQuery)({
    queryKey: _queryKeys.queryKeys.accounts.current(),
    queryFn: async () => {
      if (!activeSessionId) {
        throw new Error('No active session');
      }
      return await oxyServices.getUserBySession(activeSessionId);
    },
    enabled: options?.enabled !== false && isAuthenticated && !!activeSessionId,
    staleTime: 1 * 60 * 1000,
    // 1 minute for current user
    gcTime: 30 * 60 * 1000
  });
};

/**
 * Get user by ID
 */
exports.useCurrentUser = useCurrentUser;
const useUserById = (userId, options) => {
  const {
    oxyServices
  } = (0, _OxyContext.useOxy)();
  return (0, _reactQuery.useQuery)({
    queryKey: _queryKeys.queryKeys.users.detail(userId || ''),
    queryFn: async () => {
      if (!userId) {
        throw new Error('User ID is required');
      }
      return await oxyServices.getUserById(userId);
    },
    enabled: options?.enabled !== false && !!userId,
    staleTime: 5 * 60 * 1000,
    gcTime: 30 * 60 * 1000
  });
};

/**
 * Get user profile by username
 */
exports.useUserById = useUserById;
const useUserByUsername = (username, options) => {
  const {
    oxyServices
  } = (0, _OxyContext.useOxy)();
  return (0, _reactQuery.useQuery)({
    queryKey: [..._queryKeys.queryKeys.users.details(), 'username', username || ''],
    queryFn: async () => {
      if (!username) {
        throw new Error('Username is required');
      }
      return await oxyServices.getProfileByUsername(username);
    },
    enabled: options?.enabled !== false && !!username,
    staleTime: 5 * 60 * 1000,
    gcTime: 30 * 60 * 1000
  });
};

/**
 * Batch get users by session IDs (optimized single API call)
 */
exports.useUserByUsername = useUserByUsername;
const useUsersBySessions = (sessionIds, options) => {
  const {
    oxyServices
  } = (0, _OxyContext.useOxy)();
  return (0, _reactQuery.useQuery)({
    queryKey: _queryKeys.queryKeys.accounts.list(sessionIds),
    queryFn: async () => {
      if (sessionIds.length === 0) {
        return [];
      }
      return await oxyServices.getUsersBySessions(sessionIds);
    },
    enabled: options?.enabled !== false && sessionIds.length > 0,
    staleTime: 5 * 60 * 1000,
    gcTime: 30 * 60 * 1000
  });
};
exports.useUsersBySessions = useUsersBySessions;
//# sourceMappingURL=useAccountQueries.js.map