{"version":3,"sources":["../../src/utilities/getBestFitFromSizes.ts"],"sourcesContent":["/**\n * Takes image sizes and a target range and returns the url of the image within that range.\n * If no images fit within the range, it selects the next smallest adequate image, the original,\n * or the largest smaller image if no better fit exists.\n *\n * @param sizes The given FileSizes.\n * @param targetSizeMax The ideal image maximum width. Defaults to 180.\n * @param targetSizeMin The ideal image minimum width. Defaults to 40.\n * @param thumbnailURL The thumbnail url set in config. If passed a url, will return early with it.\n * @param url The url of the original file.\n * @param width The width of the original file.\n * @returns A url of the best fit file.\n */\nexport const getBestFitFromSizes = ({\n sizes,\n targetSizeMax = 180,\n targetSizeMin = 40,\n thumbnailURL,\n url,\n width,\n}: {\n sizes?: Record\n targetSizeMax?: number\n targetSizeMin?: number\n thumbnailURL?: string\n url: string\n width?: number\n}) => {\n if (thumbnailURL) {\n return thumbnailURL\n }\n\n if (!sizes) {\n return url\n }\n\n const bestFit = Object.values(sizes).reduce<{\n original?: boolean\n url?: string\n width?: number\n }>(\n (closest, current) => {\n if (!current.width || current.width < targetSizeMin) {\n return closest\n }\n\n if (current.width >= targetSizeMin && current.width <= targetSizeMax) {\n return !closest.width ||\n current.width < closest.width ||\n closest.width < targetSizeMin ||\n closest.width > targetSizeMax\n ? current\n : closest\n }\n\n if (\n !closest.width ||\n (!closest.original && closest.width < targetSizeMin && current.width > closest.width) ||\n (closest.width > targetSizeMax && current.width < closest.width)\n ) {\n return current\n }\n\n return closest\n },\n { original: true, url, width },\n )\n\n return bestFit.url || url\n}\n"],"names":["getBestFitFromSizes","sizes","targetSizeMax","targetSizeMin","thumbnailURL","url","width","bestFit","Object","values","reduce","closest","current","original"],"mappings":"AAAA;;;;;;;;;;;;CAYC,GACD,OAAO,MAAMA,sBAAsB,CAAC,EAClCC,KAAK,EACLC,gBAAgB,GAAG,EACnBC,gBAAgB,EAAE,EAClBC,YAAY,EACZC,GAAG,EACHC,KAAK,EAQN;IACC,IAAIF,cAAc;QAChB,OAAOA;IACT;IAEA,IAAI,CAACH,OAAO;QACV,OAAOI;IACT;IAEA,MAAME,UAAUC,OAAOC,MAAM,CAACR,OAAOS,MAAM,CAKzC,CAACC,SAASC;QACR,IAAI,CAACA,QAAQN,KAAK,IAAIM,QAAQN,KAAK,GAAGH,eAAe;YACnD,OAAOQ;QACT;QAEA,IAAIC,QAAQN,KAAK,IAAIH,iBAAiBS,QAAQN,KAAK,IAAIJ,eAAe;YACpE,OAAO,CAACS,QAAQL,KAAK,IACnBM,QAAQN,KAAK,GAAGK,QAAQL,KAAK,IAC7BK,QAAQL,KAAK,GAAGH,iBAChBQ,QAAQL,KAAK,GAAGJ,gBACdU,UACAD;QACN;QAEA,IACE,CAACA,QAAQL,KAAK,IACb,CAACK,QAAQE,QAAQ,IAAIF,QAAQL,KAAK,GAAGH,iBAAiBS,QAAQN,KAAK,GAAGK,QAAQL,KAAK,IACnFK,QAAQL,KAAK,GAAGJ,iBAAiBU,QAAQN,KAAK,GAAGK,QAAQL,KAAK,EAC/D;YACA,OAAOM;QACT;QAEA,OAAOD;IACT,GACA;QAAEE,UAAU;QAAMR;QAAKC;IAAM;IAG/B,OAAOC,QAAQF,GAAG,IAAIA;AACxB,EAAC"}