"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationDaemon = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const DoctorEngine_1 = require("../doctor/DoctorEngine");
class ValidationDaemon {
    constructor(options = {}) {
        this.timer = null;
        this.running = false;
        this.lastResult = null;
        this.interval = options.interval || 5000;
        this.onResult = options.onResult;
    }
    async start(directory) {
        if (this.running) {
            console.log(chalk_1.default.yellow("  Validation Daemon already running"));
            return;
        }
        this.running = true;
        const target = directory || process.cwd();
        console.log(chalk_1.default.bold("\n🔍 Validation Daemon"));
        console.log(chalk_1.default.dim(`  Watching: ${path.resolve(target)}`));
        console.log(chalk_1.default.dim(`  Interval: ${this.interval}ms`));
        console.log(chalk_1.default.dim("  Press Ctrl+C to stop\n"));
        await this.runOnce(target);
        this.timer = setInterval(async () => {
            await this.runOnce(target);
        }, this.interval);
    }
    stop() {
        if (this.timer) {
            clearInterval(this.timer);
            this.timer = null;
        }
        this.running = false;
        console.log(chalk_1.default.dim("\n  Validation Daemon stopped\n"));
    }
    async runOnce(directory) {
        const ctxPath = path.join(directory, ".mycontext", "context.json");
        const ctxExists = await fs.pathExists(ctxPath);
        if (!ctxExists) {
            this.printStatus("no-context", "No .mycontext/context.json found");
            return;
        }
        try {
            const result = await (0, DoctorEngine_1.runDoctor)(directory, {
                verbose: false,
            });
            this.lastResult = result;
            if (this.onResult) {
                this.onResult(result);
            }
            this.printResult(result);
        }
        catch (error) {
            this.printStatus("error", `Validation error: ${error.message}`);
        }
    }
    printResult(result) {
        const timestamp = new Date().toLocaleTimeString();
        const errors = result.diagnostics.filter((d) => d.severity === "error").length;
        const warnings = result.diagnostics.filter((d) => d.severity === "warning").length;
        const statusColor = result.score >= 90
            ? chalk_1.default.green
            : result.score >= 70
                ? chalk_1.default.yellow
                : chalk_1.default.red;
        console.log(`  [${timestamp}] ${statusColor(`Score: ${result.score}/100 (${result.grade})`)} ` +
            chalk_1.default.dim(`· ${errors} ${errors === 1 ? "error" : "errors"}`) +
            chalk_1.default.dim(` · ${warnings} ${warnings === 1 ? "warning" : "warnings"}`));
    }
    printStatus(status, message) {
        const timestamp = new Date().toLocaleTimeString();
        const icon = status === "no-context"
            ? chalk_1.default.yellow("!")
            : status === "error"
                ? chalk_1.default.red("✗")
                : chalk_1.default.green("✓");
        console.log(`  [${timestamp}] ${icon} ${chalk_1.default.dim(message)}`);
    }
    getLastResult() {
        return this.lastResult;
    }
    isRunning() {
        return this.running;
    }
}
exports.ValidationDaemon = ValidationDaemon;
//# sourceMappingURL=ValidationDaemon.js.map