// @bun
var{defineProperty:k,getOwnPropertyNames:m,getOwnPropertyDescriptor:q}=Object,u=Object.prototype.hasOwnProperty;function w(b){return this[b]}var B=(b)=>{var f=(l??=new WeakMap).get(b),g;if(f)return f;if(f=k({},"__esModule",{value:!0}),b&&typeof b==="object"||typeof b==="function"){for(var j of m(b))if(!u.call(f,j))k(f,j,{get:w.bind(b,j),enumerable:!(g=q(b,j))||g.enumerable})}return l.set(b,f),f},l;var z=(b)=>b;function A(b,f){this[b]=z.bind(null,f)}var C=(b,f)=>{for(var g in f)k(b,g,{get:f[g],enumerable:!0,configurable:!0,set:A.bind(f,g)})};var D=(b,f)=>()=>(b&&(f=b(b=0)),f);var E=import.meta.require;function H(){return{selectedStoryId:null,selectedVariantId:null,recentStories:[],maxRecent:10,searchQuery:"",focusedIndex:-1}}function J(b){if(!b||b==="#")return{storyId:null,variantId:null};let g=(b.startsWith("#")?b.slice(1):b).split("/").filter(Boolean);return{storyId:g[0]||null,variantId:g[1]||null}}function K(b,f){if(!b)return"";if(!f)return`#/${b}`;return`#/${b}/${f}`}function L(b,f){let g=b.recentStories.indexOf(f);if(g!==-1)b.recentStories.splice(g,1);if(b.recentStories.unshift(f),b.recentStories.length>b.maxRecent)b.recentStories.pop()}function N(){return`
    (function() {
      const state = {
        focusedIndex: -1,
        items: [],
      };

      function updateItems() {
        state.items = Array.from(document.querySelectorAll('.stx-tree-item, .stx-search-result'));
      }

      function focusItem(index) {
        // Remove previous focus
        state.items.forEach(item => item.classList.remove('focused'));

        // Clamp index
        if (index < 0) index = state.items.length - 1;
        if (index >= state.items.length) index = 0;

        state.focusedIndex = index;

        // Add focus
        if (state.items[index]) {
          state.items[index].classList.add('focused');
          state.items[index].scrollIntoView({ block: 'nearest' });
        }
      }

      function selectFocused() {
        if (state.items[state.focusedIndex]) {
          state.items[state.focusedIndex].click();
        }
      }

      document.addEventListener('keydown', function(e) {
        // Ignore if typing in input
        if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
          if (e.key === 'Escape') {
            e.target.blur();
          }
          return;
        }

        updateItems();

        switch (e.key) {
          case 'ArrowUp':
            e.preventDefault();
            focusItem(state.focusedIndex - 1);
            break;

          case 'ArrowDown':
            e.preventDefault();
            focusItem(state.focusedIndex + 1);
            break;

          case 'Enter':
            e.preventDefault();
            selectFocused();
            break;

          case '/':
          case 'k':
            if (e.metaKey || e.ctrlKey || e.key === '/') {
              e.preventDefault();
              const searchInput = document.querySelector('.stx-search-input');
              if (searchInput) searchInput.focus();
            }
            break;

          case 'Escape':
            state.focusedIndex = -1;
            state.items.forEach(item => item.classList.remove('focused'));
            break;
        }
      });

      // Hash change handling
      window.addEventListener('hashchange', function() {
        const hash = window.location.hash;
        if (window.__stxStory && window.__stxStory.navigateToHash) {
          window.__stxStory.navigateToHash(hash);
        }
      });

      // Initialize from hash
      if (window.location.hash) {
        setTimeout(function() {
          if (window.__stxStory && window.__stxStory.navigateToHash) {
            window.__stxStory.navigateToHash(window.location.hash);
          }
        }, 100);
      }
    })();
  `}function O(){return`
    .stx-tree-item.focused,
    .stx-search-result.focused {
      outline: 2px solid var(--primary);
      outline-offset: -2px;
    }

    .stx-recent-list {
      padding: 8px 0;
      border-bottom: 1px solid var(--border);
      margin-bottom: 8px;
    }

    .stx-recent-title {
      font-size: 11px;
      text-transform: uppercase;
      color: var(--text-secondary);
      padding: 4px 12px;
      font-weight: 500;
    }

    .stx-recent-item {
      padding: 6px 12px;
      cursor: pointer;
      font-size: 13px;
      display: flex;
      align-items: center;
      gap: 8px;
    }

    .stx-recent-item:hover {
      background: var(--bg-secondary);
    }

    .stx-recent-item .stx-recent-icon {
      color: var(--text-secondary);
    }
  `}function P(b,f){if(b.length===0)return"";return`
    <div class="stx-recent-list">
      <div class="stx-recent-title">Recent</div>
      ${b.filter((j)=>f.has(j)).map((j)=>`
      <div class="stx-recent-item" onclick="window.__stxStory.selectStory('${j}')">
        <span class="stx-recent-icon">\uD83D\uDD50</span>
        <span>${f.get(j)}</span>
      </div>
    `).join("")}
    </div>
  `}function Q(b){try{localStorage.setItem("stx-story-nav",JSON.stringify({selectedStoryId:b.selectedStoryId,selectedVariantId:b.selectedVariantId,recentStories:b.recentStories}))}catch{}}function R(){try{let b=localStorage.getItem("stx-story-nav");if(b)return JSON.parse(b)}catch{}return{}}function T(){return`
    (function() {
      // Load saved state
      try {
        const saved = localStorage.getItem('stx-story-nav');
        if (saved) {
          const state = JSON.parse(saved);
          window.__stxStoryState = state;

          // Restore selection
          if (state.selectedStoryId && window.__stxStory) {
            window.__stxStory.selectStory(state.selectedStoryId, state.selectedVariantId);
          }
        }
      }
catch (e) {}

      // Save state on changes
      window.__stxStory = window.__stxStory || {};
      const originalSelectStory = window.__stxStory.selectStory;

      window.__stxStory.selectStory = function(storyId, variantId) {
        if (originalSelectStory) {
          originalSelectStory(storyId, variantId);
        }

        try {
          const state = window.__stxStoryState || {};
          state.selectedStoryId = storyId;
          state.selectedVariantId = variantId;

          // Add to recent
          state.recentStories = state.recentStories || [];
          const idx = state.recentStories.indexOf(storyId);
          if (idx !== -1) state.recentStories.splice(idx, 1);
          state.recentStories.unshift(storyId);
          if (state.recentStories.length > 10) state.recentStories.pop();

          localStorage.setItem('stx-story-nav', JSON.stringify(state));
          window.__stxStoryState = state;
        }
catch (e) {}
      };
    })();
  `}export{Q as saveNavigationState,J as parseHash,R as loadNavigationState,T as getStatePersistenceScript,O as getNavigationStyles,N as getKeyboardNavigationScript,P as generateRecentList,K as generateHash,H as createNavigationState,L as addToRecent};
