// @bun
var{defineProperty:P,getOwnPropertyNames:Y,getOwnPropertyDescriptor:Z}=Object,$=Object.prototype.hasOwnProperty;function l(q){return this[q]}var V=(q)=>{var w=(U??=new WeakMap).get(q),z;if(w)return w;if(w=P({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function"){for(var C of Y(q))if(!$.call(w,C))P(w,C,{get:l.bind(q,C),enumerable:!(z=Z(q,C))||z.enumerable})}return U.set(q,w),w},U;var B=(q)=>q;function F(q,w){this[q]=B.bind(null,w)}var j=(q,w)=>{for(var z in w)P(q,z,{get:w[z],enumerable:!0,configurable:!0,set:F.bind(w,z)})};var u=(q,w)=>()=>(q&&(w=q(q=0)),w);var A=import.meta.require;import Q from"fs";import W from"path";var L=".stx/story/bookmarks.json",M=1;class N{storage;storagePath;dirty=!1;constructor(q){this.storagePath=W.join(q,L),this.storage=this.loadStorage()}loadStorage(){try{let q=Q.readFileSync(this.storagePath,"utf-8");return JSON.parse(q)}catch{return{version:M,bookmarks:[]}}}async save(){if(!this.dirty)return;let q=W.dirname(this.storagePath);await Q.promises.mkdir(q,{recursive:!0}),await Q.promises.writeFile(this.storagePath,JSON.stringify(this.storage,null,2)),this.dirty=!1}addBookmark(q,w,z,C={}){let H={id:T(),name:C.name||`${q}/${w}`,storyId:q,variantId:w,props:z,created:Date.now(),notes:C.notes,color:C.color};return this.storage.bookmarks.push(H),this.dirty=!0,H}getAllBookmarks(){return[...this.storage.bookmarks]}getBookmarksForStory(q){return this.storage.bookmarks.filter((w)=>w.storyId===q)}getBookmark(q){return this.storage.bookmarks.find((w)=>w.id===q)}isBookmarked(q,w){return this.storage.bookmarks.some((z)=>z.storyId===q&&z.variantId===w)}updateBookmark(q,w){let z=this.getBookmark(q);if(!z)return;return Object.assign(z,w),this.dirty=!0,z}deleteBookmark(q){let w=this.storage.bookmarks.findIndex((z)=>z.id===q);if(w===-1)return!1;return this.storage.bookmarks.splice(w,1),this.dirty=!0,!0}toggleBookmark(q,w,z){let C=this.storage.bookmarks.find((J)=>J.storyId===q&&J.variantId===w);if(C)return this.deleteBookmark(C.id),{added:!1};return{added:!0,bookmark:this.addBookmark(q,w,z)}}exportBookmarks(){return JSON.stringify(this.storage,null,2)}importBookmarks(q,w=!0){let z=JSON.parse(q),C=0;for(let H of z.bookmarks){if(w){let J=this.storage.bookmarks.find((X)=>X.id===H.id);if(J)Object.assign(J,H);else this.storage.bookmarks.push(H)}else this.storage.bookmarks.push(H);C++}if(C>0)this.dirty=!0;return C}}function T(){return`bm-${Date.now()}-${Math.random().toString(36).slice(2,8)}`}function _(q){return`
    <div class="bookmarks-panel">
      <div class="bookmarks-header">
        <h3>\uD83D\uDCD1 Bookmarks</h3>
        <button onclick="addCurrentBookmark()" title="Bookmark current">+</button>
      </div>
      <div class="bookmarks-list">
        ${q.map((z)=>`
    <div class="bookmark-item" data-bookmark-id="${z.id}" style="${z.color?`border-left: 3px solid ${z.color}`:""}">
      <div class="bookmark-info">
        <div class="bookmark-name">${z.name}</div>
        <div class="bookmark-path">${z.storyId} / ${z.variantId}</div>
        ${z.notes?`<div class="bookmark-notes">${z.notes}</div>`:""}
      </div>
      <div class="bookmark-actions">
        <button onclick="loadBookmark('${z.id}')" title="Load">\u25B6</button>
        <button onclick="deleteBookmark('${z.id}')" title="Delete">\xD7</button>
      </div>
    </div>
  `).join("")||'<div class="bookmarks-empty">No bookmarks yet</div>'}
      </div>
    </div>
  `}function c(){return`
    .bookmarks-panel {
      padding: 12px;
      background: var(--bg-secondary);
      border-radius: 8px;
    }
    .bookmarks-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 12px;
    }
    .bookmarks-header h3 {
      margin: 0;
      font-size: 14px;
    }
    .bookmarks-header button {
      padding: 4px 8px;
      background: var(--primary);
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }
    .bookmarks-list {
      display: flex;
      flex-direction: column;
      gap: 8px;
      max-height: 300px;
      overflow-y: auto;
    }
    .bookmark-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 8px;
      background: var(--bg);
      border-radius: 4px;
      border: 1px solid var(--border);
    }
    .bookmark-name {
      font-weight: 500;
      font-size: 13px;
    }
    .bookmark-path {
      font-size: 11px;
      color: var(--text-secondary);
    }
    .bookmark-notes {
      font-size: 11px;
      color: var(--text-secondary);
      font-style: italic;
      margin-top: 4px;
    }
    .bookmark-actions {
      display: flex;
      gap: 4px;
    }
    .bookmark-actions button {
      padding: 4px 8px;
      background: transparent;
      border: 1px solid var(--border);
      border-radius: 4px;
      cursor: pointer;
      font-size: 12px;
    }
    .bookmark-actions button:hover {
      background: var(--bg-secondary);
    }
    .bookmarks-empty {
      text-align: center;
      color: var(--text-secondary);
      font-size: 13px;
      padding: 20px;
    }
  `}function h(){return`
<script>
window.__stxBookmarks = {
  async loadBookmark(id) {
    const response = await fetch('/api/bookmarks/' + id);
    const bookmark = await response.json();

    if (bookmark) {
      window.__stxHotSwap?.selectVariant(bookmark.storyId, bookmark.variantId);
      if (bookmark.props) {
        window.dispatchEvent(new CustomEvent('stx:props-change', {
          detail: { props: bookmark.props }
        }));
      }
    }
  },

  async addCurrentBookmark() {
    const name = prompt('Bookmark name:');
    if (!name) return;

    const storyId = window.__stxHotSwap?.storyId;
    const variantId = window.__stxHotSwap?.variantId;
    const props = window.__stxProps || {};

    if (!storyId || !variantId) {
      alert('No story selected');
      return;
    }

    await fetch('/api/bookmarks', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ name, storyId, variantId, props })
    });

    window.__stxStory?.refreshBookmarks?.();
  },

  async deleteBookmark(id) {
    if (!confirm('Delete this bookmark?')) return;

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

  async toggleCurrent() {
    const storyId = window.__stxHotSwap?.storyId;
    const variantId = window.__stxHotSwap?.variantId;
    const props = window.__stxProps || {};

    if (!storyId || !variantId) return;

    await fetch('/api/bookmarks/toggle', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ storyId, variantId, props })
    });

    window.__stxStory?.refreshBookmarks?.();
  }
};

window.loadBookmark = window.__stxBookmarks.loadBookmark.bind(window.__stxBookmarks);
window.addCurrentBookmark = window.__stxBookmarks.addCurrentBookmark.bind(window.__stxBookmarks);
window.deleteBookmark = window.__stxBookmarks.deleteBookmark.bind(window.__stxBookmarks);
</script>
`}export{h as getBookmarksScript,c as getBookmarksPanelStyles,_ as getBookmarksPanelHtml,N as BookmarksManager};
