/**
 * Single source of truth for what URL widths the chat renders at — used
 * by BOTH the renderer (so the right `?width=N` lands on the network)
 * and the prefetcher (so the warmed bytes match what the tile will
 * request). If you change a size here, both stay in sync automatically.
 */
import {AvatarSize} from '@components';
import {
  DPR,
  IMAGE_SIZE_SMALL_PX,
  IMAGE_SIZE_TINY_PX,
} from '@modules/chats/constants';

import {avatarSizeToPixels} from './avatar';

/** Avatar size for the conversations list rows. */
export const CHAT_LIST_AVATAR_SIZE: AvatarSize = 'lg';

/** Avatar size for the chat header. */
export const CHAT_HEADER_AVATAR_SIZE: AvatarSize = 'md';

/** Avatar size for the in-message sender avatar. */
export const MESSAGE_SENDER_AVATAR_SIZE: AvatarSize = 'sm';

/**
 * Pixel widths of every conversation-level avatar variant the chat renders
 * (chat list + chat header). Used by the prefetcher to warm both.
 */
export const getConversationAvatarPrefetchWidths = (): number[] => {
  return [CHAT_LIST_AVATAR_SIZE, CHAT_HEADER_AVATAR_SIZE]
    .map(s => avatarSizeToPixels(s))
    .filter((px): px is number => px !== undefined)
    .map(px => Math.round(px * DPR));
};

/**
 * Width(s) the chat image tile renders for a message with N attachments.
 *
 * - 1 attachment → `<ProgressiveImage smallWidth={TINY} largeWidth={SMALL}>`.
 *   TINY is the placeholder, SMALL is the source.
 * - 2+ attachments → grid; just SMALL.
 *
 * `placeholderWidth` and `upgradeWidth` plug straight into ProgressiveImage.
 * `prefetchWidths` is the list the prefetcher should warm.
 */
export const getMessageImageTileSizing = (attachmentCount: number) => {
  const isBigSingle = attachmentCount === 1;
  return {
    placeholderWidth: isBigSingle ? IMAGE_SIZE_TINY_PX : IMAGE_SIZE_SMALL_PX,
    upgradeWidth: isBigSingle ? IMAGE_SIZE_SMALL_PX : undefined,
    prefetchWidths: isBigSingle
      ? [IMAGE_SIZE_TINY_PX, IMAGE_SIZE_SMALL_PX]
      : [IMAGE_SIZE_SMALL_PX],
  };
};

/** Width the video poster thumbnail renders at in the chat tile. */
export const MESSAGE_VIDEO_THUMBNAIL_WIDTH = IMAGE_SIZE_SMALL_PX;
