"use strict";var e=require("node:crypto"),t=require("node:fs"),r=require("node:path"),a=require("fast-glob");function i(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var a=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,a.get?a:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var o=i(e),s=i(t),n=i(r);const h=e=>"string"==typeof e?e:"number"==typeof e||"boolean"==typeof e?e.toString():e instanceof Date?e.toISOString():Buffer.isBuffer(e)?e.toString("hex"):Array.isArray(e)?e.map(h).join(","):"object"==typeof e&&null!==e?Object.entries(e).sort((([e],[t])=>e.localeCompare(t))).map((([e,t])=>`${e}:${h(t)}`)).join(","):"",c=({data:e,key:t,algorithm:r="sha256",metadata:a={}})=>{const i=t?o.createHmac(r,t):o.createHash(r);return i.update(h({data:e,metadata:a})),i.digest("hex")},l=({filePath:e,algorithm:t,key:r,metadata:a})=>new Promise(((i,o)=>{s.existsSync(e)||o(new Error(`File not found: ${e}`));const n=[],h=s.createReadStream(e);h.on("data",(e=>n.push(e))),h.on("end",(()=>{const e=Buffer.concat(n);i(c({data:e,algorithm:t,key:r,metadata:a}))})),h.on("error",(e=>o(e)))})),f=async({directoryPath:e,algorithm:t,key:r,metadata:i,include:o=["**/*"],exclude:h=[],includeStructure:f=!1})=>{if(!s.existsSync(e))throw new Error(`Directory not found: '${e}'`);const d=[...o,...h.map((e=>`!${e}`))],u=a.globSync(d,{cwd:e,dot:!0,onlyFiles:!0});u.sort();const y=await Promise.all(u.map((async a=>{const o=n.join(e,a),s=f?{...i,filePath:o}:i;return await l({filePath:o,algorithm:t,key:r,metadata:s})})));return c({data:y.join(""),algorithm:t,key:r,metadata:i})},d=(e,t)=>{try{const r=c({data:e,algorithm:t});return Buffer.from(r,"hex")}catch(e){throw new Error(`Failed to generate hashed buffer: ${e.message}`)}};class u{leaves;layers;algorithm;constructor(e,t="sha256"){this.algorithm=t,this.leaves=e,this.layers=[this.leaves],this.buildTree()}buildTree(){let e=this.leaves;for(;e.length>1;)e=this.createNextLayer(e),this.layers.push(e)}createNextLayer(e){const t=[];for(let r=0;re.toString("hex"))),t=this.layers.map((e=>e.map((e=>e.toString("hex")))));return JSON.stringify({leaves:e,layers:t,algorithm:this.algorithm})}static deserialize(e){const{leaves:t,layers:r,algorithm:a}=JSON.parse(e),i=t.map((e=>Buffer.from(e,"hex"))),o=r.map((e=>e.map((e=>Buffer.from(e,"hex"))))),s=Object.create(u.prototype);return s.leaves=i,s.layers=o,s.algorithm=a,s}}class y extends u{constructor(e,t="sha256"){super(e.map((e=>d(e,t))),t)}getRootHash(){return this.getRoot().toString("hex")}addItem(e){const t=d(e,this.algorithm);return this.addLeaf(t),this.getRootHash()}verifyItem(e,t){const r=d(e,this.algorithm),a=this.getLeaves().findIndex((e=>e.equals(r)));if(-1===a)return!1;const i=this.generateProof(a);return this.verifyProof(i,r,Buffer.from(t,"hex"))}toJSON(){return this.serialize()}static fromJSON(e){try{const t=u.deserialize(e),r=Object.create(y.prototype);return Object.assign(r,t),r}catch(e){throw new Error(`Failed to deserialize VerifiableHashList: ${e.message}`)}}}exports.VerifiableHashList=y,exports.canonicalize=h,exports.generateDirectoryHash=f,exports.generateFileHash=l,exports.generateHash=c,exports.generateHashedBuffer=d,exports.monitorIntegrity=async({type:e,expectedHash:t,onIntegrityCheckFailed:r,onError:a,...i})=>{try{const o=async()=>{if("file"===e){const{filePath:e,...t}=i;if(!e)throw new Error("File path is required for file integrity monitoring");return l({filePath:e,...t})}if("directory"===e){const{directoryPath:e,...t}=i;if(!e)throw new Error("Directory path is required for directory integrity monitoring");return f({directoryPath:e,...t})}throw new Error(`Expected type to be "file" or "directory" but got ${e}`)};t||(t=await o());const n=async()=>{try{if(await o()!==t&&r){const t=new Error(`Integrity check failed for ${e}: ${"file"===e?i.filePath:i.directoryPath}`);if(!r)throw t;r(t)}}catch(e){if(!a)throw e;a(e)}};s.watch("file"===e?i.filePath:i.directoryPath,{recursive:"directory"===e},(async t=>{("change"===t||"rename"===t&&"directory"===e&&i.includeStructure)&&await n()})),await n()}catch(e){a&&a(e)}},exports.validateIntegrity=async({type:e,expectedHash:t,...r})=>{if("data"===e){const{data:e,...a}=r;if(!e)throw new Error("Data is required for data integrity validation");return c({data:e,...a})===t}if("file"===e){const{filePath:e,...a}=r;if(!e)throw new Error("File path is required for file integrity validation");return await l({filePath:e,...a})===t}if("directory"===e){const{directoryPath:e,...a}=r;if(!e)throw new Error("Directory path is required for directory integrity validation");return await f({directoryPath:e,...a})===t}throw new Error("Invalid type or missing fixtures/filePath")};