#!/usr/bin/env node
import{t as e}from"./rolldown-runtime-yuFVEuWy.js";import{l as t,o as n,p as r,r as i}from"./supersession-DO_ZROFl.js";import{join as a}from"node:path";import{readFile as o,writeFile as s}from"node:fs/promises";var c=e({DEFAULT_GROUP_CONFIG:()=>u,DEFAULT_PRUNE_CONFIG:()=>l,groupLessons:()=>w,pruneLessons:()=>C});const l={maxConfidence:15,minIdleDays:30,safetyDays:7,maxPrune:20,dryRun:!0},u={similarityThreshold:.7,minGroupSize:2,maxGroups:10,dryRun:!0},d=new Set([`the`,`a`,`to`,`for`,`in`,`of`,`and`,`is`,`use`]);function f(e){return{maxConfidence:Math.min(100,Math.max(0,Math.trunc(e?.maxConfidence??l.maxConfidence))),minIdleDays:Math.max(1,Math.trunc(e?.minIdleDays??l.minIdleDays)),safetyDays:Math.max(0,Math.trunc(e?.safetyDays??l.safetyDays)),maxPrune:Math.max(1,Math.trunc(e?.maxPrune??l.maxPrune)),dryRun:e?.dryRun??l.dryRun}}function p(e){return{similarityThreshold:Math.min(1,Math.max(0,e?.similarityThreshold??u.similarityThreshold)),minGroupSize:Math.max(2,Math.trunc(e?.minGroupSize??u.minGroupSize)),maxGroups:Math.max(1,Math.trunc(e?.maxGroups??u.maxGroups)),dryRun:e?.dryRun??u.dryRun}}function m(e){if(!e)return 0;let t=Date.parse(e);return Number.isNaN(t)?0:t}function h(e,t){let n=Math.max(m(e),m(t));return n===0?1/0:Math.max(0,Date.now()-n)/864e5}function g(e){return e.toLowerCase().match(/[a-z0-9]{2,}/g)??[]}function _(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``).slice(0,60)}function v(e,t){return`${[`---`,`title: "${e.title.replace(/"/g,`\\"`)}"`,`category: ${e.category}`,`tags: [${e.tags.map(e=>`"${e}"`).join(`, `)}]`,`created: ${e.created}`,`updated: ${e.updated}`,`version: ${e.version}`,`origin: ${e.origin}`,`changelog:`,...e.changelog.map(e=>`  - version: ${e.version}\n    date: ${e.date}\n    reason: "${e.reason.replace(/"/g,`\\"`)}"`),`---`].join(`
`)}\n\n${t}\n`}function y(e,t){let n=e.map(()=>[]);for(let r=0;r<e.length;r+=1)for(let a=r+1;a<e.length;a+=1)i(e[r],e[a])>=t&&(n[r].push(a),n[a].push(r));return n}function b(e){let t=new Set,n=[];for(let r=0;r<e.length;r+=1){if(t.has(r))continue;let i=[r],a=[];for(t.add(r);i.length>0;){let n=i.pop();if(n!==void 0){a.push(n);for(let r of e[n])t.has(r)||(t.add(r),i.push(r))}}n.push(a)}return n}function x(e){let t=new Map,n=new Map,r=0;for(let i of e)for(let e of g(i))d.has(e)||(t.set(e,(t.get(e)??0)+1),n.has(e)||(n.set(e,r),r+=1));let i=[...t.entries()].sort((e,t)=>t[1]-e[1]||(n.get(e[0])??0)-(n.get(t[0])??0))[0];return i&&i[1]>1?_(i[0]):_(e.flatMap(e=>g(e)).find(e=>!d.has(e))??`lesson-group`)}async function S(e,t,n){let r=a(e.curatedDir,t),i=await o(r,`utf8`),{frontmatter:c,content:l}=e.parseFile(i),u=`group:${n}`;if(c.tags.includes(u))return;let d=new Date().toISOString(),f=(c.version??1)+1;await s(r,v({...c,tags:[...new Set([...c.tags,u])],updated:d,version:f,changelog:[...c.changelog??[],{version:f,date:d,reason:`Grouped lesson under ${n}`}]},l),`utf8`)}async function C(e,i,a,o){let s=f(a),c=r(o??n),l=(await e.list({category:`lessons`})).filter(e=>e.tags.includes(`lesson`)),u=l.map(e=>{let n=i.memoryMetaGet(e.path);if(!n||n.supersededBy!=null)return null;let r=h(n.lastAccessedAt,n.createdAt??e.created);if(r<s.safetyDays)return null;let a=t(n.confidence,n.lastAccessedAt,n.createdAt,c);return a>=s.maxConfidence||r<s.minIdleDays?null:{path:e.path,effectiveConfidence:a}}).filter(e=>e!==null).sort((e,t)=>e.effectiveConfidence-t.effectiveConfidence||e.path.localeCompare(t.path)).slice(0,s.maxPrune);if(!s.dryRun)for(let e of u)i.memoryMetaSetSuperseded(e.path,`lesson:auto-prune`);return{pruned:u.map(e=>e.path),skipped:l.length-u.length,dryRun:s.dryRun}}async function w(e,t,n){let r=p(n),i=(await e.list({category:`lessons`})).filter(e=>{if(!e.tags.includes(`lesson`)||e.tags.some(e=>e.startsWith(`group:`)))return!1;let n=t.memoryMetaGet(e.path);return n?.supersededBy==null||n===void 0});if(i.length<r.minGroupSize)return{groupsCreated:0,lessonsGrouped:0,groups:[],dryRun:r.dryRun};let a=b(y(i.map(e=>e.title),r.similarityThreshold)).filter(e=>e.length>=r.minGroupSize).map(e=>{let t=e.map(e=>i[e]).sort((e,t)=>e.path.localeCompare(t.path));return{label:x(t.map(e=>e.title)),members:t.map(e=>e.path)}}).sort((e,t)=>t.members.length-e.members.length||e.members[0].localeCompare(t.members[0])).slice(0,r.maxGroups);if(!r.dryRun&&a.length>0){for(let t of a)for(let n of t.members)await S(e,n,t.label);await e.reindexAll()}return{groupsCreated:a.length,lessonsGrouped:a.reduce((e,t)=>e+t.members.length,0),groups:a,dryRun:r.dryRun}}export{w as n,C as r,c as t};