// @bun
var{defineProperty:Z,getOwnPropertyNames:$,getOwnPropertyDescriptor:k}=Object,G=Object.prototype.hasOwnProperty;function R(j){return this[j]}var y=(j)=>{var q=(_??=new WeakMap).get(j),A;if(q)return q;if(q=Z({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var v of $(j))if(!G.call(q,v))Z(q,v,{get:R.bind(j,v),enumerable:!(A=k(j,v))||A.enumerable})}return _.set(j,q),q},_;var V=(j)=>j;function K(j,q){this[j]=V.bind(null,q)}var T=(j,q)=>{for(var A in q)Z(j,A,{get:q[A],enumerable:!0,configurable:!0,set:K.bind(q,A)})};var M=(j,q)=>()=>(j&&(q=j(j=0)),q);var b=import.meta.require;async function W(j,q){let{endpoint:A,method:v="POST",headers:I={},expirationSeconds:J=3600,oneTimeUse:L=!1,params:D={}}=q,N=JSON.stringify({url:j,expirationSeconds:J,oneTimeUse:L,...D}),F=await fetch(A,{method:v,headers:{"Content-Type":"application/json",...I},body:v!=="GET"?N:void 0});if(!F.ok)throw Error(`Failed to sign URL: ${F.status} ${F.statusText}`);let H=await F.json();return{url:H.url||H.signedUrl,signature:H.signature,expires:H.expires||Date.now()+J*1000,oneTimeUse:H.oneTimeUse||L}}async function w(j,q){let{endpoint:A,method:v="POST",headers:I={},expirationSeconds:J=3600,oneTimeUse:L=!1,params:D={}}=q,N=JSON.stringify({urls:j,expirationSeconds:J,oneTimeUse:L,...D}),F=await fetch(A,{method:v,headers:{"Content-Type":"application/json",...I},body:v!=="GET"?N:void 0});if(!F.ok)throw Error(`Failed to batch sign URLs: ${F.status} ${F.statusText}`);let H=await F.json(),Q=new Map,X=new Map;if(Array.isArray(H.results))for(let B of H.results)if(B.error)X.set(B.url,B.error);else Q.set(B.url||B.originalUrl,{url:B.signedUrl||B.url,signature:B.signature,expires:B.expires||Date.now()+J*1000,oneTimeUse:B.oneTimeUse||L});else if(typeof H.signatures==="object"){for(let[B,Y]of Object.entries(H.signatures)){let P=Y;Q.set(B,{url:P.signedUrl||P.url||B,signature:P.signature,expires:P.expires||Date.now()+J*1000,oneTimeUse:L})}if(H.errors)for(let[B,Y]of Object.entries(H.errors))X.set(B,Y)}return{signatures:Q,errors:X}}async function S(j,q,A=10){let v=new Map,I=new Map,J=[];for(let D=0;D<j.length;D+=A)J.push(j.slice(D,D+A));let L=await Promise.all(J.map((D)=>w(D,q)));for(let D of L){for(let[N,F]of D.signatures)v.set(N,F);for(let[N,F]of D.errors)I.set(N,F)}return{signatures:v,errors:I}}function E(j){return j.expires>Date.now()}function z(j){return Math.max(0,j.expires-Date.now())}function C(j,q=300000){return z(j)<q}async function U(j){if(j.check)return await j.check();return!0}function p(j){let q={};if(j.role)q.role=j.role;if(j.permission)q.permission=j.permission;if(j.userId)q.userId=j.userId;return q}async function m(j,q){let A=`${j}:${q.endpoint}`,v=O.get(A);if(v&&E(v)&&!C(v))return v;let I=await W(j,q);return O.set(A,I),I}function l(){O.clear()}function g(){let j=0,q=Date.now();for(let[A,v]of O)if(v.expires<=q)O.delete(A),j++;return j}function h(){return`
(function() {
  'use strict';

  if (typeof window === 'undefined') return;

  window.STX = window.STX || {};
  var cache = new Map();

  window.STX.signUrl = async function(src, config) {
    var cacheKey = src + ':' + config.endpoint;
    var cached = cache.get(cacheKey);

    if (cached && cached.expires > Date.now()) {
      return cached;
    }

    var response = await fetch(config.endpoint, {
      method: config.method || 'POST',
      headers: Object.assign({ 'Content-Type': 'application/json' }, config.headers || {}),
      body: JSON.stringify({ url: src, expirationSeconds: config.expirationSeconds || 3600 })
    });

    if (!response.ok) throw new Error('Failed to sign URL');

    var data = await response.json();
    var signed = {
      url: data.url || data.signedUrl,
      signature: data.signature,
      expires: data.expires || Date.now() + (config.expirationSeconds || 3600) * 1000
    };

    cache.set(cacheKey, signed);
    return signed;
  };

  window.STX.batchSignUrls = async function(sources, config) {
    var response = await fetch(config.endpoint, {
      method: config.method || 'POST',
      headers: Object.assign({ 'Content-Type': 'application/json' }, config.headers || {}),
      body: JSON.stringify({ urls: sources, expirationSeconds: config.expirationSeconds || 3600 })
    });

    if (!response.ok) throw new Error('Failed to batch sign URLs');

    return response.json();
  };

  window.STX.loadProtectedMedia = async function(element, config) {
    var src = element.dataset.protectedSrc;
    if (!src) return;

    try {
      var signed = await window.STX.signUrl(src, config);
      if (element.tagName === 'IMG') {
        element.src = signed.url;
      }
else if (element.tagName === 'VIDEO') {
        element.src = signed.url;
        element.load();
      }
else if (element.tagName === 'SOURCE') {
        element.src = signed.url;
        element.parentElement.load();
      }
    }
catch (error) {
      console.error('Failed to load protected media:', error);
      if (element.dataset.fallback) {
        element.src = element.dataset.fallback;
      }
    }
  };
})();
`.trim()}var O;var x=M(()=>{O=new Map});x();export{S as smartBatchSign,W as signUrl,C as shouldRefreshSignature,g as pruneSignatureCache,E as isSignatureValid,z as getTimeUntilExpiry,m as getCachedSignature,h as generateSignatureRuntime,l as clearSignatureCache,U as checkAuthAccess,p as buildAuthParams,w as batchSignUrls};
