import { Arr, Fun, Obj, Type } from '@ephox/katamari';
const defaultEventHandler = {
    can: Fun.always,
    abort: Fun.never,
    run: Fun.noop
};
const nu = (parts) => {
    if (!Obj.hasNonNullableKey(parts, 'can') && !Obj.hasNonNullableKey(parts, 'abort') && !Obj.hasNonNullableKey(parts, 'run')) {
        throw new Error('EventHandler defined by: ' + JSON.stringify(parts, null, 2) + ' does not have can, abort, or run!');
    }
    return {
        ...defaultEventHandler,
        ...parts
    };
};
const all = (handlers, f) => (...args) => Arr.foldl(handlers, (acc, handler) => acc && f(handler).apply(undefined, args), true);
const any = (handlers, f) => (...args) => Arr.foldl(handlers, (acc, handler) => acc || f(handler).apply(undefined, args), false);
const read = (handler) => Type.isFunction(handler) ? {
    can: Fun.always,
    abort: Fun.never,
    run: handler
} : handler;
const fuse = (handlers) => {
    const can = all(handlers, (handler) => handler.can);
    const abort = any(handlers, (handler) => handler.abort);
    const run = (...args) => {
        Arr.each(handlers, (handler) => {
            // ASSUMPTION: Return value is unimportant.
            handler.run.apply(undefined, args);
        });
    };
    return {
        can,
        abort,
        run
    };
};
export { read, fuse, nu };
//# sourceMappingURL=EventHandler.js.map