"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.refreshAvatarInStore = refreshAvatarInStore;
exports.updateAvatarVisibility = updateAvatarVisibility;
exports.updateProfileWithAvatar = updateProfileWithAvatar;
var _accountStore = require("../stores/accountStore");
var _authStore = require("../stores/authStore");
var _queryKeys = require("../hooks/queries/queryKeys");
/**
 * Updates file visibility to public for avatar use.
 * Handles errors gracefully, only logging non-404 errors.
 * 
 * @param fileId - The file ID to update visibility for
 * @param oxyServices - OxyServices instance
 * @param contextName - Optional context name for logging
 * @returns Promise that resolves when visibility is updated (or skipped)
 */
async function updateAvatarVisibility(fileId, oxyServices, contextName = 'AvatarUtils') {
  // Skip if temporary asset ID or no file ID
  if (!fileId || fileId.startsWith('temp-')) {
    return;
  }
  try {
    await oxyServices.assetUpdateVisibility(fileId, 'public');
    // Visibility update is logged by the API
  } catch (visError) {
    // Silently handle errors - 404 means asset doesn't exist yet (which is OK)
    // Other errors are logged by the API, so no need to log here
    // Function continues gracefully regardless of visibility update success
  }
}

/**
 * Refreshes avatar in accountStore with cache-busted URL to force image reload.
 * 
 * @param sessionId - The session ID for the account to update
 * @param avatarFileId - The new avatar file ID
 * @param oxyServices - OxyServices instance to generate download URL
 */
function refreshAvatarInStore(sessionId, avatarFileId, oxyServices) {
  const {
    updateAccount
  } = _accountStore.useAccountStore.getState();
  const cacheBustedUrl = oxyServices.getFileDownloadUrl(avatarFileId, 'thumb') + `?t=${Date.now()}`;
  updateAccount(sessionId, {
    avatar: avatarFileId,
    avatarUrl: cacheBustedUrl
  });
}

/**
 * Updates user profile with avatar and handles all side effects (query invalidation, accountStore update).
 * This function can be used from within OxyContext provider without requiring useOxy hook.
 * 
 * @param updates - Profile updates including avatar
 * @param oxyServices - OxyServices instance
 * @param activeSessionId - Active session ID
 * @param queryClient - TanStack Query client
 * @param syncIdentity - Optional sync identity function for handling auth errors
 * @returns Promise that resolves with updated user data
 */
async function updateProfileWithAvatar(updates, oxyServices, activeSessionId, queryClient, syncIdentity) {
  // Ensure we have a valid token before making the request
  if (!oxyServices.hasValidToken() && activeSessionId) {
    try {
      await oxyServices.getTokenBySession(activeSessionId);
    } catch (tokenError) {
      const errorMessage = tokenError instanceof Error ? tokenError.message : String(tokenError);
      if (errorMessage.includes('AUTH_REQUIRED_OFFLINE_SESSION') || errorMessage.includes('offline')) {
        if (syncIdentity) {
          try {
            await syncIdentity();
            await oxyServices.getTokenBySession(activeSessionId);
          } catch (syncError) {
            throw new Error('Session needs to be synced. Please try again.');
          }
        } else {
          throw tokenError;
        }
      } else {
        throw tokenError;
      }
    }
  }
  try {
    const data = await oxyServices.updateProfile(updates);

    // Update cache with server response
    queryClient.setQueryData(_queryKeys.queryKeys.accounts.current(), data);
    if (activeSessionId) {
      queryClient.setQueryData(_queryKeys.queryKeys.users.profile(activeSessionId), data);
    }

    // Update authStore so frontend components see the changes immediately
    _authStore.useAuthStore.getState().setUser(data);

    // If avatar was updated, refresh accountStore with cache-busted URL
    if (updates.avatar && activeSessionId) {
      refreshAvatarInStore(activeSessionId, updates.avatar, oxyServices);
    }

    // Invalidate all related queries to refresh everywhere
    (0, _queryKeys.invalidateUserQueries)(queryClient);
    (0, _queryKeys.invalidateAccountQueries)(queryClient);
    return data;
  } catch (error) {
    const errorMessage = error?.message || '';
    const status = error?.status || error?.response?.status;

    // Handle authentication errors
    if (status === 401 || errorMessage.includes('Authentication required') || errorMessage.includes('Invalid or missing authorization header')) {
      if (activeSessionId && syncIdentity) {
        try {
          await syncIdentity();
          await oxyServices.getTokenBySession(activeSessionId);
          // Retry the update after getting token
          return await updateProfileWithAvatar(updates, oxyServices, activeSessionId, queryClient, syncIdentity);
        } catch (retryError) {
          throw new Error('Authentication failed. Please sign in again.');
        }
      } else {
        throw new Error('No active session. Please sign in.');
      }
    }
    throw error;
  }
}
//# sourceMappingURL=avatarUtils.js.map