import { generateRoleNames } from "./name-generator.js";
/** Valid role name pattern: lowercase alphanumeric + hyphens */
const NAME_PATTERN = /^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]$/;
export class RoleManager {
    store;
    constructor(store) {
        this.store = store;
    }
    createRole(opts) {
        const { name, label, promptTemplate, toolPolicies, accessory = "none", model, thinkingLevel } = opts;
        if (!name || typeof name !== "string") {
            throw new Error("Missing role name");
        }
        if (!NAME_PATTERN.test(name)) {
            throw new Error("Role name must be lowercase alphanumeric + hyphens (e.g. 'my-role')");
        }
        if (this.store.getLocal(name)) {
            throw new Error(`Role \"${name}\" already exists`);
        }
        if (!label || typeof label !== "string") {
            throw new Error("Missing role label");
        }
        const now = Date.now();
        const role = {
            name,
            label,
            promptTemplate: promptTemplate || "",
            accessory,
            toolPolicies,
            model,
            thinkingLevel,
            createdAt: now,
            updatedAt: now,
        };
        this.store.put(role);
        // Fire-and-forget: generate role-themed names via LLM
        generateRoleNames(name, label).catch((err) => {
            console.error(`[role-manager] Failed to generate names for role "${name}":`, err);
        });
        return role;
    }
    getRole(name) {
        return this.store.get(name);
    }
    listRoles() {
        return this.store.getAll();
    }
    updateRole(name, updates) {
        return this.store.update(name, updates);
    }
    deleteRole(name) {
        const role = this.store.get(name);
        if (!role)
            return false;
        this.store.remove(name);
        return true;
    }
}
//# sourceMappingURL=role-manager.js.map