"use strict";
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.doTeach = doTeach;
exports.doProfile = doProfile;
const readline = __importStar(require("readline"));
const utils_1 = require("../utils");
const teach_sections_1 = require("../../../06-memory/teach/teach-sections");
const teach_store_1 = require("../../../06-memory/teach/teach-store");
const teach_parser_1 = require("../../../06-memory/teach/teach-parser");
function readMultilineInput() {
    return new Promise((resolve) => {
        const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
        const lines = [];
        let emptyCount = 0;
        rl.on('line', (line) => {
            if (line.trim() === '') {
                emptyCount++;
                if (emptyCount >= 1) {
                    rl.close();
                    resolve(lines);
                }
            }
            else {
                emptyCount = 0;
                lines.push(line);
            }
        });
        rl.on('close', () => resolve(lines));
    });
}
async function doTeach(args) {
    (0, utils_1.printHeader)('Cell Teach — Tell Me About Yourself');
    const allSections = teach_sections_1.TEACH_SECTIONS.map((s) => s.key);
    // Check if profile exists
    const exists = (0, teach_store_1.profileExists)();
    const completion = (0, teach_store_1.profileCompletionStatus)(allSections);
    if (exists && !args.includes('--reset')) {
        (0, utils_1.printDim)(`Profile already exists (${completion.done.length}/${completion.total} sections, ${completion.completionPercent}% complete).`);
        (0, utils_1.printDim)('Use --reset to start over, or run `cell profile` to view/edit.');
        console.log();
        return;
    }
    if (args.includes('--reset')) {
        (0, teach_store_1.deleteProfile)();
        (0, utils_1.printSuccess)('Profile cleared. Starting fresh.');
        console.log();
    }
    // Walk through each section
    for (const section of teach_sections_1.TEACH_SECTIONS) {
        console.log();
        console.log(utils_1.C.bold(`  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`));
        console.log(utils_1.C.bold(`  Section ${teach_sections_1.TEACH_SECTIONS.indexOf(section) + 1}/${teach_sections_1.TEACH_SECTIONS.length}: ${section.title}`));
        console.log(utils_1.C.dim(`  ${section.prompt}`));
        console.log(utils_1.C.dim(`  Example: ${section.example.split('\n')[0]}`));
        console.log(utils_1.C.dim('  (Press Enter twice to finish, or type "skip" to skip)'));
        console.log();
        const lines = await readMultilineInput();
        if (lines.length === 0 || lines[0] === 'skip') {
            (0, utils_1.printDim)('  Skipped.');
            continue;
        }
        const rawText = lines.join('\n');
        const structured = (0, teach_parser_1.parseTeachInput)(rawText, section.key);
        (0, teach_store_1.saveProfileSection)(section.key, rawText, structured, 85);
        (0, utils_1.printSuccess)('  ✓ Saved.');
    }
    console.log();
    (0, utils_1.printSuccess)('Profile complete! Run `cell profile` to view it.');
}
function doProfile(args) {
    const sub = args[1] || '';
    if (sub === 'edit')
        return doProfileEdit(args[2]);
    if (sub === 'export')
        return doProfileExport();
    if (sub === 'reset')
        return doProfileReset();
    // Default: show profile
    doProfileShow();
}
function doProfileShow() {
    (0, utils_1.printHeader)('Developer Profile');
    const profile = (0, teach_store_1.getFullProfile)();
    if (Object.keys(profile).length === 0) {
        (0, utils_1.printDim)('No profile yet. Run `cell teach` to create one.');
        return;
    }
    for (const [section, data] of Object.entries(profile)) {
        const sectionName = section.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase());
        console.log(utils_1.C.bold(`  ${sectionName}:`));
        const items = Object.values(data.structuredData);
        if (items.length > 0) {
            for (const item of items) {
                console.log(`    - ${item}`);
            }
        }
        else {
            (0, utils_1.printDim)('    (empty)');
        }
        console.log();
    }
}
function doProfileEdit(sectionKey) {
    if (!sectionKey) {
        (0, utils_1.printWarning)('Usage: cell profile edit <section>');
        (0, utils_1.printDim)('Sections: coding_style, ai_expectations, hates, project_rules, workflow, communication, problems, notes');
        return;
    }
    const section = teach_sections_1.TEACH_SECTIONS.find((s) => s.key === sectionKey);
    if (!section) {
        (0, utils_1.printWarning)(`Unknown section: ${sectionKey}`);
        (0, utils_1.printDim)('Available: coding_style, ai_expectations, hates, project_rules, workflow, communication, problems, notes');
        return;
    }
    const existing = (0, teach_store_1.getProfileSection)(sectionKey);
    if (existing) {
        console.log(utils_1.C.bold(`  Current ${section.title}:`));
        const items = Object.values(existing.structuredData);
        for (const item of items) {
            console.log(`    - ${item}`);
        }
        console.log();
    }
    console.log(utils_1.C.dim(`  Enter new text (or "keep" to keep current):`));
    console.log();
    const lines = [];
    const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
    rl.on('line', (line) => {
        if (line.trim() === 'keep') {
            rl.close();
            (0, utils_1.printDim)('  Kept current.');
            return;
        }
        if (line.trim() === '') {
            rl.close();
            if (lines.length > 0) {
                const rawText = lines.join('\n');
                const structured = (0, teach_parser_1.parseTeachInput)(rawText, sectionKey);
                (0, teach_store_1.saveProfileSection)(sectionKey, rawText, structured, 85);
                (0, utils_1.printSuccess)('  ✓ Updated.');
            }
            return;
        }
        lines.push(line);
    });
    rl.on('close', () => { });
}
function doProfileExport() {
    const profile = (0, teach_store_1.getFullProfile)();
    if (Object.keys(profile).length === 0) {
        (0, utils_1.printWarning)('No profile to export. Run `cell teach` first.');
        return;
    }
    const json = JSON.stringify(profile, null, 2);
    console.log(json);
}
function doProfileReset() {
    const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
    rl.question('Are you sure you want to reset your profile? (y/n) ', (answer) => {
        rl.close();
        if (answer.toLowerCase() === 'y') {
            (0, teach_store_1.deleteProfile)();
            (0, utils_1.printSuccess)('Profile reset.');
        }
        else {
            (0, utils_1.printDim)('Cancelled.');
        }
    });
}
//# sourceMappingURL=teach.js.map