{"version":3,"file":"toBaseImageDescriptors.mjs","names":[],"sources":["../../../../src/components/BaseImage/toBaseImageDescriptors.ts"],"sourcesContent":["import {\n  type Attachment,\n  isGiphyAttachment,\n  isImageAttachment,\n  isLocalImageAttachment,\n  isLocalVideoAttachment,\n  isScrapedContent,\n  isVideoAttachment,\n  type LinkPreview,\n  type LocalImageAttachment,\n  type LocalVideoAttachment,\n} from 'stream-chat';\nimport type { Dimensions } from '../../types/types';\nimport { getGiphyDescriptiveTitle } from '../Attachment/giphyAccessibility';\n\ntype AttachmentPreviewableInGallery =\n  | LocalImageAttachment\n  | LocalVideoAttachment\n  | LinkPreview\n  | Attachment;\n\n/** Image and video fields shared with gallery items. */\nexport type BaseImageDescriptor = {\n  alt?: string;\n  dimensions?: Dimensions;\n  imageUrl?: string;\n  title?: string;\n  videoThumbnailUrl?: string;\n  videoUrl?: string;\n};\n\n/**\n * Maps an attachment or link preview to the image and video fields that {@link BaseImage} and the\n * gallery render.\n *\n * @param attachment - Attachment or link preview to describe.\n * @param options.giphyVersionName - Key of `attachment.giphy` that supplies the image URL and\n * dimensions of a Giphy attachment. Without that version the image loads from `thumb_url`.\n * @returns The descriptor, or undefined when the attachment is not an image, video, Giphy or link\n * preview.\n */\nexport const toBaseImageDescriptors = (\n  attachment: AttachmentPreviewableInGallery,\n  options: { giphyVersionName?: string } = {},\n): BaseImageDescriptor | undefined => {\n  if (isGiphyAttachment(attachment)) {\n    const giphyVersion =\n      options?.giphyVersionName && attachment.giphy\n        ? attachment.giphy[\n            options.giphyVersionName as keyof NonNullable<Attachment['giphy']>\n          ]\n        : undefined;\n\n    return {\n      // Giphy titles are often media URLs, which are not a usable text alternative.\n      alt: getGiphyDescriptiveTitle(attachment.title),\n      dimensions: giphyVersion\n        ? {\n            height: giphyVersion.height,\n            width: giphyVersion.width,\n          }\n        : undefined,\n      imageUrl: giphyVersion?.url || attachment.thumb_url,\n      title: attachment.title || attachment.thumb_url,\n    };\n  }\n\n  if (isScrapedContent(attachment)) {\n    const imageUrl = attachment.image_url || attachment.thumb_url;\n    return {\n      alt: attachment.title || imageUrl,\n      imageUrl,\n      title: attachment.title,\n    };\n  }\n\n  if (isLocalVideoAttachment(attachment)) {\n    return {\n      title: attachment.title,\n      // `previewUri` is the video file's own blob URL, which cannot render as an image.\n      videoThumbnailUrl: attachment.thumb_url,\n      videoUrl: attachment.asset_url ?? attachment.localMetadata.previewUri,\n    };\n  }\n\n  if (isVideoAttachment(attachment)) {\n    return {\n      title: attachment.title,\n      videoThumbnailUrl: attachment.thumb_url,\n      videoUrl: attachment.asset_url,\n    };\n  }\n\n  if (isLocalImageAttachment(attachment)) {\n    const imageUrl = attachment.image_url || attachment.localMetadata.previewUri;\n    return {\n      alt: attachment.title || imageUrl,\n      imageUrl,\n      title: attachment.title,\n    };\n  }\n\n  if (isImageAttachment(attachment)) {\n    const imageUrl = attachment.image_url;\n    return {\n      alt: attachment.title || imageUrl,\n      imageUrl,\n      title: attachment.title,\n    };\n  }\n\n  return undefined;\n};\n"],"mappings":";;;;;;;;;;;;;AAyCA,IAAa,0BACX,YACA,UAAyC,CAAC,MACN;CACpC,IAAI,kBAAkB,UAAU,GAAG;EACjC,MAAM,eACJ,SAAS,oBAAoB,WAAW,QACpC,WAAW,MACT,QAAQ,oBAEV,KAAA;EAEN,OAAO;GAEL,KAAK,yBAAyB,WAAW,KAAK;GAC9C,YAAY,eACR;IACE,QAAQ,aAAa;IACrB,OAAO,aAAa;GACtB,IACA,KAAA;GACJ,UAAU,cAAc,OAAO,WAAW;GAC1C,OAAO,WAAW,SAAS,WAAW;EACxC;CACF;CAEA,IAAI,iBAAiB,UAAU,GAAG;EAChC,MAAM,WAAW,WAAW,aAAa,WAAW;EACpD,OAAO;GACL,KAAK,WAAW,SAAS;GACzB;GACA,OAAO,WAAW;EACpB;CACF;CAEA,IAAI,uBAAuB,UAAU,GACnC,OAAO;EACL,OAAO,WAAW;EAElB,mBAAmB,WAAW;EAC9B,UAAU,WAAW,aAAa,WAAW,cAAc;CAC7D;CAGF,IAAI,kBAAkB,UAAU,GAC9B,OAAO;EACL,OAAO,WAAW;EAClB,mBAAmB,WAAW;EAC9B,UAAU,WAAW;CACvB;CAGF,IAAI,uBAAuB,UAAU,GAAG;EACtC,MAAM,WAAW,WAAW,aAAa,WAAW,cAAc;EAClE,OAAO;GACL,KAAK,WAAW,SAAS;GACzB;GACA,OAAO,WAAW;EACpB;CACF;CAEA,IAAI,kBAAkB,UAAU,GAAG;EACjC,MAAM,WAAW,WAAW;EAC5B,OAAO;GACL,KAAK,WAAW,SAAS;GACzB;GACA,OAAO,WAAW;EACpB;CACF;AAGF"}