{
  "version": 3,
  "sources": ["../../../../src/lib/utils/embeds/embeds.ts"],
  "sourcesContent": ["import { safeParseUrl } from '@tldraw/editor'\nimport { TLEmbedDefinition } from '../../defaultEmbedDefinitions'\n\n// https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js\nfunction escapeStringRegexp(string: string) {\n\tif (typeof string !== 'string') {\n\t\tthrow new TypeError('Expected a string')\n\t}\n\n\t// Escape characters with special meaning either inside or outside character sets.\n\t// Use a simple backslash escape when it\u2019s always valid, and a `\\xnn` escape when the simpler form would be disallowed by Unicode patterns\u2019 stricter grammar.\n\treturn string.replace(/[|\\\\{}()[\\]^$+*?.]/g, '\\\\$&').replace(/-/g, '\\\\x2d')\n}\n\n/** @public */\nexport function matchEmbedUrl(definitions: readonly TLEmbedDefinition[], url: string) {\n\tconst parsed = safeParseUrl(url)\n\tif (!parsed) return undefined\n\tconst host = parsed.host.replace('www.', '')\n\tfor (const localEmbedDef of definitions) {\n\t\tif (checkHostnames(localEmbedDef.hostnames, host)) {\n\t\t\tconst originalUrl = localEmbedDef.fromEmbedUrl(url)\n\t\t\tif (originalUrl) {\n\t\t\t\treturn {\n\t\t\t\t\tdefinition: localEmbedDef,\n\t\t\t\t\turl: originalUrl,\n\t\t\t\t\tembedUrl: url,\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn undefined\n}\n\nconst globlikeRegExp = (input: string) => {\n\treturn input\n\t\t.split('*')\n\t\t.map((str) => escapeStringRegexp(str))\n\t\t.join('.+')\n}\n\nconst checkHostnames = (hostnames: readonly string[], targetHostname: string) => {\n\treturn !!hostnames.find((hostname) => {\n\t\tconst re = new RegExp(globlikeRegExp(hostname))\n\t\treturn targetHostname.match(re)\n\t})\n}\n\n/** @public */\nexport function matchUrl(\n\tdefinitions: readonly TLEmbedDefinition[],\n\turl: string,\n\tembedConfig?: Record<string, unknown>\n) {\n\tconst parsed = safeParseUrl(url)\n\tif (!parsed) return undefined\n\tconst host = parsed.host.replace('www.', '')\n\tfor (const localEmbedDef of definitions) {\n\t\tif (checkHostnames(localEmbedDef.hostnames, host)) {\n\t\t\tconst embedUrl = localEmbedDef.toEmbedUrl(url, embedConfig?.[localEmbedDef.type])\n\n\t\t\tif (embedUrl) {\n\t\t\t\treturn {\n\t\t\t\t\tdefinition: localEmbedDef,\n\t\t\t\t\tembedUrl,\n\t\t\t\t\turl,\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn undefined\n}\n\n/** @public */\nexport type TLEmbedResult =\n\t| {\n\t\t\tdefinition: TLEmbedDefinition\n\t\t\turl: string\n\t\t\tembedUrl: string\n\t  }\n\t| undefined\n\n/**\n * Tests whether an URL supports embedding and returns the result. If we encounter an error, we\n * return undefined.\n *\n * @param inputUrl - The URL to match\n * @param embedConfig - Optional per-embed config, keyed by embed type, passed to `toEmbedUrl`\n * @public\n */\nexport function getEmbedInfo(\n\tdefinitions: readonly TLEmbedDefinition[],\n\tinputUrl: string,\n\tembedConfig?: Record<string, unknown>\n): TLEmbedResult {\n\ttry {\n\t\treturn matchUrl(definitions, inputUrl, embedConfig) ?? matchEmbedUrl(definitions, inputUrl)\n\t} catch {\n\t\treturn undefined\n\t}\n}\n\nconst ASPECT_RATIO_EPSILON = 0.001\n\n/**\n * Given an embed shape's current size and a newly-resolved aspect ratio, return the size it should\n * be corrected to, or `null` when no change is needed \u2014 either because there's no resolved ratio or\n * the shape is already at it. The correction preserves the box's area, so a portrait video takes the\n * same visual footprint as a landscape one instead of ballooning the shape's height.\n *\n * @param opts - The current `w`/`h` and the `resolvedRatio` (`width / height`) discovered at runtime.\n * @internal\n */\nexport function getCorrectedEmbedSize({\n\tw,\n\th,\n\tresolvedRatio,\n}: {\n\tw: number\n\th: number\n\tresolvedRatio: number | undefined\n}): { w: number; h: number } | null {\n\tif (!resolvedRatio || resolvedRatio <= 0) return null\n\n\t// Already at the resolved ratio: nothing to do.\n\tif (Math.abs(w / h - resolvedRatio) <= ASPECT_RATIO_EPSILON) return null\n\n\tcon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