// @bun
var{defineProperty:T,getOwnPropertyNames:W,getOwnPropertyDescriptor:Y}=Object,Z=Object.prototype.hasOwnProperty;function _(M){return this[M]}var x=(M)=>{var n=(F??=new WeakMap).get(M),u;if(n)return n;if(n=T({},"__esModule",{value:!0}),M&&typeof M==="object"||typeof M==="function"){for(var g of W(M))if(!Z.call(n,g))T(n,g,{get:_.bind(M,g),enumerable:!(u=Y(M,g))||u.enumerable})}return F.set(M,n),n},F;var $=(M)=>M;function d(M,n){this[M]=$.bind(null,n)}var S=(M,n)=>{for(var u in n)T(M,u,{get:n[u],enumerable:!0,configurable:!0,set:d.bind(n,u)})};var v=(M,n)=>()=>(M&&(n=M(M=0)),n);var N=import.meta.require;function f(M){let{endpoint:n,accept:u,maxSize:g,maxFiles:I=1,fieldName:U="file",preview:X=!1,dropzone:j=!0,multiple:J=!1,autoUpload:K=!0,showProgress:L=!0,disabled:q=!1,placeholder:O="Drop files here or click to select",label:B}=M,C=M.class||"",D=M.style||"",E=M.id||`stx-upload-${Math.random().toString(36).slice(2,8)}`,k=Array.isArray(u)?u.join(","):u,Q=`
<div id="${E}" class="stx-upload${j?" stx-upload-dropzone":""}${q?" stx-upload-disabled":""}${C?" "+C:""}"${D?` style="${D}"`:""}>
  ${B?`<label class="stx-upload-label">${G(B)}</label>`:""}
  <div class="stx-upload-area" data-stx-upload-area>
    <input
      type="file"
      name="${U}"
      ${k?`accept="${k}"`:""}
      ${J?"multiple":""}
      ${q?"disabled":""}
      class="stx-upload-input"
      data-stx-upload-input
    />
    <div class="stx-upload-placeholder">
      <svg class="stx-upload-icon" viewBox="0 0 24 24" width="48" height="48">
        <path fill="currentColor" d="M9 16h6v-6h4l-7-7-7 7h4v6zm-4 2h14v2H5v-2z"/>
      </svg>
      <span class="stx-upload-text">${G(O)}</span>
      ${g?`<span class="stx-upload-hint">Max size: ${w(g)}</span>`:""}
    </div>
  </div>
  ${X?'<div class="stx-upload-previews" data-stx-upload-previews></div>':""}
  ${L?'<div class="stx-upload-progress" data-stx-upload-progress style="display:none;"><div class="stx-upload-progress-bar"></div><span class="stx-upload-progress-text">0%</span></div>':""}
</div>
`.trim(),R=`
(function() {
  var container = document.getElementById('${E}');
  if (!container) return;

  var input = container.querySelector('[data-stx-upload-input]');
  var area = container.querySelector('[data-stx-upload-area]');
  var previews = container.querySelector('[data-stx-upload-previews]');
  var progress = container.querySelector('[data-stx-upload-progress]');
  var progressBar = progress ? progress.querySelector('.stx-upload-progress-bar') : null;
  var progressText = progress ? progress.querySelector('.stx-upload-progress-text') : null;

  var config = {
    endpoint: '${h(n)}',
    fieldName: '${U}',
    maxSize: ${g||"null"},
    maxFiles: ${I},
    accept: ${k?JSON.stringify(k.split(",")):"null"},
    autoUpload: ${K}
  };

  // Handle file selection
  input.addEventListener('change', function(e) {
    var files = Array.from(e.target.files);
    handleFiles(files);
  });

  // Dropzone events
  if (${j}) {
    area.addEventListener('dragover', function(e) {
      e.preventDefault();
      area.classList.add('stx-upload-dragover');
    });

    area.addEventListener('dragleave', function(e) {
      e.preventDefault();
      area.classList.remove('stx-upload-dragover');
    });

    area.addEventListener('drop', function(e) {
      e.preventDefault();
      area.classList.remove('stx-upload-dragover');
      var files = Array.from(e.dataTransfer.files);
      handleFiles(files);
    });
  }

  function handleFiles(files) {
    files = files.slice(0, config.maxFiles);

    // Validate files
    files = files.filter(function(file) {
      var valid = STX.validateFile(file, config);
      if (!valid.valid) {
        console.warn('File validation failed:', file.name, valid.error);
        return false;
      }
      return true;
    });

    if (files.length === 0) return;

    // Generate previews
    if (${X} && previews) {
      previews.innerHTML = '';
      files.forEach(function(file) {
        createPreview(file);
      });
    }

    // Auto upload
    if (config.autoUpload) {
      uploadFiles(files);
    }
  }

  function createPreview(file) {
    var item = document.createElement('div');
    item.className = 'stx-upload-preview-item';

    if (file.type.startsWith('image/')) {
      var reader = new FileReader();
      reader.onload = function(e) {
        item.innerHTML = '<img src="' + e.target.result + '" alt="' + escapeHtml(file.name) + '" /><span>' + escapeHtml(file.name) + '</span>';
      };
      reader.readAsDataURL(file);
    }
else {
      item.innerHTML = '<div class="stx-upload-preview-file">' + getFileIcon(file.type) + '</div><span>' + escapeHtml(file.name) + '</span>';
    }

    previews.appendChild(item);
  }

  function uploadFiles(files) {
    if (progress) progress.style.display = 'block';

    var uploaded = 0;
    var total = files.length;

    files.forEach(function(file) {
      STX.uploadFile(file, config, function(prog) {
        if (progressBar) progressBar.style.width = prog.percent + '%';
        if (progressText) progressText.textContent = prog.percent + '%';
      }).then(function(result) {
        uploaded++;
        if (result.success) {
          container.dispatchEvent(new CustomEvent('upload', { detail: result }));
        }
else {
          container.dispatchEvent(new CustomEvent('error', { detail: result }));
        }

        if (uploaded === total) {
          container.dispatchEvent(new CustomEvent('complete', { detail: { files: files.length } }));
          if (progress) {
            setTimeout(function() { progress.style.display = 'none'; }, 1000);
          }
        }
      });
    });
  }

  function escapeHtml(str) {
    return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  }

  function getFileIcon(type) {
    if (type.startsWith('video/')) return '\uD83C\uDFAC';
    if (type.startsWith('audio/')) return '\uD83C\uDFB5';
    if (type.includes('pdf')) return '\uD83D\uDCC4';
    if (type.includes('zip') || type.includes('rar')) return '\uD83D\uDCE6';
    return '\uD83D\uDCC1';
  }
})();
`.trim(),V=`
.stx-upload {
  font-family: system-ui, -apple-system, sans-serif;
}

.stx-upload-label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
}

.stx-upload-area {
  position: relative;
  border: 2px dashed #d1d5db;
  border-radius: 0.5rem;
  padding: 2rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background-color 0.2s;
}

.stx-upload-area:hover,
.stx-upload-dragover {
  border-color: #3b82f6;
  background-color: #eff6ff;
}

.stx-upload-input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.stx-upload-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  color: #6b7280;
}

.stx-upload-icon {
  color: #9ca3af;
}

.stx-upload-text {
  font-size: 0.875rem;
}

.stx-upload-hint {
  font-size: 0.75rem;
  color: #9ca3af;
}

.stx-upload-previews {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1rem;
}

.stx-upload-preview-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.5rem;
  border: 1px solid #e5e7eb;
  border-radius: 0.25rem;
  background: #f9fafb;
  max-width: 100px;
}

.stx-upload-preview-item img {
  max-width: 80px;
  max-height: 80px;
  object-fit: cover;
  border-radius: 0.25rem;
}

.stx-upload-preview-item span {
  font-size: 0.625rem;
  color: #6b7280;
  margin-top: 0.25rem;
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stx-upload-preview-file {
  font-size: 2rem;
}

.stx-upload-progress {
  margin-top: 1rem;
  background: #e5e7eb;
  border-radius: 0.25rem;
  height: 0.5rem;
  position: relative;
  overflow: hidden;
}

.stx-upload-progress-bar {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  background: #3b82f6;
  transition: width 0.3s;
  width: 0;
}

.stx-upload-progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 0.625rem;
  color: white;
  text-shadow: 0 0 2px rgba(0,0,0,0.5);
}

.stx-upload-disabled {
  opacity: 0.5;
  pointer-events: none;
}
`.trim();return{html:Q,script:R,css:V}}function G(M){return M.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function h(M){return M.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#039;")}function w(M){if(M<1024)return`${M} B`;if(M<1048576)return`${(M/1024).toFixed(1)} KB`;return`${(M/1048576).toFixed(1)} MB`}var H=()=>{};H();export{f as renderMediaUpload};
