// Epic #518 / Issue #527 — Workspace UI interaction substrate contracts.
//
// These types are consumed by the workspace UI (`@oscharko-dev/keiko-ui`)
// and by the registration-time descriptor validator that #528 lands. They
// have no runtime cost; they are the typed contract that makes the
// workspace's command + undo + keyboard substrate reviewable.
//
// The Action union is the load-bearing guarantee for ADR-0028. It declares
// constructors only for UI-state mutations. There is no Action variant for
// evidence creation, applied patches, verification records, model calls,
// tool execution, memory writes, workspace FS writes, or durable config
// writes — so the undo stack cannot record or reverse those classes. The
// refusal is compile-time, not runtime.
//
// File name `workspace-ui.ts` avoids a collision with the existing
// `workspace.ts` that owns the `@oscharko-dev/keiko-workspace` package
// types.
export const WORKSPACE_RESERVED_CHORDS = [
    { key: "t", mod: ["cmd"] },
    { key: "t", mod: ["ctrl"] },
    { key: "r", mod: ["cmd"] },
    { key: "r", mod: ["ctrl"] },
    { key: "n", mod: ["cmd", "shift"] },
    { key: "n", mod: ["ctrl", "shift"] },
    { key: "w", mod: ["cmd"] },
    { key: "w", mod: ["ctrl"] },
];
// ─── Pure helpers ─────────────────────────────────────────────────────────
// Exhaustive discriminated-union dispatch; one branch per WorkspaceUiAction kind
// is the pattern the type system requires.
// eslint-disable-next-line complexity
export function workspaceActionLabel(action) {
    switch (action.kind) {
        case "ui.window.move":
            return "Move window";
        case "ui.window.resize":
            return "Resize window";
        case "ui.window.zorder":
            return "Reorder window";
        case "ui.window.close":
            return "Close window";
        case "ui.window.open":
            return "Open window";
        case "ui.workspace.pan":
            return "Pan workspace";
        case "ui.workspace.zoom":
            return "Zoom workspace";
        case "ui.workspace.fit":
            return "Fit workspace to view";
        case "ui.panel.toggle":
            return `Toggle ${action.panel} panel`;
        case "ui.selection.change":
            return "Change selection";
        case "ui.tab.switch":
            return "Switch tab";
    }
}
export function workspaceChordKey(chord) {
    const sorted = [...chord.mod].sort();
    return `${sorted.join("+")}|${chord.key.toLowerCase()}`;
}
export function workspaceChordsEqual(a, b) {
    return workspaceChordKey(a) === workspaceChordKey(b);
}
export function isWorkspaceReservedChord(chord) {
    for (const reserved of WORKSPACE_RESERVED_CHORDS) {
        if (workspaceChordsEqual(chord, reserved))
            return true;
    }
    return false;
}
// ─── Inverse-action helper for the undo stack ─────────────────────────────
//
// Pure function that computes the inverse of any ui.* action by swapping
// before/after. Lives next to the contracts so consumers and tests share
// the canonical inverse implementation.
// Exhaustive discriminated-union dispatch; one branch per WorkspaceUiAction kind
// is the pattern the type system requires.
// eslint-disable-next-line complexity
export function workspaceInverseAction(action) {
    switch (action.kind) {
        case "ui.window.move":
            return { ...action, before: action.after, after: action.before };
        case "ui.window.resize":
            return { ...action, before: action.after, after: action.before };
        case "ui.window.zorder":
            return { ...action, before: action.after, after: action.before };
        case "ui.window.close":
            return { ...action, kind: "ui.window.open" };
        case "ui.window.open":
            return { ...action, kind: "ui.window.close" };
        case "ui.workspace.pan":
            return { ...action, before: action.after, after: action.before };
        case "ui.workspace.zoom":
            return { ...action, before: action.after, after: action.before };
        case "ui.workspace.fit":
            return { ...action, before: action.after, after: action.before };
        case "ui.panel.toggle":
            return { ...action, before: action.after, after: action.before };
        case "ui.selection.change":
            return { ...action, before: action.after, after: action.before };
        case "ui.tab.switch":
            return { ...action, before: action.after, after: action.before };
    }
}
