{"version":3,"file":"attachment-sizing.mjs","names":[],"sources":["../../../../src/components/Attachment/attachment-sizing.tsx"],"sourcesContent":["import type { Attachment } from 'stream-chat';\nimport * as linkify from 'linkifyjs';\n\nexport const getImageAttachmentConfiguration = (\n  attachment: Attachment,\n  element: HTMLElement,\n) => {\n  let newUrl = undefined;\n\n  const urlToTest = attachment.image_url || attachment.thumb_url || '';\n\n  if (linkify.test(urlToTest, 'url')) {\n    const url = new URL(urlToTest);\n    const resizeDimensions = getSizingRestrictions(url, element);\n\n    if (resizeDimensions) {\n      // Apply 2x for retina displays\n      resizeDimensions.height *= 2;\n      resizeDimensions.width *= 2;\n      addResizingParamsToUrl(resizeDimensions, url);\n    }\n    newUrl = url.href;\n  }\n\n  return {\n    url: newUrl || '',\n  };\n};\n\nexport const getVideoAttachmentConfiguration = (\n  attachment: Attachment,\n  element: HTMLElement,\n  shouldGenerateVideoThumbnail: boolean,\n) => {\n  let thumbUrl = undefined;\n  if (\n    attachment.thumb_url &&\n    shouldGenerateVideoThumbnail &&\n    linkify.test(attachment.thumb_url, 'url')\n  ) {\n    const url = new URL(attachment.thumb_url);\n    const resizeDimensions = getSizingRestrictions(url, element);\n\n    if (resizeDimensions) {\n      // Apply 2x for retina displays\n      resizeDimensions.height *= 2;\n      resizeDimensions.width *= 2;\n      addResizingParamsToUrl(resizeDimensions, url);\n    }\n    thumbUrl = url.href;\n  }\n\n  return {\n    thumbUrl,\n    url: attachment.asset_url || '',\n  };\n};\n\nconst getSizingRestrictions = (url: URL, htmlElement: HTMLElement) => {\n  const urlParams = url.searchParams;\n  const originalHeight = Number(urlParams.get('oh')) || 1;\n  const originalWidth = Number(urlParams.get('ow')) || 1;\n  const cssSizeRestriction = getCSSSizeRestrictions(htmlElement);\n  let resizeDimensions: { height: number; width: number } | undefined;\n\n  if (\n    (cssSizeRestriction.maxHeight || cssSizeRestriction.height) &&\n    cssSizeRestriction.maxWidth\n  ) {\n    resizeDimensions = getResizeDimensions(\n      originalHeight,\n      originalWidth,\n      /* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */\n      cssSizeRestriction.maxHeight || cssSizeRestriction.height!,\n      cssSizeRestriction.maxWidth,\n    );\n  } else {\n    resizeDimensions = undefined;\n  }\n\n  return resizeDimensions;\n};\n\nconst getResizeDimensions = (\n  originalHeight: number,\n  originalWidth: number,\n  maxHeight: number,\n  maxWidth: number,\n) => ({\n  height: Math.round(Math.max(maxHeight, (maxWidth / originalWidth) * originalHeight)),\n  width: Math.round(Math.max(maxHeight, (maxWidth / originalHeight) * originalWidth)),\n});\n\nconst getCSSSizeRestrictions = (htmlElement: HTMLElement) => {\n  const computedStylesheet = getComputedStyle(htmlElement);\n  const height = getValueRepresentationOfCSSProperty(\n    computedStylesheet.getPropertyValue('height'),\n  );\n  const maxHeight = getValueRepresentationOfCSSProperty(\n    computedStylesheet.getPropertyValue('max-height'),\n  );\n  const maxWidth = getValueRepresentationOfCSSProperty(\n    computedStylesheet.getPropertyValue('max-width'),\n  );\n\n  if (!((height || maxHeight) && maxWidth)) {\n    console.warn(\n      `Invalid value set for height/max-height and/or max-width for HTML element, this can cause scrolling issues inside the message list, more info https://getstream.io/chat/docs/sdk/react/message-components/attachment/#image-and-video-sizing`,\n    );\n  }\n\n  return { height, maxHeight, maxWidth };\n};\n\nconst getValueRepresentationOfCSSProperty = (property: string) => {\n  if (!property.endsWith('px')) {\n    return undefined;\n  }\n  const number = parseFloat(property);\n  return isNaN(number) ? undefined : number;\n};\n\nconst addResizingParamsToUrl = (\n  resizeDimensions: { height: number; width: number },\n  url: URL,\n) => {\n  url.searchParams.set('h', resizeDimensions.height.toString());\n  url.searchParams.set('w', resizeDimensions.width.toString());\n};\n"],"mappings":";;AAGA,IAAa,mCACX,YACA,YACG;CACH,IAAI,SAAS,KAAA;CAEb,MAAM,YAAY,WAAW,aAAa,WAAW,aAAa;CAElE,IAAI,QAAQ,KAAK,WAAW,KAAK,GAAG;EAClC,MAAM,MAAM,IAAI,IAAI,SAAS;EAC7B,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