import { Arr, Obj, Optional } from '@ephox/katamari';
const transpose = (obj) => 
// Assumes no duplicate fields.
Obj.tupleMap(obj, (v, k) => ({ k: v, v: k }));
const trace = (items, byItem, byMenu, finish) => 
// Given a finishing submenu (which will be the value of expansions),
// find the triggering item, find its menu, and repeat the process. If there
// is no triggering item, we are done.
Obj.get(byMenu, finish).bind((triggerItem) => Obj.get(items, triggerItem).bind((triggerMenu) => {
    const rest = trace(items, byItem, byMenu, triggerMenu);
    return Optional.some([triggerMenu].concat(rest));
})).getOr([]);
const generate = (menus, expansions) => {
    const items = {};
    Obj.each(menus, (menuItems, menu) => {
        Arr.each(menuItems, (item) => {
            items[item] = menu;
        });
    });
    const byItem = expansions;
    const byMenu = transpose(expansions);
    // For each menu, calculate the backlog of submenus to get to it.
    const menuPaths = Obj.map(byMenu, (_triggerItem, submenu) => [submenu].concat(trace(items, byItem, byMenu, submenu)));
    return Obj.map(items, (menu) => Obj.get(menuPaths, menu).getOr([menu]));
};
export { generate };
//# sourceMappingURL=MenuPathing.js.map