"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.ProbeManager = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
const util_1 = require("util");
const logger_1 = require("../utils/logger");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
class ProbeManager {
    constructor(projectPath) {
        this.projectPath = projectPath;
        this.probesDir = path.join(projectPath, '.mycontext', 'probes');
        this.ensureProbesDir();
    }
    ensureProbesDir() {
        if (!fs.existsSync(this.probesDir)) {
            fs.mkdirpSync(this.probesDir);
        }
    }
    /**
     * Run a specific repository probe
     */
    async runProbe(probe) {
        const timestamp = new Date().toISOString();
        logger_1.logger.info(`Executing probe: ${probe.name}`);
        try {
            const { stdout, stderr } = await execAsync(probe.command, {
                cwd: this.projectPath,
                timeout: 30000, // 30s timeout
            });
            if (stderr && !stdout) {
                return {
                    success: false,
                    output: stdout,
                    error: stderr,
                    timestamp,
                    probeName: probe.name,
                };
            }
            return {
                success: true,
                output: stdout,
                timestamp,
                probeName: probe.name,
            };
        }
        catch (error) {
            logger_1.logger.error(`Probe failed: ${probe.name}`, error);
            return {
                success: false,
                output: '',
                error: error.message,
                timestamp,
                probeName: probe.name,
            };
        }
    }
    /**
     * Save a successful probe as a reusable script
     */
    async saveProbe(probe) {
        const probePath = path.join(this.probesDir, `${probe.name}.json`);
        await fs.writeJson(probePath, probe, { spaces: 2 });
        logger_1.logger.info(`Probe saved: ${probe.name}`);
    }
    /**
     * List all available probes
     */
    async listProbes() {
        const files = await fs.readdir(this.probesDir);
        const probes = [];
        for (const file of files) {
            if (file.endsWith('.json')) {
                const probe = await fs.readJson(path.join(this.probesDir, file));
                probes.push(probe);
            }
        }
        return probes;
    }
}
exports.ProbeManager = ProbeManager;
//# sourceMappingURL=ProbeManager.js.map