export function resolveAction(state, keyEvent) {
    const target = state.bindings.resolve(keyEvent);
    if (target?.type !== "action") {
        return null;
    }
    const actionState = state.actionState.get(target.id);
    if (actionState?.available !== true ||
        actionState.running === true ||
        actionState.action === undefined) {
        return null;
    }
    return actionState.action;
}
export function buildActionContext(state, _action, source, runtimeHandles, rowsOverride) {
    const row = rowsOverride?.[0] ?? currentRow(state) ?? { id: "", title: "" };
    return {
        row,
        rows: rowsOverride ?? selectedRows(state, row),
        item: source === "detail" ? currentDetailItem(state) : undefined,
        filter: state.filter,
        refresh: runtimeHandles.refresh,
        suspendAnd: runtimeHandles.suspendAnd,
        toast: runtimeHandles.toast,
        confirm: runtimeHandles.confirm,
        exit: runtimeHandles.exit
    };
}
function currentRow(state) {
    return state.rows[state.filtered[state.cursor] ?? -1];
}
function currentDetailItem(state) {
    return state.detail.items?.[state.detail.cursor];
}
function selectedRows(state, fallback) {
    if (!state.multiSelect || state.selected.size === 0) {
        return fallback.id === "" ? [] : [fallback];
    }
    return state.rows.filter((row) => state.selected.has(row.id));
}
