"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.ReuseEngine = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class ReuseEngine {
    memoryFile;
    promotedDir;
    constructor() {
        this.memoryFile = path.join(process.cwd(), '.artifacts', 'operational-memory.json');
        this.promotedDir = path.join(process.cwd(), '.openlife', 'promoted-assets');
    }
    findHints(goal) {
        const normalized = goal.toLowerCase();
        const hints = [];
        if (fs.existsSync(this.memoryFile)) {
            try {
                const entries = JSON.parse(fs.readFileSync(this.memoryFile, 'utf-8'));
                for (const entry of entries) {
                    const blob = `${entry.goal || ''} ${entry.summary || ''} ${(entry.lessons || []).join(' ')}`.toLowerCase();
                    if (blob.includes(normalized.slice(0, 20)) || normalized.includes((entry.goal || '').toLowerCase().slice(0, 20))) {
                        hints.push({
                            source: 'operational-memory',
                            summary: entry.summary || entry.goal || 'memory hit',
                            filePath: this.memoryFile
                        });
                    }
                }
            }
            catch { }
        }
        if (fs.existsSync(this.promotedDir)) {
            const files = fs.readdirSync(this.promotedDir).map(f => path.join(this.promotedDir, f));
            for (const file of files) {
                try {
                    const content = fs.readFileSync(file, 'utf-8');
                    if (content.toLowerCase().includes(normalized.slice(0, 20))) {
                        hints.push({
                            source: 'promoted-asset',
                            summary: content.slice(0, 200),
                            filePath: file
                        });
                    }
                }
                catch { }
            }
        }
        return hints.slice(0, 5);
    }
    enrichAgents(agents, hints) {
        if (!hints.length)
            return agents;
        return agents.map(agent => ({ ...agent, priority: agent.priority + 1 }));
    }
}
exports.ReuseEngine = ReuseEngine;
