import { loadSeedConfig } from "../config/index.js";
import { createRunStore, getActiveRun, getRun } from "../features/run-state/index.js";
import { formatLabMeetingNotice, triggerLabMeetingNow } from "../features/lab-meeting/index.js";
import { buildSeedStatusBoard } from "../features/status-board/index.js";
import { formatLabMeetingBackgroundCompletionFollowup, requestBackgroundCompletionFromActiveBackgrounds, requestLabRepresentativeMeetingReport, } from "../plugin/lab-meeting-control.js";
const tui = async (api, options) => {
    api.keymap.registerLayer({
        commands: [
            {
                namespace: "palette",
                name: "seed.status",
                title: "Show SEED status",
                desc: "Read-only SEED runtime status board",
                category: "SEED",
                slashName: "seed-status",
                run: async () => {
                    await showSeedStatus(api, options);
                },
            },
            {
                namespace: "palette",
                name: "seed.labMeeting",
                title: "Start SEED Lab Meeting",
                desc: "Pause new work, wait for active backgrounds, and request a Lab-Representative report",
                category: "SEED",
                slashName: "seed-lab-meeting",
                run: async () => {
                    await openLabMeeting(api, options);
                },
            },
        ],
    });
};
async function showSeedStatus(api, options) {
    try {
        const { store } = await loadTuiStore(api, options);
        const text = await buildSeedStatusBoard(store);
        showTextDialog(api, "SEED status", text);
    }
    catch (error) {
        showTextDialog(api, "SEED status failed", errorText(error));
    }
}
async function openLabMeeting(api, options) {
    try {
        const { store, directory, worktree } = await loadTuiStore(api, options);
        const active = await getActiveRun(store);
        if (!active) {
            showTextDialog(api, "SEED Lab Meeting", "No active SEED run found.");
            return;
        }
        const run = await getRun(store, active.run_id);
        const result = await triggerLabMeetingNow(store, active.run_id, "manual");
        const followup = result.state?.status === "wrap_up"
            ? await requestBackgroundCompletionFromActiveBackgrounds({ directory, client: api.client, worktree }, store, active.run_id, {
                sessionId: currentSessionId(api) ?? run?.root_session_id,
                meetingId: result.state.current_meeting_id,
            })
            : undefined;
        const report = result.state?.status === "awaiting_feedback"
            ? await requestLabRepresentativeMeetingReport({ directory, client: api.client, worktree }, store, active.run_id, {
                sessionId: run?.root_session_id,
                source: "seed-lab-meeting-tui",
            })
            : undefined;
        showTextDialog(api, "SEED Lab Meeting", [
            formatLabMeetingNotice(result),
            followup ? formatLabMeetingBackgroundCompletionFollowup(followup) : "",
            report ? `Lab-Representative report request: ${report.status}${report.reason ? ` (${report.reason})` : ""}` : "",
        ].filter(Boolean).join("\n\n"));
    }
    catch (error) {
        showTextDialog(api, "SEED Lab Meeting failed", errorText(error));
    }
}
async function loadTuiStore(api, options) {
    const directory = api.state.path.directory;
    const worktree = api.state.path.worktree || directory;
    const loaded = await loadSeedConfig(directory, normalizeOptions(options));
    return {
        directory,
        worktree,
        config: loaded.config,
        store: createRunStore(directory, loaded.config),
    };
}
function normalizeOptions(options) {
    if (!options || typeof options !== "object")
        return undefined;
    return options;
}
function currentSessionId(api) {
    const current = api.route.current;
    const params = "params" in current ? current.params : undefined;
    const sessionId = params && typeof params.sessionID === "string" ? params.sessionID : undefined;
    return sessionId;
}
function showTextDialog(api, title, message) {
    api.ui.dialog.setSize("xlarge");
    api.ui.dialog.replace(() => api.ui.DialogAlert({
        title,
        message,
        onConfirm: () => api.ui.dialog.clear(),
    }));
}
function errorText(error) {
    return error instanceof Error ? error.message : String(error);
}
const pluginModule = {
    id: "seed-agent",
    tui,
};
export default pluginModule;
//# sourceMappingURL=index.js.map