import { parseShortcut } from "./parser.js";
import { formatShortcut } from "./shortcutFormat.js";
export function normalizeBindingBlocks(blocks) {
    return blocks.map((block) => ({
        context: block.context ?? "Global",
        bindings: normalizeBindings(block.bindings ?? {}),
    }));
}
export function normalizeBindings(bindings) {
    if (Array.isArray(bindings))
        return bindings;
    return Object.entries(bindings ?? {}).map(([shortcut, action]) => ({
        action,
        keys: parseShortcut(shortcut),
        source: shortcut,
    }));
}
export function getBindingDisplayText(action, context, blocks) {
    for (const block of normalizeBindingBlocks(blocks)) {
        if (block.context !== context && block.context !== "Global")
            continue;
        const found = block.bindings.find((binding) => binding.action === action);
        if (found?.source)
            return formatShortcut(found.source);
    }
    return undefined;
}
export function resolveAction(shortcut, context, blocks) {
    const normalizedShortcut = formatShortcut(shortcut).toLowerCase();
    for (const block of normalizeBindingBlocks(blocks)) {
        if (block.context !== context && block.context !== "Global")
            continue;
        const found = block.bindings.find((binding) => formatShortcut(binding.source ?? "").toLowerCase() === normalizedShortcut);
        if (found)
            return found.action;
    }
    return undefined;
}
