import { nowIso } from "../shared/index.js";
const READONLY_VERBS = new Set(["status"]);
const MUTATING_VERBS = new Set(["lab-meeting"]);
const DEFAULT_MODE_TTL_MS = 120_000;
export function recordSeedCommandSession(ctx, sessionID, input) {
    if (!sessionID)
        return;
    const parsed = parseSeedCommandArgs(input.args);
    const mode = seedCommandMode(parsed);
    const at = nowIso();
    ctx.commandStateBySession.set(sessionID, {
        mode,
        command: parsed.args,
        source: input.source,
        set_at: at,
        expires_at_ms: Date.now() + Math.max(1_000, input.ttlMs ?? DEFAULT_MODE_TTL_MS),
    });
}
export function recordSeedCommandText(ctx, sessionID, text, source) {
    const parsed = parseSeedCommandEnvelope(text);
    if (!parsed)
        return false;
    recordSeedCommandSession(ctx, sessionID, { args: parsed.args, source });
    return true;
}
export function clearSeedCommandSession(ctx, sessionID) {
    if (!sessionID)
        return;
    ctx.commandStateBySession.delete(sessionID);
}
export function getSeedCommandSession(ctx, sessionID) {
    if (!sessionID)
        return undefined;
    const state = ctx.commandStateBySession.get(sessionID);
    if (!state)
        return undefined;
    if (state.expires_at_ms <= Date.now()) {
        ctx.commandStateBySession.delete(sessionID);
        return undefined;
    }
    return state;
}
export function parseSeedCommandArgs(args) {
    const normalized = args.trim().replace(/\s+/g, " ");
    const [verb] = normalized.split(/\s+/);
    return {
        args: normalized,
        verb: verb || undefined,
    };
}
function seedCommandMode(parsed) {
    if (!parsed.verb)
        return "task_goal";
    if (READONLY_VERBS.has(parsed.verb))
        return "readonly_control";
    if (MUTATING_VERBS.has(parsed.verb))
        return "mutating_control";
    return "task_goal";
}
function parseSeedCommandEnvelope(text) {
    if (!text.includes("<seed-command>"))
        return undefined;
    const match = text.match(/Arguments:\s*([^\n\r]*)/);
    if (!match)
        return undefined;
    return parseSeedCommandArgs(match[1] ?? "");
}
//# sourceMappingURL=command-mode.js.map