// @bun
var{defineProperty:x,getOwnPropertyNames:D,getOwnPropertyDescriptor:E}=Object,G=Object.prototype.hasOwnProperty;function H(g){return this[g]}var M=(g)=>{var j=(z??=new WeakMap).get(g),k;if(j)return j;if(j=x({},"__esModule",{value:!0}),g&&typeof g==="object"||typeof g==="function"){for(var v of D(g))if(!G.call(j,v))x(j,v,{get:H.bind(g,v),enumerable:!(k=E(g,v))||k.enumerable})}return z.set(g,j),j},z;var J=(g)=>g;function K(g,j){this[g]=J.bind(null,j)}var O=(g,j)=>{for(var k in j)x(g,k,{get:j[k],enumerable:!0,configurable:!0,set:K.bind(j,k)})};var Q=(g,j)=>()=>(g&&(j=g(g=0)),j);var S=import.meta.require;class A{metrics=new Map;maxHistorySize=100;enabled=!0;setEnabled(g){this.enabled=g}recordRender(g,j,k,v){if(!this.enabled)return;let q=this.metrics.get(g);if(!q)q={componentId:g,componentName:j,renderCount:0,totalRenderTime:0,avgRenderTime:0,minRenderTime:1/0,maxRenderTime:0,lastRenderTime:0,history:[]},this.metrics.set(g,q);q.renderCount++,q.totalRenderTime+=k,q.avgRenderTime=q.totalRenderTime/q.renderCount,q.minRenderTime=Math.min(q.minRenderTime,k),q.maxRenderTime=Math.max(q.maxRenderTime,k),q.lastRenderTime=k;let w=this.getMemoryUsage();if(w!==void 0)q.memoryUsage=w;if(q.history.push({timestamp:Date.now(),duration:k,props:v,memory:w}),q.history.length>this.maxHistorySize)q.history=q.history.slice(-this.maxHistorySize)}getMetrics(g){return this.metrics.get(g)}getAllMetrics(){return Array.from(this.metrics.values())}getSlowestComponents(g=10){return this.getAllMetrics().sort((j,k)=>k.avgRenderTime-j.avgRenderTime).slice(0,g)}getMostRenderedComponents(g=10){return this.getAllMetrics().sort((j,k)=>k.renderCount-j.renderCount).slice(0,g)}clear(){this.metrics.clear()}clearComponent(g){this.metrics.delete(g)}getMemoryUsage(){if(typeof performance<"u"&&"memory"in performance)return performance.memory?.usedJSHeapSize;return}exportMetrics(){return JSON.stringify({timestamp:Date.now(),metrics:this.getAllMetrics()},null,2)}createTimer(g,j){return new C(this,g,j)}}class C{profiler;componentId;componentName;startTime;props;constructor(g,j,k){this.profiler=g;this.componentId=j;this.componentName=k;this.startTime=performance.now()}setProps(g){return this.props=g,this}end(){let g=performance.now()-this.startTime;return this.profiler.recordRender(this.componentId,this.componentName,g,this.props),g}}var V=new A;function W(g){return`
    <div class="performance-panel">
      <div class="performance-header">
        <h3>Performance</h3>
        <button onclick="clearPerformanceMetrics()">Clear</button>
      </div>
      <table class="performance-table">
        <thead>
          <tr>
            <th>Component</th>
            <th>Renders</th>
            <th>Avg</th>
            <th>Min</th>
            <th>Max</th>
            <th>Memory</th>
          </tr>
        </thead>
        <tbody>
          ${g.map((k)=>`
    <tr>
      <td>${k.componentName}</td>
      <td>${k.renderCount}</td>
      <td>${k.avgRenderTime.toFixed(2)}ms</td>
      <td>${k.minRenderTime.toFixed(2)}ms</td>
      <td>${k.maxRenderTime.toFixed(2)}ms</td>
      <td>${k.memoryUsage?L(k.memoryUsage):"-"}</td>
    </tr>
  `).join("")||'<tr><td colspan="6" class="empty">No data</td></tr>'}
        </tbody>
      </table>
    </div>
  `}function X(){return`
    .performance-panel {
      padding: 12px;
      background: var(--bg-secondary);
      border-radius: 8px;
    }
    .performance-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 12px;
    }
    .performance-header h3 {
      margin: 0;
      font-size: 14px;
    }
    .performance-table {
      width: 100%;
      border-collapse: collapse;
      font-size: 12px;
    }
    .performance-table th,
    .performance-table td {
      padding: 6px 8px;
      text-align: left;
      border-bottom: 1px solid var(--border);
    }
    .performance-table th {
      font-weight: 600;
      color: var(--text-secondary);
    }
    .performance-table .empty {
      text-align: center;
      color: var(--text-secondary);
      padding: 20px;
    }
  `}function Y(){return`
<script>
window.__stxPerformance = {
  metrics: new Map(),

  recordRender(componentId, componentName, duration, props) {
    let m = this.metrics.get(componentId);
    if (!m) {
      m = {
        componentId,
        componentName,
        renderCount: 0,
        totalRenderTime: 0,
        avgRenderTime: 0,
        minRenderTime: Infinity,
        maxRenderTime: 0,
        lastRenderTime: 0,
        history: []
      };
      this.metrics.set(componentId, m);
    }

    m.renderCount++;
    m.totalRenderTime += duration;
    m.avgRenderTime = m.totalRenderTime / m.renderCount;
    m.minRenderTime = Math.min(m.minRenderTime, duration);
    m.maxRenderTime = Math.max(m.maxRenderTime, duration);
    m.lastRenderTime = duration;

    m.history.push({ timestamp: Date.now(), duration, props });
    if (m.history.length > 100) m.history.shift();

    this.updatePanel();
  },

  getMetrics() {
    return Array.from(this.metrics.values());
  },

  clear() {
    this.metrics.clear();
    this.updatePanel();
  },

  updatePanel() {
    const panel = document.getElementById('performance-panel');
    if (panel) {
      // Update panel content
      window.dispatchEvent(new CustomEvent('stx:performance-update', {
        detail: { metrics: this.getMetrics() }
      }));
    }
  },

  // Timer helper
  startTimer(componentId, componentName) {
    const start = performance.now();
    return {
      end: (props) => {
        const duration = performance.now() - start;
        this.recordRender(componentId, componentName, duration, props);
        return duration;
      }
    };
  }
};

window.clearPerformanceMetrics = () => window.__stxPerformance.clear();
</script>
`}function L(g){if(g<1024)return`${g}B`;if(g<1048576)return`${(g/1024).toFixed(1)}KB`;return`${(g/1048576).toFixed(1)}MB`}export{V as profiler,Y as getPerformanceScript,X as getPerformancePanelStyles,W as getPerformancePanelHtml,C as RenderTimer,A as PerformanceProfiler};
