"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.AgentManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class AgentManager {
    root;
    constructor(root = process.cwd()) {
        this.root = root;
    }
    filePath() {
        return path.join(this.root, '.openlife', 'agents.json');
    }
    list() {
        const p = this.filePath();
        if (!fs.existsSync(p))
            return [{ id: 'default', name: 'Lara', isDefault: true }];
        try {
            return JSON.parse(fs.readFileSync(p, 'utf-8'));
        }
        catch {
            return [{ id: 'default', name: 'Lara', isDefault: true }];
        }
    }
    save(list) {
        const p = this.filePath();
        fs.mkdirSync(path.dirname(p), { recursive: true });
        fs.writeFileSync(p, JSON.stringify(list, null, 2), 'utf-8');
    }
    ensureDefault(defaultName) {
        const list = this.list();
        const hasDefault = list.some(a => a.isDefault);
        if (!hasDefault)
            list.unshift({ id: 'default', name: defaultName || 'Lara', isDefault: true });
        if (defaultName) {
            const idx = list.findIndex(a => a.isDefault);
            if (idx >= 0)
                list[idx].name = defaultName;
        }
        // Dedup extras by normalized name and avoid collision with default name
        const defaultAgent = list.find(a => a.isDefault);
        const defaultNorm = (defaultAgent?.name || 'lara').trim().toLowerCase();
        const seen = new Set();
        const normalized = [];
        for (const a of list) {
            if (a.isDefault) {
                normalized.push(a);
                continue;
            }
            const key = a.name.trim().toLowerCase();
            if (key === defaultNorm)
                continue;
            if (seen.has(key))
                continue;
            seen.add(key);
            normalized.push(a);
        }
        this.save(normalized);
        return normalized;
    }
    createAdditional(name) {
        const list = this.ensureDefault();
        const normalized = name.trim().toLowerCase();
        const defaultNorm = (list.find(a => a.isDefault)?.name || 'lara').trim().toLowerCase();
        const exists = list.some(a => !a.isDefault && a.name.trim().toLowerCase() === normalized);
        if (exists || normalized === defaultNorm) {
            this.save(list);
            return list;
        }
        const id = `agent-${Date.now()}`;
        list.push({ id, name, isDefault: false });
        this.save(list);
        return list;
    }
}
exports.AgentManager = AgentManager;
