"use strict";var N=Object.defineProperty;var R=(e,n,r)=>n in e?N(e,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[n]=r;var U=(e,n,r)=>R(e,typeof n!="symbol"?n+"":n,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./constants-BbfMtzIs.cjs");var k={},d={};Object.defineProperty(d,"__esModule",{value:!0});function V(e){return String.fromCharCode(parseInt(e.slice(1),16))}function j(e){return btoa(encodeURIComponent(e).replace(/%[0-9A-F]{2}/g,V))}d.encode=j;function z(e){return`%${`00${e.charCodeAt(0).toString(16)}`.slice(-2)}`}function B(e){return decodeURIComponent(Array.from(atob(e),z).join(""))}d.decode=B;Object.defineProperty(k,"__esModule",{value:!0});const I=d;function P(e){return I.decode(e.replace(/\-/g,"+").replace(/_/g,"/"))}var x=k.decode=P;function $(e){return I.encode(e).replace(/\//g,"_").replace(/\+/g,"-").replace(/=+$/,"")}var W=k.encode=$;const H=e=>e.filter(Boolean),E=e=>{try{return new URL(e)}catch{return null}},M=e=>{switch(e){case t.ImageFormatToken.AVIF:return{contentType:"image/avif",extension:"avif"};case t.ImageFormatToken.JPEG:return{contentType:"image/jpeg",extension:"jpg"};case t.ImageFormatToken.WEBP:return{contentType:"image/webp",extension:"webp"};default:throw new Error(`formatToContentType: unknown format ${e}`)}},f=(e="")=>/^[A-Za-z0-9_-]*$/.test(e),G=(e,n)=>{const r=e/n,o=t.BASE_BLURHASH_DIMENSIONS;let s=t.BASE_BLURHASH_DIMENSIONS;return r>=1.6&&(s=3),[o,s]},w=e=>{const{altText:n,bcc:r,blurhash:o,blurhashX:s,blurhashY:i,sourceHeight:l,sourceWidth:c,url:u}=e;if(!u||!c||!l)return new Error("Cannot construct visionary code: missing required url/width/height");const a=[u,c,l];return!r||(a.push(r),!o||!s||!i)||(a.push(o,s,i),n&&n.length&&a.push(n)),T(a)},T=e=>W(e.join(t.V_CODE_SEPARATOR)),b=e=>{if(typeof e!="string")return null;const n=e.trim();if(!n.length||!f(n))return null;const r=x(n);if(!r)return null;const o=r.split(t.V_CODE_SEPARATOR);if(o.length<3)return null;const[s,i,l,c,u,a,v,L]=o,O=s.trim();if(!O.length)return console.error("Cannot parse code, empty file id"),null;const g=Number(i.trim()),m=Number(l.trim());if(isNaN(g)||isNaN(m)||!g||!m)return console.error("Cannot parse Visionary Code: invalid image dimensions",i,l),null;const p=Number(a)??0,h=Number(v)??0;return p<1||h<1?(console.error("Cannot parse Visionary Code: invalid blurhash x, y component dimensions",p,h),null):{altText:L,bcc:c,blurhash:u,blurhashX:p,blurhashY:h,sourceHeight:m,sourceWidth:g,url:O}};class X extends Error{constructor(){super(...arguments);U(this,"message","invalid endpoint URL (does it contain http/https?)")}}const C=e=>e===t.UrlOptionToken.DEBUG,S=e=>e===t.UrlOptionToken.DOWNLOAD,_=e=>e===t.UrlOptionToken.FOLLOW,A=e=>Object.values(t.ImageFormatToken).includes(e),y=e=>Object.values(t.ImageSizeToken).includes(e),D=(e=[])=>{const n={};for(const r of e)y(r)?n.size=t.ImageSizeToken[r]:C(r)?n.debug=!0:S(r)?n.download=!0:_(r)?n.follow=!0:A(r)&&(n.format=r);return n},Y=e=>{if(!e||typeof e!="object")return null;const n=[];return e.debug&&n.push(t.UrlOptionToken.DEBUG),e.download&&n.push(t.UrlOptionToken.DOWNLOAD),e.follow&&n.push(t.UrlOptionToken.FOLLOW),e.format&&e.format!==t.ImageFormatToken.AUTO&&n.push(e.format),e.size&&y(e.size)&&n.push(e.size),n.length?n.sort().join(","):null},q=(e="")=>D(e.split(",")),J=e=>{if(f(e)){const r=b(e);if(r)return{fields:r,options:{}}}return F(e)},F=e=>{if(!e)return null;const n=e.trim();if(!n)return null;try{const r=K(n);if(!r)return null;const{code:o,optionTokens:s}=r,i=b(o);if(!i)return null;const l=D(s);return{fields:i,options:l}}catch(r){r instanceof Error?console.error(`Error parsing URL: ${r.message}`,r):console.error("uncaught error",r)}return null},Z=(e,n)=>{const r=w(e);if(r instanceof Error)return null;let o=null;if(n!=null&&n.endpoint&&(o=E(n==null?void 0:n.endpoint),!o))throw new X("Cannot construct URL: bad endpoint. Ensure endpoint starts with http:// or https://");o||(o=E(t.DEFAULT_ENDPOINT));const s=[o.origin,"image",r],i=n?Y(n):null;return i&&s.push(i),n!=null&&n.filename?s.push(n.filename):s.push("image.jpg"),s.join("/")},K=e=>{try{const n=new URL(e),r=H(n.pathname.split("/"));if(r[0]!=="image"||![3,4].includes(r.length))throw new Error("Unrecognized URL");const o=r[1].trim();if(!o.length||!f(o))throw new Error("URL is not formatted as base64url");if(r.length===4){const s=r[2].split(",");return{code:o,optionTokens:s}}if(r.length===3)return{code:o,optionTokens:[]}}catch{return null}return null};exports.ImageFormatToken=t.ImageFormatToken;exports.ImageSizeToken=t.ImageSizeToken;exports.UrlOptionToken=t.UrlOptionToken;exports.formatToContentType=M;exports.generateVisionaryCode=w;exports.generateVisionaryUrl=Z;exports.isBase64UrlEncoded=f;exports.isDebugToken=C;exports.isDownloadToken=S;exports.isFollowToken=_;exports.isImageFormatToken=A;exports.isImageSizeToken=y;exports.parseOptionsString=q;exports.parseVisionaryCode=b;exports.parseVisionaryString=J;exports.parseVisionaryUrl=F;exports.suggestedBlurhashComponentDimensions=G;