import{cryptoLib as o}from"./utils.js";function u(e){return"then"in e?e:new Promise(t=>{e.oncomplete=r=>{t(r.target.result)}})}function l(e){const t=new ArrayBuffer(e.length),r=new Uint8Array(t);for(let i=0,n=e.length;i<n;i++)r[i]=e.charCodeAt(i);return t}const a=()=>{const e=l(atob(typeof REDOCLY_PUBLIC_KEY<"u"?REDOCLY_PUBLIC_KEY:"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMHjNir5fXx/ZXoaEeXQ5XyxbNJ4YJYczoCRdSkL6NLzw1FHnng5Vfcgk5+bvox9QRYRbuk84mA4f2NhywDbXKECAwEAAQ=="));return u(o?.subtle.importKey("spki",e,{name:"RSASSA-PKCS1-v1_5",hash:"SHA-256"},!0,["verify"]))};function f(e){if(window.TextEncoder)return new TextEncoder().encode(e);const t=unescape(encodeURIComponent(e)),r=new Uint8Array(t.length);for(let i=0;i<t.length;i++)r[i]=t.charCodeAt(i);return r}async function c(e,t){return u(o?.subtle.verify({name:"RSASSA-PKCS1-v1_5",hash:"SHA-256"},await a(),l(atob(t)),f(e)))}function s(e,t){return t===!0?!0:Array.isArray(t)&&typeof window<"u"?e==="localhost"||e==="127.0.0.1"||e.endsWith(".localhost")||t.find(r=>r===e||e.endsWith("."+r))?!0:!!t.find(r=>{if(r.indexOf("*")!==r.lastIndexOf("*"))return;const[i,n]=r.split("*");return n&&e.startsWith(i)&&e.endsWith(n)&&n.split(".").length>2}):!1}async function d(e){if(process.env.NODE_ENV==="development")return{valid:!0,allowed:!0};const t=window.location.hostname;if(!e)return t==="localhost"||t==="127.0.0.1"||t.endsWith(".localhost")?{local:!0}:{};const[r,i]=e.split(".");let n;try{if(!await c(atob(r),i))return{valid:!1};n=JSON.parse(atob(r))}catch{return{valid:!1,cryptoMissing:!0}}return n.valid=!0,(!n.e||Date.now()/1e3>n.e)&&(n.expired=!0),n.allowed=s(t,n.h),n}export{d as default,s as isHostAllowed};
