import{s as e}from"./DrKWWDpL.js";import{t}from"./DYP3EJMY.js";import{t as n}from"./fl61BeEu2.js";var r=e(t(),1),i=class extends Error{constructor(e,t){super(`Blob size ${e} bytes exceeds maximum of ${t} bytes (${Math.round(t/1024/1024)} MB)`),this.name=`BlobTooLargeError`,Object.setPrototypeOf(this,new.target.prototype)}},a=class extends Error{constructor(e,t){super(`Blob name "${e}" is invalid: ${t}`),this.name=`BlobNameInvalidError`,Object.setPrototypeOf(this,new.target.prototype)}},o=class extends Error{constructor(e,t){super(`Blob hash mismatch for ${e} at ${t} — storage may be corrupt or tampered`),this.name=`BlobCorruptError`,Object.setPrototypeOf(this,new.target.prototype)}},s=e(n(),1);function c(e){return e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`)}function l(e){let t=s;if(typeof t.canonicalFrontmatter==`function`)return t.canonicalFrontmatter(JSON.stringify(e));let n=[...e.contributors].sort(),r=``;r+=`---
`,r+=`title: "${c(e.title)}"\n`,r+=`slug: "${c(e.slug)}"\n`,r+=`version: ${e.version}\n`,r+=`state: "${c(e.state)}"\n`,r+=`contributors:
`;for(let e of n)r+=`  - "${c(e)}"\n`;return r+=`content_hash: "${c(e.content_hash)}"\n`,r+=`exported_at: "${c(e.exported_at)}"\n`,r+=`---
`,r}function u(e){return e.replace(/\r\n/g,`
`).replace(/\r/g,`
`).trimEnd()+`
`}function d(e,t={}){let n=t.includeMetadata!==!1,r=u(e.content);return n?l({title:e.title,slug:e.slug,version:e.version,state:e.state,contributors:e.contributors,content_hash:e.contentHash,exported_at:e.exportedAt})+`
`+r:r}var f=[`schema`,`title`,`slug`,`version`,`state`,`contributors`,`content_hash`,`exported_at`,`content`,`labels`,`created_by`,`created_at`,`updated_at`,`version_count`];function p(e,t={}){let n=[...e.contributors].sort(),r={schema:`llmtxt-export/1`,title:e.title,slug:e.slug,version:e.version,state:e.state,contributors:n,content_hash:e.contentHash,exported_at:e.exportedAt,content:e.content,labels:e.labels??null,created_by:e.createdBy??null,created_at:e.createdAt??null,updated_at:e.updatedAt??null,version_count:e.versionCount??null},i=JSON.stringify(r,f,2);return i=i.replace(/\r\n/g,`
`).replace(/\r/g,`
`),i.trimEnd()+`
`}function m(e){return e.content.replace(/\r\n/g,`
`).replace(/\r/g,`
`).trimEnd()+`
`}function h(e){return e.replace(/\r\n/g,`
`).replace(/\r/g,`
`).trimEnd()+`
`}function g(e,t){if(!e.endsWith(`---
`))throw Error(`canonicalFrontmatter output does not end with "---\\n"`);return e.slice(0,-4)+(t===null?`chain_ref: null
`:`chain_ref: "${t}"\n`)+`format: "llmtxt/1"
---
`}function _(e,t={}){let n=t.includeMetadata!==!1,r=h(e.content);return n?g(l({title:e.title,slug:e.slug,version:e.version,state:e.state,contributors:e.contributors,content_hash:e.contentHash,exported_at:e.exportedAt}),e.chainRef??null)+`
`+r:r}var v=class extends Error{code;constructor(e,t){super(t),this.code=e,this.name=`ExportError`,Object.setPrototypeOf(this,new.target.prototype)}};function y(e){return(0,r.createHash)(`sha256`).update(Buffer.from(e,`utf8`)).digest(`hex`)}function b(e){let t=e.split(`
`);if(t[0]?.trimEnd()!==`---`)return null;let n={},r=-1,i=null,a=null;for(let e=1;e<t.length;e++){let o=t[e];if(o.trimEnd()===`---`){i!==null&&a!==null&&(n[i]=a,a=null,i=null),r=e;break}if(o.startsWith(`  - `)&&a!==null){a.push(o.slice(4).trim().replace(/^"|"$/g,``));continue}a!==null&&i!==null&&(n[i]=a,a=null,i=null);let s=o.indexOf(`:`);if(s===-1)continue;let c=o.slice(0,s).trim(),l=o.slice(s+1).trim();if(l===``){i=c,a=[];continue}let u=l.startsWith(`"`)&&l.endsWith(`"`)?l.slice(1,-1):l,d=Number.parseInt(u,10);!Number.isNaN(d)&&String(d)===u?n[c]=d:n[c]=u}if(r===-1)return null;let o=t.slice(r+1);return o.length>0&&o[0]?.trim()===``&&o.shift(),{fields:n,body:o.join(`
`)}}function x(e){return r.default.basename(e,r.default.extname(e))}function S(e,t){let n=b(t),r=x(e);if(n===null)return{slug:r,title:r,content:t.replace(/\n+$/,``)+`
`,expectedContentHash:null};let{fields:i,body:a}=n,o=typeof i.slug==`string`?i.slug:r,s=typeof i.title==`string`?i.title:r,c=typeof i.content_hash==`string`?i.content_hash:null;return{slug:o,title:s,content:a.replace(/\n+$/,``)+`
`,expectedContentHash:c}}function C(e,t){let n;try{n=JSON.parse(t)}catch(t){throw new v(`PARSE_FAILED`,`Failed to parse JSON from ${e}: ${t instanceof Error?t.message:String(t)}`)}let r=x(e),i=typeof n.content==`string`?n.content:``,a=typeof n.slug==`string`?n.slug:r,o=typeof n.title==`string`?n.title:r,s=typeof n.content_hash==`string`?n.content_hash:null;return{slug:a,title:o,content:i.replace(/\n+$/,``)+`
`,expectedContentHash:s}}function w(e,t){let n=x(e);return{slug:n,title:n,content:t.replace(/\n+$/,``)+`
`,expectedContentHash:null}}function T(e){let t;try{t=r.default.readFileSync(e,`utf8`)}catch(t){throw new v(`PARSE_FAILED`,`Cannot read import file ${e}: ${t instanceof Error?t.message:String(t)}`)}let n=r.default.extname(e).toLowerCase(),i;switch(n){case`.md`:case`.llmtxt`:i=S(e,t);break;case`.json`:i=C(e,t);break;case`.txt`:i=w(e,t);break;default:i=S(e,t);break}if(i.expectedContentHash!==null){let t=y(i.content);if(t!==i.expectedContentHash)throw new v(`HASH_MISMATCH`,`Content hash mismatch in ${e}: frontmatter says ${i.expectedContentHash} but body SHA-256 is ${t}`)}return i}export{p as a,a as c,m as i,i as l,v as n,d as o,_ as r,o as s,T as t};