#!/usr/bin/env node
import*as i from'fs';import*as o from'path';import*as d from'js-yaml';var u=".paradigm/tasks",m="entries",P="index.yaml";function b(n,t){let s=o.join(n,u,m,t);if(!i.existsSync(s))return `T-${t}-001`;let e=i.readdirSync(s).filter(a=>a.startsWith("T-")&&a.endsWith(".yaml")).map(a=>{let l=a.match(/T-\d{4}-\d{2}-\d{2}-(\d+)\.yaml/);return l?parseInt(l[1],10):0}),r=e.length>0?Math.max(...e)+1:1;return `T-${t}-${String(r).padStart(3,"0")}`}var k={high:0,medium:1,low:2};function w(n,t){let s=n;return t.status&&t.status!=="all"&&(s=s.filter(e=>e.status===t.status)),t.priority&&(s=s.filter(e=>e.priority===t.priority)),t.tag&&(s=s.filter(e=>e.tags.includes(t.tag))),s.sort((e,r)=>{let a=(k[e.priority]??1)-(k[r.priority]??1);return a!==0?a:new Date(r.created).getTime()-new Date(e.created).getTime()}),t.limit&&(s=s.slice(0,t.limit)),s}async function _(n,t){let s=o.join(n,u,m);if(!i.existsSync(s))return [];let e={status:"open",limit:20,...t},r=[],a=i.readdirSync(s).filter(l=>/^\d{4}-\d{2}-\d{2}$/.test(l)).sort().reverse();for(let l of a){let f=o.join(s,l),p=i.readdirSync(f).filter(c=>c.endsWith(".yaml")).sort();for(let c of p)try{let g=i.readFileSync(o.join(f,c),"utf8"),y=d.load(g);r.push(y);}catch{}}return w(r,e)}async function v(n,t){let s=t.match(/^T-(\d{4}-\d{2}-\d{2})-/);if(s){let r=o.join(n,u,m,s[1],`${t}.yaml`);if(i.existsSync(r))try{return d.load(i.readFileSync(r,"utf8"))}catch{return null}}return (await _(n,{status:"all",limit:9999})).find(r=>r.id===t)||null}async function I(n,t){let s=new Date,e=s.toISOString().slice(0,10),r=o.join(n,u,m,e);i.mkdirSync(r,{recursive:true});let a=b(n,e),l={id:a,blurb:t.blurb,priority:t.priority||"medium",status:"open",tags:t.tags||[],created:s.toISOString(),session_link:t.session_link,related_lore:t.related_lore};return i.writeFileSync(o.join(r,`${a}.yaml`),d.dump(l,{lineWidth:-1,noRefs:true})),await x(n),a}async function S(n,t,s){let e=await v(n,t);if(!e)return  false;let r=e.created.slice(0,10),a=o.join(n,u,m,r,`${t}.yaml`);if(!i.existsSync(a))return  false;let{id:l,created:f,...p}=s,c={...e,...p};return i.writeFileSync(a,d.dump(c,{lineWidth:-1,noRefs:true})),await x(n),true}async function j(n,t){return S(n,t,{status:"done",completed:new Date().toISOString()})}async function F(n,t){return S(n,t,{status:"shelved",shelved:new Date().toISOString()})}async function x(n){let t=o.join(n,u,m),s=o.join(n,u),e=0,r=0,a=0,l=0;if(i.existsSync(t)){let p=i.readdirSync(t).filter(c=>/^\d{4}-\d{2}-\d{2}$/.test(c));for(let c of p){let g=o.join(t,c),y=i.readdirSync(g).filter(h=>h.endsWith(".yaml"));for(let h of y)try{let T=d.load(i.readFileSync(o.join(g,h),"utf8"));e++,T.status==="open"?r++:T.status==="done"?a++:T.status==="shelved"&&l++;}catch{}}}let f={version:"1.0",total:e,open:r,done:a,shelved:l,last_updated:new Date().toISOString()};return i.mkdirSync(s,{recursive:true}),i.writeFileSync(o.join(s,P),d.dump(f,{lineWidth:-1,noRefs:true})),f}export{_ as a,v as b,I as c,S as d,j as e,F as f,x as g};