"use strict";
// vibe-cli.ts — CLI interface for Cell Vibe
// Commands: cell vibe, cell vibe show, cell vibe learn, cell vibe reset
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.doVibe = doVibe;
const path = __importStar(require("path"));
const vibe_engine_1 = require("./vibe-engine");
const C = {
    bold: (s) => `\x1b[1m${s}\x1b[0m`,
    dim: (s) => `\x1b[2m${s}\x1b[0m`,
    warn: (s) => `\x1b[33m${s}\x1b[0m`,
    success: (s) => `\x1b[32m${s}\x1b[0m`,
    primary: (s) => `\x1b[36m${s}\x1b[0m`,
};
function printDim(s) { console.log(C.dim(s)); }
function printSuccess(s) { console.log(C.success(s)); }
function printWarning(s) { console.log(C.warn(s)); }
function getProjectName() {
    try {
        const pkg = require('../../../../../package.json');
        return pkg.name || path.basename(process.cwd());
    }
    catch {
        return path.basename(process.cwd());
    }
}
function doVibe(args) {
    const sub = args[1] || '';
    const cwd = process.cwd();
    switch (sub) {
        case 'learn':
            doVibeLearn(cwd);
            break;
        case 'reset':
            doVibeReset(cwd);
            break;
        case 'show':
        case 'view':
            doVibeShow(cwd);
            break;
        case 'export':
            doVibeExport(cwd);
            break;
        default:
            doVibeSummary(cwd);
            break;
    }
}
function doVibeSummary(cwd) {
    console.log();
    console.log(C.bold('  Cell Vibe'));
    console.log(C.dim('  ─────────'));
    console.log();
    const summary = (0, vibe_engine_1.getVibeSummary)(cwd);
    if (!summary || summary.totalRules === 0) {
        printDim('No vibe data yet. Vibe learns automatically from your sessions.');
        console.log();
        console.log(C.dim('  Just keep working — Cell observes and learns your style.'));
        console.log(C.dim('  Or force-learn now:  cell vibe learn'));
        console.log();
        return;
    }
    console.log(C.bold(`  ${summary.summary}`));
    console.log();
    console.log(C.dim(`  Learned ${summary.totalRules} rules (avg vibe: ${summary.averageVibe}/100)`));
    console.log();
    if (Object.keys(summary.coding).length > 0) {
        const parts = [];
        if (summary.coding.changeSize)
            parts.push(`Changes: ${summary.coding.changeSize}`);
        if (summary.coding.abstraction)
            parts.push(`Abstraction: ${summary.coding.abstraction}`);
        if (summary.coding.testing)
            parts.push(`Testing: ${summary.coding.testing}`);
        console.log(`  Code:      ${parts.join(', ') || 'learning...'}`);
    }
    if (Object.keys(summary.communication).length > 0) {
        const parts = [];
        if (summary.communication.language)
            parts.push(summary.communication.language);
        if (summary.communication.structure)
            parts.push(summary.communication.structure);
        if (summary.communication.tone)
            parts.push(summary.communication.tone);
        console.log(`  Comm:      ${parts.join(', ') || 'learning...'}`);
    }
    console.log(`  Decisions: ${summary.decisionDriver}-driven`);
    console.log(`  Risk:      ${summary.riskProfile}`);
    console.log(`  Ship:      ${summary.shipStyle}`);
    console.log(`  AI Trust:  ${summary.aiTrust}`);
    console.log();
    if (summary.strengths.length > 0 && summary.strengths[0] !== 'Still discovering strengths') {
        console.log(C.success('  Strengths:'));
        for (const s of summary.strengths)
            console.log(C.dim(`    • ${s}`));
        console.log();
    }
    if (summary.risks.length > 0 && summary.risks[0] !== 'No risks detected yet') {
        console.log(C.warn('  Watch out:'));
        for (const r of summary.risks)
            console.log(C.dim(`    • ${r}`));
        console.log();
    }
    if (summary.avoid.length > 0 && summary.avoid[0] !== 'Nothing specific yet') {
        console.log(C.warn('  Avoid:'));
        for (const a of summary.avoid)
            console.log(C.dim(`    • ${a}`));
        console.log();
    }
    console.log(C.dim('  Commands: cell vibe show | cell vibe learn | cell vibe reset'));
    console.log();
}
function doVibeShow(cwd) {
    console.log();
    console.log(C.bold('  Cell Vibe — Full Profile'));
    console.log(C.dim('  ────────────────────────'));
    console.log();
    const md = (0, vibe_engine_1.getVibeMarkdown)(cwd);
    const lines = md.split('\n');
    for (const line of lines) {
        if (line.startsWith('# ')) {
            console.log(C.bold(`\n  ${line.replace('# ', '')}`));
        }
        else if (line.startsWith('## ')) {
            console.log(C.bold(`\n  ${line.replace('## ', '')}`));
        }
        else if (line.startsWith('- ')) {
            console.log(C.dim(`  ${line}`));
        }
        else if (line.trim()) {
            console.log(`  ${line}`);
        }
    }
    console.log();
}
function doVibeLearn(cwd) {
    console.log();
    console.log(C.bold('  Cell Vibe — Learning'));
    console.log(C.dim('  ────────────────────'));
    console.log();
    const project = getProjectName();
    printDim(`  Analyzing sessions for: ${project}...`);
    try {
        const { getDb } = require('../database/database');
        const db = getDb();
        const sessionCount = db.prepare('SELECT COUNT(*) as c FROM sessions WHERE project = ?').get(project);
        printDim(`  Found ${sessionCount.c} sessions.`);
        const data = (0, vibe_engine_1.analyzeSession)(project, cwd, sessionCount.c);
        printSuccess(`\n  Vibe learned from ${sessionCount.c} sessions!`);
        const cats = new Set(data.rules.map(r => r.category)).size;
        console.log(C.dim(`  ${data.rules.length} rules across ${cats} categories.`));
        console.log();
        console.log(C.dim('  View:  cell vibe'));
        console.log(C.dim('  Show:  cell vibe show'));
        console.log();
    }
    catch (e) {
        const msg = e instanceof Error ? e.message : String(e);
        printWarning(`  Learn failed: ${msg}`);
        console.log();
    }
}
function doVibeExport(cwd) {
    const summary = (0, vibe_engine_1.getVibeSummary)(cwd);
    if (!summary) {
        console.log(JSON.stringify({ error: 'No vibe data yet' }, null, 2));
        return;
    }
    console.log(JSON.stringify(summary, null, 2));
}
function doVibeReset(cwd) {
    console.log();
    console.log(C.warn('  Reset all vibe data?'));
    const confirmed = process.argv.includes('--force') || process.argv.includes('-f');
    if (!confirmed) {
        console.log(C.dim('  Run: cell vibe reset --force  to confirm'));
        console.log();
        return;
    }
    (0, vibe_engine_1.resetVibe)(cwd);
    printSuccess('  Vibe data cleared.');
    console.log();
}
//# sourceMappingURL=vibe-cli.js.map