import { Fun } from '@ephox/katamari';
import { Compare, Focus } from '@ephox/sugar';
import { Highlighting } from '../behaviour/Highlighting';
import * as AlloyTriggers from '../events/AlloyTriggers';
import * as SystemEvents from '../events/SystemEvents';
const reportFocusShifting = (component, prevFocus, newFocus) => {
    const noChange = prevFocus.exists((p) => newFocus.exists((n) => Compare.eq(n, p)));
    if (!noChange) {
        AlloyTriggers.emitWith(component, SystemEvents.focusShifted(), {
            prevFocus,
            newFocus
        });
    }
};
const dom = () => {
    const get = (component) => Focus.search(component.element);
    const set = (component, focusee) => {
        const prevFocus = get(component);
        component.getSystem().triggerFocus(focusee, component.element);
        const newFocus = get(component);
        reportFocusShifting(component, prevFocus, newFocus);
    };
    return {
        get,
        set
    };
};
const highlights = () => {
    const get = (component) => Highlighting.getHighlighted(component).map((item) => item.element);
    const set = (component, element) => {
        const prevFocus = get(component);
        component.getSystem().getByDom(element).fold(Fun.noop, (item) => {
            Highlighting.highlight(component, item);
        });
        const newFocus = get(component);
        reportFocusShifting(component, prevFocus, newFocus);
    };
    return {
        get,
        set
    };
};
export { dom, highlights };
//# sourceMappingURL=FocusManagers.js.map