#!/usr/bin/env node
import*as i from'fs';import*as l from'path';import*as c from'js-yaml';var f=".paradigm/university",y="content";function U(s){let t=s.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);if(!t)return null;try{return {frontmatter:c.load(t[1]),body:t[2].trim()}}catch{return null}}function j(s,t){return `---
${c.dump(s,{lineWidth:-1,noRefs:true,sortKeys:false})}---

${t}
`}function b(s){let t=l.join(s,f,"index.yaml");if(!i.existsSync(t))return null;try{return c.load(i.readFileSync(t,"utf8"))}catch{return null}}function h(s,t,e){let n=l.join(s,f,y);for(let r of ["notes","policies","quizzes","paths"]){let a=l.join(n,r,`${t}${e}`);if(i.existsSync(a))return a}return null}function F(s,t){let e=h(s,t,".md");if(!e)return null;try{let n=U(i.readFileSync(e,"utf8"));if(!n)return null;let r=n.frontmatter;return {frontmatter:{...r,tags:r.tags||[],symbols:r.symbols||[],prerequisites:r.prerequisites||[]},body:n.body}}catch{return null}}function z(s,t){let e=h(s,t,".yaml");if(!e)return null;try{let n=c.load(i.readFileSync(e,"utf8"));return n?.id?{...n,tags:n.tags||[],symbols:n.symbols||[],questions:n.questions||[]}:null}catch{return null}}function D(s,t){let e=h(s,t,".yaml");if(!e)return null;try{let n=c.load(i.readFileSync(e,"utf8"));return n?.id?n:null}catch{return null}}function w(s,t){let e=l.join(s,f,"diplomas");if(!i.existsSync(e))return [];let n=[];try{for(let r of i.readdirSync(e).filter(a=>a.endsWith(".yaml")))try{let a=c.load(i.readFileSync(l.join(e,r),"utf8"));if(!a?.id||t?.student&&a.student!==t.student||t?.type&&a.type!==t.type)continue;n.push(a);}catch{}}catch{}return n.sort((r,a)=>a.earnedAt.localeCompare(r.earnedAt))}function I(s,t,e){let n=t.type==="policy"?"policies":"notes",r=l.join(s,f,y,n);i.mkdirSync(r,{recursive:true});let a=l.join(r,`${t.id}.md`);return i.writeFileSync(a,j(t,e),"utf8"),a}function $(s,t){let e=l.join(s,f,y,"quizzes");i.mkdirSync(e,{recursive:true});let n=l.join(e,`${t.id}.yaml`);return i.writeFileSync(n,c.dump(t,{lineWidth:-1,noRefs:true}),"utf8"),n}function k(s,t){let e=l.join(s,f,"diplomas");i.mkdirSync(e,{recursive:true});let n=l.join(e,`${t.id}.yaml`);return i.writeFileSync(n,c.dump(t,{lineWidth:-1,noRefs:true}),"utf8"),n}function q(s){let t=l.join(s,f),e=l.join(t,y),n=[];for(let u of ["notes","policies"]){let o=l.join(e,u);if(i.existsSync(o))try{for(let p of i.readdirSync(o).filter(m=>m.endsWith(".md")))try{let m=i.readFileSync(l.join(o,p),"utf8"),S=U(m);if(!S)continue;let d=S.frontmatter;n.push({id:d.id||p.replace(".md",""),title:d.title||p,type:d.type||(u==="policies"?"policy":"note"),author:d.author||"unknown",created:d.created||"",updated:d.updated||"",tags:Array.isArray(d.tags)?d.tags:[],symbols:Array.isArray(d.symbols)?d.symbols:[],difficulty:d.difficulty||"beginner",file:`${y}/${u}/${p}`});}catch{}}catch{}}let r=l.join(e,"quizzes");if(i.existsSync(r))try{for(let u of i.readdirSync(r).filter(o=>o.endsWith(".yaml")))try{let o=c.load(i.readFileSync(l.join(r,u),"utf8"));if(!o?.id)continue;n.push({id:o.id,title:o.title||u,type:"quiz",author:o.author||"unknown",created:o.created||"",updated:o.updated||"",tags:o.tags||[],symbols:o.symbols||[],difficulty:o.difficulty||"beginner",file:`${y}/quizzes/${u}`});}catch{}}catch{}let a=l.join(e,"paths");if(i.existsSync(a))try{for(let u of i.readdirSync(a).filter(o=>o.endsWith(".yaml")))try{let o=c.load(i.readFileSync(l.join(a,u),"utf8"));if(!o?.id)continue;n.push({id:o.id,title:o.title||u,type:"path",author:o.author||"unknown",created:o.created||"",updated:o.updated||"",tags:o.tags||[],symbols:[],file:`${y}/paths/${u}`});}catch{}}catch{}let g=0,v=l.join(t,"diplomas");if(i.existsSync(v))try{g=i.readdirSync(v).filter(u=>u.endsWith(".yaml")).length;}catch{}let x={version:"1.0",generatedAt:new Date().toISOString(),totalContent:n.length,entries:n,diplomaCount:g};return i.mkdirSync(t,{recursive:true}),i.writeFileSync(l.join(t,"index.yaml"),c.dump(x,{lineWidth:-1,noRefs:true}),"utf8"),x}function Q(s,t){let e=b(s);if(!e)return [];let n=[...e.entries];if(t.type&&(n=n.filter(r=>r.type===t.type)),t.tag&&(n=n.filter(r=>r.tags.some(a=>a.startsWith(t.tag)))),t.difficulty&&(n=n.filter(r=>r.difficulty===t.difficulty)),t.symbol&&(n=n.filter(r=>r.symbols.includes(t.symbol))),t.query){let r=t.query.toLowerCase();n=n.filter(a=>a.title.toLowerCase().includes(r)||a.id.toLowerCase().includes(r));}return n.slice(0,t.limit||20)}export{b as a,F as b,z as c,D as d,w as e,I as f,$ as g,k as h,q as i,Q as j};