// @bun
var{defineProperty:U,getOwnPropertyNames:Y,getOwnPropertyDescriptor:Z}=Object,_=Object.prototype.hasOwnProperty;function $(b){return this[b]}var C=(b)=>{var j=(W??=new WeakMap).get(b),q;if(j)return j;if(j=U({},"__esModule",{value:!0}),b&&typeof b==="object"||typeof b==="function"){for(var z of Y(b))if(!_.call(j,z))U(j,z,{get:$.bind(b,z),enumerable:!(q=Z(b,z))||q.enumerable})}return W.set(b,j),j},W;var G=(b)=>b;function k(b,j){this[b]=G.bind(null,j)}var S=(b,j)=>{for(var q in j)U(b,q,{get:j[q],enumerable:!0,configurable:!0,set:k.bind(j,q)})};var F=(b,j)=>()=>(b&&(j=b(b=0)),j);var P=import.meta.require;import M from"fs";import N from"path";function Q(b){return N.join(b.root,".stx","story","snapshots")}function R(b,j){return N.join(b,`${j}.versions.json`)}async function T(b,j){let q=Q(b),z=R(q,j);try{let B=await M.promises.readFile(z,"utf-8");return JSON.parse(B)}catch{return null}}async function D(b,j,q){let z=Q(b),B=R(z,j);await M.promises.mkdir(z,{recursive:!0});let A=await T(b,j);if(!A)A={current:{version:0,timestamp:0},history:[],maxVersions:10};let E={version:A.current.version+1,timestamp:Date.now(),commit:await u(b.root),description:q};if(A.current.version>0){if(A.history.unshift(A.current),A.history.length>A.maxVersions)A.history=A.history.slice(0,A.maxVersions)}return A.current=E,await M.promises.writeFile(B,JSON.stringify(A,null,2)),E}async function u(b){try{let j=N.join(b,".git","HEAD"),q=await M.promises.readFile(j,"utf-8");if(q.startsWith("ref:")){let z=N.join(b,".git",q.slice(5).trim());return(await M.promises.readFile(z,"utf-8")).trim().slice(0,8)}return q.trim().slice(0,8)}catch{return}}async function g(b,j,q){let z=await T(b,j);if(!z)return!1;let B=z.history.find((K)=>K.version===q);if(!B)return!1;let A=Q(b),E=N.join(A,`${j}.v${q}.html`),J=N.join(A,`${j}.html`);try{await M.promises.copyFile(E,J),z.current=B;let K=R(A,j);return await M.promises.writeFile(K,JSON.stringify(z,null,2)),!0}catch{return!1}}function m(b,j){let q=b.split(`
`),z=j.split(`
`),B=0,A=Math.max(q.length,z.length);for(let J=0;J<A;J++)if(q[J]!==z[J])B++;let E=A>0?B/A*100:0;return{hasDiff:B>0,percentage:E,changedPixels:B,totalPixels:A}}function f(b,j){let q=b.split(`
`),z=j.split(`
`),B=[],A=Math.max(q.length,z.length);for(let E=0;E<A;E++){let J=q[E]||"",K=z[E]||"";if(J===K)B.push(`<div class="diff-line diff-same">${O(J)}</div>`);else if(!J)B.push(`<div class="diff-line diff-added">+ ${O(K)}</div>`);else if(!K)B.push(`<div class="diff-line diff-removed">- ${O(J)}</div>`);else B.push(`<div class="diff-line diff-removed">- ${O(J)}</div>`),B.push(`<div class="diff-line diff-added">+ ${O(K)}</div>`)}return`
    <div class="stx-diff-view">
      <div class="diff-content">
        ${B.join(`
`)}
      </div>
    </div>
  `}function y(){return`
    .stx-diff-view {
      font-family: var(--code-font-family);
      font-size: 12px;
      line-height: 1.5;
      overflow: auto;
    }
    .diff-content {
      padding: 12px;
    }
    .diff-line {
      padding: 2px 8px;
      white-space: pre;
    }
    .diff-same {
      color: var(--text);
    }
    .diff-added {
      background: rgba(34, 197, 94, 0.2);
      color: #22c55e;
    }
    .diff-removed {
      background: rgba(239, 68, 68, 0.2);
      color: #ef4444;
    }
  `}function O(b){return b.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}async function I(b,j){let q=await T(b,j);if(!q)return[];return[q.current,...q.history]}async function x(b,j,q=5){let z=await T(b,j);if(!z)return 0;let B=z.history.slice(q),A=Q(b),E=0;for(let K of B){let X=N.join(A,`${j}.v${K.version}.html`);try{await M.promises.unlink(X),E++}catch{}}z.history=z.history.slice(0,q);let J=R(A,j);return await M.promises.writeFile(J,JSON.stringify(z,null,2)),E}export{D as saveSnapshotVersion,g as restoreSnapshotVersion,x as pruneSnapshotVersions,T as loadSnapshotVersions,I as listSnapshotVersions,Q as getSnapshotDir,y as getDiffStyles,m as generateTextDiff,f as generateDiffHTML};
