// @bun
var{defineProperty:F,getOwnPropertyNames:O,getOwnPropertyDescriptor:X}=Object,P=Object.prototype.hasOwnProperty;function f(j){return this[j]}var y=(j)=>{var k=(W??=new WeakMap).get(j),x;if(k)return k;if(k=F({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var B of O(j))if(!P.call(k,B))F(k,B,{get:f.bind(j,B),enumerable:!(x=X(j,B))||x.enumerable})}return W.set(j,k),k},W;var _=(j)=>j;function A(j,k){this[j]=_.bind(null,k)}var V=(j,k)=>{for(var x in k)F(j,x,{get:k[x],enumerable:!0,configurable:!0,set:A.bind(k,x)})};var b=(j,k)=>()=>(j&&(k=j(j=0)),k);var N=import.meta.require;import Q from"fs";import Y from"path";var R=".stx/story/analytics.json",S=1,D=1e4;class q{storage;storagePath;currentSession=null;dirty=!1;viewStartTimes=new Map;constructor(j){this.storagePath=Y.join(j,R),this.storage=this.loadStorage(),this.startSession()}loadStorage(){try{let j=Q.readFileSync(this.storagePath,"utf-8");return JSON.parse(j)}catch{return{version:S,events:[],sessions:[]}}}async save(){if(!this.dirty)return;let j=Y.dirname(this.storagePath);if(await Q.promises.mkdir(j,{recursive:!0}),this.storage.events.length>D)this.storage.events=this.storage.events.slice(-D);await Q.promises.writeFile(this.storagePath,JSON.stringify(this.storage,null,2)),this.dirty=!1}startSession(){this.endSession(),this.currentSession={id:T(),startTime:Date.now(),componentViews:[]},this.storage.sessions.push(this.currentSession),this.dirty=!0}endSession(){if(this.currentSession)this.currentSession.endTime=Date.now(),this.currentSession=null}track(j){this.storage.events.push({...j,timestamp:Date.now()}),this.dirty=!0}trackView(j,k){if(this.endViewTiming(j),this.viewStartTimes.set(j,Date.now()),this.track({type:"view",componentId:j,variantId:k}),this.currentSession&&!this.currentSession.componentViews.includes(j))this.currentSession.componentViews.push(j)}endViewTiming(j){let k=this.viewStartTimes.get(j);if(k){let x=Date.now()-k;this.track({type:"view",componentId:j,metadata:{duration:x,endView:!0}}),this.viewStartTimes.delete(j)}}trackPropChange(j,k,x){this.track({type:"prop_change",componentId:j,propName:k,metadata:{value:x}})}trackVariantSwitch(j,k){this.track({type:"variant_switch",componentId:j,variantId:k})}trackSearch(j,k){this.track({type:"search",query:j,metadata:{resultCount:k}})}getComponentAnalytics(j,k){let x=this.storage.events.filter((z)=>z.componentId===j),B=x.filter((z)=>z.type==="view"&&!z.metadata?.endView),H=x.filter((z)=>z.type==="prop_change"),J=x.filter((z)=>z.type==="variant_switch"),K={};for(let z of H)if(z.propName)K[z.propName]=(K[z.propName]||0)+1;let U={};for(let z of J)if(z.variantId)U[z.variantId]=(U[z.variantId]||0)+1;let G=new Set(this.storage.sessions.filter((z)=>z.componentViews.includes(j)).map((z)=>z.id)),Z=x.filter((z)=>z.metadata?.duration).map((z)=>z.metadata.duration),L=Z.length>0?Z.reduce((z,$)=>z+$,0)/Z.length:0,M=Object.entries(K).sort((z,$)=>$[1]-z[1]).slice(0,5).map(([z])=>z);return{componentId:j,componentName:k,viewCount:B.length,uniqueViews:G.size,lastViewed:B[B.length-1]?.timestamp,propChanges:K,topChangedProps:M,variantViews:U,avgTimeSpent:L}}getMostViewedComponents(j=10){let k=new Map;for(let x of this.storage.events)if(x.type==="view"&&x.componentId&&!x.metadata?.endView)k.set(x.componentId,(k.get(x.componentId)||0)+1);return Array.from(k.entries()).map(([x,B])=>({componentId:x,viewCount:B})).sort((x,B)=>B.viewCount-x.viewCount).slice(0,j)}getMostChangedProps(j=10){let k=new Map;for(let x of this.storage.events)if(x.type==="prop_change"&&x.propName)k.set(x.propName,(k.get(x.propName)||0)+1);return Array.from(k.entries()).map(([x,B])=>({propName:x,changeCount:B})).sort((x,B)=>B.changeCount-x.changeCount).slice(0,j)}getSearchAnalytics(){let j=this.storage.events.filter((H)=>H.type==="search"),k=new Map,x=0;for(let H of j){if(H.query)k.set(H.query,(k.get(H.query)||0)+1);if(H.metadata?.resultCount!==void 0)x+=H.metadata.resultCount}let B=Array.from(k.entries()).map(([H,J])=>({query:H,count:J})).sort((H,J)=>J.count-H.count).slice(0,10);return{totalSearches:j.length,topQueries:B,avgResultCount:j.length>0?x/j.length:0}}clear(){this.storage.events=[],this.storage.sessions=[],this.dirty=!0,this.startSession()}exportAnalytics(){return JSON.stringify({exportedAt:Date.now(),...this.storage},null,2)}}function T(){return`session-${Date.now()}-${Math.random().toString(36).slice(2,8)}`}function w(j,k){let x=j.map((H)=>`
    <tr>
      <td>${H.componentId}</td>
      <td>${H.viewCount}</td>
    </tr>
  `).join(""),B=k.map((H)=>`
    <tr>
      <td>${H.propName}</td>
      <td>${H.changeCount}</td>
    </tr>
  `).join("");return`
    <div class="analytics-dashboard">
      <h3>\uD83D\uDCCA Usage Analytics</h3>

      <div class="analytics-section">
        <h4>Most Viewed Components</h4>
        <table>
          <thead><tr><th>Component</th><th>Views</th></tr></thead>
          <tbody>${x||'<tr><td colspan="2">No data</td></tr>'}</tbody>
        </table>
      </div>

      <div class="analytics-section">
        <h4>Most Changed Props</h4>
        <table>
          <thead><tr><th>Prop</th><th>Changes</th></tr></thead>
          <tbody>${B||'<tr><td colspan="2">No data</td></tr>'}</tbody>
        </table>
      </div>
    </div>
  `}function h(){return`
    .analytics-dashboard {
      padding: 16px;
      background: var(--bg-secondary);
      border-radius: 8px;
    }
    .analytics-dashboard h3 {
      margin: 0 0 16px;
      font-size: 14px;
    }
    .analytics-section {
      margin-bottom: 16px;
    }
    .analytics-section h4 {
      margin: 0 0 8px;
      font-size: 12px;
      color: var(--text-secondary);
    }
    .analytics-dashboard table {
      width: 100%;
      border-collapse: collapse;
      font-size: 12px;
    }
    .analytics-dashboard th,
    .analytics-dashboard td {
      padding: 6px 8px;
      text-align: left;
      border-bottom: 1px solid var(--border);
    }
  `}export{h as getAnalyticsStyles,w as getAnalyticsDashboardHtml,q as AnalyticsTracker};
