// @bun
var{defineProperty:U,getOwnPropertyNames:F,getOwnPropertyDescriptor:L}=Object,M=Object.prototype.hasOwnProperty;function P(k){return this[k]}var h=(k)=>{var z=(Z??=new WeakMap).get(k),B;if(z)return z;if(z=U({},"__esModule",{value:!0}),k&&typeof k==="object"||typeof k==="function"){for(var H of F(k))if(!M.call(z,H))U(z,H,{get:P.bind(k,H),enumerable:!(B=L(k,H))||B.enumerable})}return Z.set(k,z),z},Z;var X=(k)=>k;function b(k,z){this[k]=X.bind(null,z)}var Q=(k,z)=>{for(var B in z)U(k,B,{get:z[B],enumerable:!0,configurable:!0,set:b.bind(z,B)})};var V=(k,z)=>()=>(k&&(z=k(k=0)),z);var f=import.meta.require;import W from"fs";import $ from"path";var j=".stx/story/presets.json",Y=1,q=10;class v{storage;storagePath;dirty=!1;constructor(k){this.storagePath=$.join(k,j),this.storage=this.loadStorage()}loadStorage(){try{let k=W.readFileSync(this.storagePath,"utf-8"),z=JSON.parse(k);if(z.version!==Y)return this.migrateStorage(z);return z}catch{return{version:Y,presets:[],recentIds:[]}}}migrateStorage(k){return{version:Y,presets:k.presets||[],recentIds:k.recentIds||[]}}async save(){if(!this.dirty)return;let k=$.dirname(this.storagePath);await W.promises.mkdir(k,{recursive:!0}),await W.promises.writeFile(this.storagePath,JSON.stringify(this.storage,null,2)),this.dirty=!1}createPreset(k,z,B={}){let H={id:D(),name:k,description:B.description,props:z,componentId:B.componentId,created:Date.now(),usageCount:0,tags:B.tags};return this.storage.presets.push(H),this.dirty=!0,H}getPreset(k){return this.storage.presets.find((z)=>z.id===k)}getAllPresets(){return[...this.storage.presets]}getPresetsForComponent(k){return this.storage.presets.filter((z)=>!z.componentId||z.componentId===k)}getRecentPresets(){return this.storage.recentIds.map((k)=>this.getPreset(k)).filter((k)=>k!==void 0)}usePreset(k){let z=this.getPreset(k);if(!z)return;return z.lastUsed=Date.now(),z.usageCount++,this.storage.recentIds=[k,...this.storage.recentIds.filter((B)=>B!==k)].slice(0,q),this.dirty=!0,z}updatePreset(k,z){let B=this.getPreset(k);if(!B)return;return Object.assign(B,z),this.dirty=!0,B}deletePreset(k){let z=this.storage.presets.findIndex((B)=>B.id===k);if(z===-1)return!1;return this.storage.presets.splice(z,1),this.storage.recentIds=this.storage.recentIds.filter((B)=>B!==k),this.dirty=!0,!0}searchPresets(k){let z=k.toLowerCase();return this.storage.presets.filter((B)=>{if(B.name.toLowerCase().includes(z))return!0;if(B.description?.toLowerCase().includes(z))return!0;if(B.tags?.some((H)=>H.toLowerCase().includes(z)))return!0;return!1})}getMostUsedPresets(k=5){return[...this.storage.presets].sort((z,B)=>B.usageCount-z.usageCount).slice(0,k)}exportPresets(k){let z=k?this.storage.presets.filter((B)=>k.includes(B.id)):this.storage.presets;return JSON.stringify({presets:z},null,2)}importPresets(k,z=!1){let B=JSON.parse(k),H=0;for(let J of B.presets){let K=this.storage.presets.find((C)=>C.id===J.id);if(K&&!z)continue;if(K)Object.assign(K,J);else this.storage.presets.push(J);H++}if(H>0)this.dirty=!0;return H}}function D(){return`preset-${Date.now()}-${Math.random().toString(36).slice(2,8)}`}function N(k){return`
    <div class="presets-panel">
      <div class="presets-header">
        <h3>Presets</h3>
        <button class="preset-save-btn" onclick="saveCurrentAsPreset()">+ Save Current</button>
      </div>
      <div class="presets-list">
        ${k.map((B)=>`
    <div class="preset-item" data-preset-id="${B.id}">
      <div class="preset-info">
        <div class="preset-name">${B.name}</div>
        ${B.description?`<div class="preset-desc">${B.description}</div>`:""}
      </div>
      <div class="preset-actions">
        <button class="preset-apply" onclick="applyPreset('${B.id}')">Apply</button>
        <button class="preset-delete" onclick="deletePreset('${B.id}')">\xD7</button>
      </div>
    </div>
  `).join("")||'<div class="presets-empty">No presets saved</div>'}
      </div>
    </div>
  `}function c(){return`
    .presets-panel {
      padding: 12px;
      background: var(--bg-secondary);
      border-radius: 8px;
    }
    .presets-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 12px;
    }
    .presets-header h3 {
      margin: 0;
      font-size: 14px;
    }
    .preset-save-btn {
      padding: 4px 8px;
      font-size: 12px;
      background: var(--primary);
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }
    .presets-list {
      display: flex;
      flex-direction: column;
      gap: 8px;
    }
    .preset-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 8px;
      background: var(--bg);
      border-radius: 4px;
      border: 1px solid var(--border);
    }
    .preset-name {
      font-weight: 500;
      font-size: 13px;
    }
    .preset-desc {
      font-size: 11px;
      color: var(--text-secondary);
    }
    .preset-actions {
      display: flex;
      gap: 4px;
    }
    .preset-apply {
      padding: 4px 8px;
      font-size: 11px;
      background: var(--bg-secondary);
      border: 1px solid var(--border);
      border-radius: 4px;
      cursor: pointer;
    }
    .preset-delete {
      padding: 4px 8px;
      font-size: 11px;
      background: transparent;
      border: none;
      color: var(--text-secondary);
      cursor: pointer;
    }
    .preset-delete:hover {
      color: #ef4444;
    }
    .presets-empty {
      text-align: center;
      color: var(--text-secondary);
      font-size: 13px;
      padding: 20px;
    }
  `}function w(){return`
<script>
window.__stxPresets = {
  async saveCurrentAsPreset() {
    const name = prompt('Preset name:');
    if (!name) return;

    const props = window.__stxProps || {};
    const storyId = window.__stxStory?.currentStoryId;

    const response = await fetch('/api/presets', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ name, props, componentId: storyId })
    });

    if (response.ok) {
      window.__stxStory?.refreshPresets?.();
    }
  },

  async applyPreset(id) {
    const response = await fetch('/api/presets/' + id + '/use', { method: 'POST' });
    const preset = await response.json();

    if (preset.props) {
      window.dispatchEvent(new CustomEvent('stx:props-change', {
        detail: { props: preset.props }
      }));
    }
  },

  async deletePreset(id) {
    if (!confirm('Delete this preset?')) return;

    await fetch('/api/presets/' + id, { method: 'DELETE' });
    window.__stxStory?.refreshPresets?.();
  }
};

// Expose to global scope
window.saveCurrentAsPreset = window.__stxPresets.saveCurrentAsPreset;
window.applyPreset = window.__stxPresets.applyPreset;
window.deletePreset = window.__stxPresets.deletePreset;
</script>
`}export{w as getPresetsScript,c as getPresetsPanelStyles,N as getPresetsPanelHtml,v as PresetsManager};
