#!/usr/bin/env node
import {a}from'./chunk-LKAT7IAK.js';import*as c from'fs/promises';import*as u from'path';import*as g from'crypto';import*as l from'js-yaml';import i from'chalk';var f=".paradigm/adoptions.yaml",y=".paradigm/roster.yaml";function m(s){let r=Object.keys(s).sort(),t={};for(let o of r)t[o]=s[o];let e=JSON.stringify(t);return g.createHash("sha256").update(e).digest("hex")}async function h(s){let r=u.join(s,f);try{let t=await c.readFile(r,"utf-8"),e=l.load(t);if(!e||typeof e!="object")return null;let o=$(e.agents),d=e["integrity-hash"]||e.integrityHash||void 0,n=!0;if(d){let a$1=m(o);a$1!==d&&(n=!1,a.component("adoption").warn("Adoption integrity hash mismatch \u2014 adoptions.yaml may have been tampered with",{expected:d.slice(0,12)+"...",computed:a$1.slice(0,12)+"..."}));}return {version:e.version||"1.0",adoptedAt:e["adopted-at"]||e.adoptedAt||"",projectType:e["project-type"]||e.projectType||"",agents:o,verified:n,integrityHash:d}}catch(t){return t.code==="ENOENT"||a.component("adoption").warn("Failed to parse adoptions.yaml",{error:String(t)}),null}}async function v(s,r){let t=u.join(s,f),e=u.dirname(t);await c.mkdir(e,{recursive:true});let o=m(r.agents),d={version:r.version,"adopted-at":r.adoptedAt,"project-type":r.projectType,"integrity-hash":o,agents:w(r.agents)},n=l.dump(d,{lineWidth:-1,noRefs:true,sortKeys:false,quotingType:'"'});await c.writeFile(t,n,"utf-8");}async function k(s,r,t){let e=await h(s);e||(e=A("unknown")),e.agents[r]=t,await v(s,e),a.component("adoption").debug("Added adoption record",{agentId:r,source:t.source});}async function j(s){let r=u.join(s,y),t=new Date().toISOString(),e={active:[]};try{let d=await c.readFile(r,"utf-8"),n=l.load(d);n&&typeof n=="object"&&(e={version:n.version,active:n.active||[],type:n.type});}catch{a.component("adoption").debug("No roster.yaml found for migration");}let o={version:"1.0",adoptedAt:t,projectType:e.type||"unknown",agents:{}};for(let d of e.active||[])o.agents[d]={adopted:t,source:"core",defaultsAccepted:true};return a.component("adoption").debug("Migrated roster to adoptions",{count:Object.keys(o.agents).length}),o}function A(s){return {version:"1.0",adoptedAt:new Date().toISOString(),projectType:s,agents:{}}}function F(s,r){let t=[],e=i.dim("\u2500".repeat(49)),o=s.filter(n=>n.source==="core"),d=s.filter(n=>n.source==="ecosystem");if(t.push(""),t.push(`  ${i.bold("Agent Adoption")}`),t.push(`  ${e}`),t.push(`  ${i.dim("Detected:")} ${r}`),o.length>0){t.push(""),t.push(`  ${i.bold(`Core team (${o.length}):`)}`);for(let n of o){let a=n.nickname?`${n.id} (${n.nickname})`:n.id;t.push(`    ${i.white(a.padEnd(22))}${i.gray(n.role)}`);}}if(d.length>0){t.push(""),t.push(`  ${i.bold(`Ecosystem (${d.length} detected):`)}`);for(let n of d){let a=n.nickname?`${n.id} (${n.nickname})`:n.id;t.push(`    ${i.white(a.padEnd(22))}${i.gray(n.role)}`);}}return t.push(""),t.push(`  ${i.dim("All using default scopes.")}`),t.push(`  ${i.dim("[Enter]")} accept all  ${i.dim("|")}  ${i.dim("[r]")} review individually  ${i.dim("|")}  ${i.dim("[c]")} customize`),t.push(""),t.join(`
`)}function $(s){if(!s||typeof s!="object")return {};let r={};for(let[t,e]of Object.entries(s)){if(!e||typeof e!="object")continue;let o=e;r[t]={adopted:o.adopted||"",source:o.source||"core",defaultsAccepted:o["defaults-accepted"]!=null?!!o["defaults-accepted"]:o.defaultsAccepted!=null?!!o.defaultsAccepted:true,...o.version!=null&&{version:o.version},...o.overrides!=null&&{overrides:o.overrides},...o["scopes-approved"]!=null&&{scopesApproved:o["scopes-approved"]},...o.scopesApproved!=null&&{scopesApproved:o.scopesApproved},...o["detected-from"]!=null&&{detectedFrom:o["detected-from"]},...o.detectedFrom!=null&&{detectedFrom:o.detectedFrom}};}return r}function w(s){let r={};for(let[t,e]of Object.entries(s)){let o={adopted:e.adopted,source:e.source,"defaults-accepted":e.defaultsAccepted};e.version!=null&&(o.version=e.version),e.overrides!=null&&(o.overrides=e.overrides),e.scopesApproved!=null&&(o["scopes-approved"]=e.scopesApproved),e.detectedFrom!=null&&(o["detected-from"]=e.detectedFrom),r[t]=o;}return r}export{h as a,v as b,k as c,j as d,A as e,F as f};