#!/usr/bin/env node
import {e,a as a$1,o,b}from'./chunk-JIXHEBGK.js';import {a}from'./chunk-LKAT7IAK.js';import'./chunk-5TAVYPOV.js';import*as m from'fs';import*as p from'path';import c from'chalk';import j from'ora';function w(s){let e=s.message.toLowerCase();if(e.includes("expected object, received array")&&s.path?.includes("features"))return 'Convert to object format: features: { feature-name: { description: "..." } }';if(e.includes("expected object, received array")&&s.path?.includes("components"))return 'Convert to object format: components: { component-name: { description: "..." } }';if(e.includes("has no description"))return "Add a description field to help AI understand this item";if(e.includes("should use alphanumeric"))return "Use kebab-case: my-feature-name (lowercase, hyphens)";if(e.includes("references unknown"))return "Define the symbol in a .purpose file or remove the reference";if(e.includes("yaml syntax")||e.includes("bad indentation"))return "Check indentation (use 2 spaces) and YAML syntax"}function E(s){let e=s.trim().split(`
`),i=e.some(u=>/^#+\s/.test(u)),f=!s.includes(":")||e[0].startsWith("#")||e[0].startsWith("@");return i||f&&!s.trim().startsWith("---")}function D(s,e){let i=s.trim().split(`
`),f=p.basename(p.dirname(e)),u="";for(let r of i){if(!r.startsWith("#")&&!r.startsWith("@")&&r.trim()){u=r.trim();break}r.startsWith("#")&&(u=r.replace(/^#+\s*/,"").trim());}return `# Auto-converted from markdown format
description: "${u||`Purpose file for ${f}`}"

# TODO: Add features and components
# features:
#   feature-name:
#     description: "What this feature does"

# components:
#   component-name:
#     description: "What this component does"
`}function I(s){return s.replace(/\[\s*([^\]]+)\s*\]/g,(e,i)=>`[${i.split(",").map(u=>{let r=u.trim();return /^[#@$^!%]/.test(r)&&!r.startsWith('"')&&!r.startsWith("'")?`"${r}"`:r}).join(", ")}]`)}function L(s,e){try{let i=m.readFileSync(s,"utf8"),f=!1;if(E(i)&&(i=D(i,s),f=!0),e.some(a=>a.message.includes("tag suffix")||a.message.includes("flow indicator")||a.message.includes("missed comma"))||i.match(/\[[^\]]*[#@$^!%][^\]"']*\]/)){let a=I(i);a!==i&&(i=a,f=!0);}if(f){let a=s+".tmp";m.writeFileSync(a,i);let n=a$1(a);return m.unlinkSync(a),n.isYamlValid&&n.data?b(n.data):i}let r=a$1(s);if(r.isYamlValid&&r.data){let a=b(r.data);if(a.trim()!==i.trim())return a}return null}catch{return null}}function k(s,e,i){let f=p.relative(e,s),u=[],r=[],a=false,n=a$1(s);for(let t of n.detailedErrors||[]){let o={message:t.message,line:t.line,path:t.path,type:(t.type==="yaml"||t.type==="file","error"),suggestion:w(t),fixable:t.type==="schema"};u.push(o);}if(n.data){let t=o(n.data,f);for(let o of t.issues){let d={message:o.message,path:o.path,type:o.type,suggestion:w(o),fixable:false};o.type==="error"?u.push(d):r.push(d);}}if(i.fix&&u.length>0){let t=L(s,u);if(t){m.writeFileSync(s,t),a=true;let o=a$1(s);if(o.detailedErrors&&o.detailedErrors.length<u.length){u.length=0,r.length=0;for(let d of o.detailedErrors||[])u.push({message:d.message,line:d.line,path:d.path,type:"error",suggestion:w(d)});}}}let l=u.length===0&&(!i.strict||r.length===0);return {path:s,relativePath:f,valid:l,errors:u,warnings:r,fixed:a}}function W(s,e="  "){let i=[],f=s.type==="error"?c.red("\u2717"):c.yellow("\u26A0"),u=s.line?c.gray(` (line ${s.line})`):"",r=s.path?c.gray(` at ${s.path}`):"";return i.push(`${e}${f} ${s.message}${u}${r}`),s.suggestion&&i.push(`${e}  ${c.cyan("\u2192")} ${c.gray(s.suggestion)}`),i}var A=["src","lib","features","components","services","utils","routes","api","commands","core","middleware","models","handlers","hooks","stores","config","plugins"],S=new Set(["node_modules","dist","build",".git",".paradigm","coverage","__pycache__",".next",".nuxt","vendor","target"]);function C(s,e){let i=new Set(e.map(r=>p.dirname(r))),f=[];function u(r,a){if(a>4||i.has(r))return;let n;try{n=m.readdirSync(r,{withFileTypes:!0});}catch{return}let l=p.basename(r);if(S.has(l))return;if((A.includes(l)||n.some(o=>!o.isDirectory()&&/\.(ts|tsx|js|jsx|rs|py|go)$/.test(o.name)))&&!n.some(o=>o.name===".purpose")){let d=n.filter(g=>!g.isDirectory()&&/\.(ts|tsx|js|jsx|rs|py|go)$/.test(g.name)).filter(g=>!g.name.endsWith(".test.ts")&&!g.name.endsWith(".spec.ts")&&g.name!=="index.ts").map(g=>g.name.replace(/\.[^.]+$/,"")).slice(0,10).map(g=>g.replace(/([A-Z])/g,(h,x,P)=>(P>0?"-":"")+x.toLowerCase()));if(d.length>0){let g=p.join(r,".purpose"),h=p.relative(s,r),x=O(l,d);f.push({dir:r,relativeDir:h,purposePath:g,relativePurposePath:p.relative(s,g),components:d,content:x});}}for(let o of n)o.isDirectory()&&!S.has(o.name)&&u(p.join(r,o.name),a+1);}return u(s,0),f}function O(s,e){let i=[`description: "Components in ${s}"`,"","components:"];for(let f of e)i.push(`  ${f}:`),i.push(`    description: "TODO: describe #${f}"`);return i.push(""),i.join(`
`)}async function T(s,e$1){let i=s?p.resolve(s):process.cwd(),f=j();!e$1.quiet&&!e$1.json&&console.log(c.blue(`
\u{1F50D} Paradigm Lint
`));let u=a.command("lint").start("Linting purpose files",{fix:!!e$1.fix});f.start("Finding .purpose files...");let r=await e(i);if(f.stop(),a.operation("find-files").debug("Purpose files found",{count:r.length}),e$1.autoPopulate){f.start("Scanning for undocumented source directories...");let l=C(i,r);if(f.stop(),l.length===0){!e$1.quiet&&!e$1.json&&console.log(c.green(`All source directories have .purpose coverage.
`)),e$1.json&&console.log(JSON.stringify({suggestions:[],populated:0}));return}if(e$1.json){let t=e$1.fix?l.length:0;if(e$1.fix)for(let o of l)m.writeFileSync(o.purposePath,o.content,"utf8");console.log(JSON.stringify({suggestions:l.map(o=>({dir:o.relativeDir,purposePath:o.relativePurposePath,components:o.components})),populated:t}));return}console.log(c.yellow(`Found ${l.length} source director${l.length>1?"ies":"y"} without .purpose files:
`));for(let t of l){console.log(`  ${c.cyan(t.relativeDir)}/`);for(let o of t.components)console.log(c.gray(`    #${o}`));e$1.fix&&(m.writeFileSync(t.purposePath,t.content,"utf8"),console.log(c.green(`    \u2192 Created ${t.relativePurposePath}`))),console.log("");}e$1.fix?console.log(c.green(`Created ${l.length} .purpose file${l.length>1?"s":""}.
`)):console.log(c.gray(`Run ${c.cyan("paradigm lint --auto-populate --fix")} to create these .purpose files.
`));return}if(r.length===0){e$1.json?console.log(JSON.stringify({files:[],summary:{totalFiles:0}})):e$1.quiet||(console.log(c.yellow(`No .purpose files found.
`)),console.log(c.gray("Run `paradigm init` to create your first .purpose file.\n")));return}!e$1.quiet&&!e$1.json&&console.log(c.gray(`Checking ${r.length} .purpose file${r.length>1?"s":""}...
`));let a$1=[];for(let l of r){let t=k(l,i,e$1);if(a$1.push(t),!e$1.quiet&&!e$1.json&&(!t.valid||t.errors.length>0||t.warnings.length>0)){let o=t.errors.length>0?c.red("\u2717"):t.warnings.length>0?c.yellow("\u26A0"):c.green("\u2713"),d=t.fixed?c.cyan(" [fixed]"):"";console.log(`${o} ${t.relativePath}${d}`);for(let g of t.errors)for(let h of W(g))console.log(h);for(let g of t.warnings)for(let h of W(g))console.log(h);console.log("");}}let n={totalFiles:a$1.length,validFiles:a$1.filter(l=>l.valid).length,filesWithErrors:a$1.filter(l=>l.errors.length>0).length,filesWithWarnings:a$1.filter(l=>l.warnings.length>0).length,totalErrors:a$1.reduce((l,t)=>l+t.errors.length,0),totalWarnings:a$1.reduce((l,t)=>l+t.warnings.length,0),fixedFiles:a$1.filter(l=>l.fixed).length};if(e$1.json){let l={files:a$1.map(t=>({path:t.relativePath,valid:t.valid,errors:t.errors,warnings:t.warnings,fixed:t.fixed})),summary:n};console.log(JSON.stringify(l,null,2)),process.exit(n.filesWithErrors>0?1:0);return}e$1.quiet||(console.log(c.gray("\u2500".repeat(40))),n.totalErrors===0&&n.totalWarnings===0?(console.log(c.green(`
\u2713 All ${n.totalFiles} file${n.totalFiles>1?"s":""} valid
`)),u.success("All files valid",{files:n.totalFiles})):(u.error("Files have issues",{errors:n.totalErrors,warnings:n.totalWarnings,fixed:n.fixedFiles}),console.log(""),n.validFiles>0&&console.log(c.green(`\u2713 ${n.validFiles} file${n.validFiles>1?"s":""} valid`)),n.filesWithErrors>0&&console.log(c.red(`\u2717 ${n.filesWithErrors} file${n.filesWithErrors>1?"s":""} with errors (${n.totalErrors} total)`)),n.filesWithWarnings>0&&console.log(c.yellow(`\u26A0 ${n.filesWithWarnings} file${n.filesWithWarnings>1?"s":""} with warnings (${n.totalWarnings} total)`)),n.fixedFiles>0&&console.log(c.cyan(`\u21BB ${n.fixedFiles} file${n.fixedFiles>1?"s":""} auto-fixed`)),console.log(""),n.totalErrors>0&&!e$1.fix&&console.log(c.gray("Run `paradigm lint --fix` to auto-fix where possible.\n")))),n.filesWithErrors>0&&process.exit(1),e$1.strict&&n.filesWithWarnings>0&&process.exit(1);}export{T as lintCommand};