"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.ExecutionHistory = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
class ExecutionHistory {
    constructor(baseDir) {
        const dir = baseDir || process.cwd();
        this.historyPath = path.join(dir, ".mycontext", "execution-history.json");
    }
    async record(entry) {
        const history = await this.load();
        const id = `hist-${Date.now()}-${Math.random().toString(36).substr(2, 6)}`;
        const full = {
            ...entry,
            id,
            timestamp: new Date().toISOString(),
        };
        history.push(full);
        await this.save(history);
        return id;
    }
    async list(filters) {
        const history = await this.load();
        let filtered = [...history];
        if (filters?.type) {
            filtered = filtered.filter(e => e.type === filters.type);
        }
        if (filters?.status) {
            filtered = filtered.filter(e => e.status === filters.status);
        }
        if (filters?.since) {
            const since = new Date(filters.since).getTime();
            filtered = filtered.filter(e => new Date(e.timestamp).getTime() >= since);
        }
        filtered.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());
        if (filters?.limit && filters.limit > 0) {
            filtered = filtered.slice(0, filters.limit);
        }
        return filtered;
    }
    async get(id) {
        const history = await this.load();
        return history.find(e => e.id === id);
    }
    async getTimeline() {
        return this.list();
    }
    async export(format) {
        const history = await this.load();
        if (format === "json") {
            return JSON.stringify(history, null, 2);
        }
        // Markdown format
        let md = "# Execution History\n\n";
        for (const entry of history) {
            const statusIcon = entry.status === "success"
                ? "✓"
                : entry.status === "failure"
                    ? "✗"
                    : "↩";
            md += `## ${statusIcon} ${entry.action}\n`;
            md += `- **ID:** ${entry.id}\n`;
            md += `- **Type:** ${entry.type}\n`;
            md += `- **Status:** ${entry.status}\n`;
            md += `- **Timestamp:** ${entry.timestamp}\n`;
            md += `- **Duration:** ${entry.duration}ms\n`;
            if (entry.trace) {
                md += `- **Stages:** ${entry.trace.length}\n`;
                for (const t of entry.trace) {
                    md += `  - ${t.stage}: ${t.status}\n`;
                }
            }
            md += "\n";
        }
        return md;
    }
    async load() {
        try {
            if (await fs.pathExists(this.historyPath)) {
                return await fs.readJson(this.historyPath);
            }
        }
        catch { }
        return [];
    }
    async save(entries) {
        await fs.ensureDir(path.dirname(this.historyPath));
        await fs.writeJson(this.historyPath, entries, { spaces: 2 });
    }
}
exports.ExecutionHistory = ExecutionHistory;
//# sourceMappingURL=ExecutionHistory.js.map