import { IS_IOS, IS_MAC } from '@blockeditor/global/env';
export function isPinchEvent(e) {
    // two finger pinches on touch pad, ctrlKey is always true.
    // https://bugs.chromium.org/p/chromium/issues/detail?id=397027
    if (IS_IOS || IS_MAC) {
        return e.ctrlKey || e.metaKey;
    }
    return e.ctrlKey;
}
// See https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
export var MOUSE_BUTTONS;
(function (MOUSE_BUTTONS) {
    MOUSE_BUTTONS[MOUSE_BUTTONS["NO_BUTTON"] = 0] = "NO_BUTTON";
    MOUSE_BUTTONS[MOUSE_BUTTONS["PRIMARY"] = 1] = "PRIMARY";
    MOUSE_BUTTONS[MOUSE_BUTTONS["SECONDARY"] = 2] = "SECONDARY";
    MOUSE_BUTTONS[MOUSE_BUTTONS["AUXILIARY"] = 4] = "AUXILIARY";
    MOUSE_BUTTONS[MOUSE_BUTTONS["FORTH"] = 8] = "FORTH";
    MOUSE_BUTTONS[MOUSE_BUTTONS["FIFTH"] = 16] = "FIFTH";
})(MOUSE_BUTTONS || (MOUSE_BUTTONS = {}));
export function isMiddleButtonPressed(e) {
    return (MOUSE_BUTTONS.AUXILIARY & e.buttons) === MOUSE_BUTTONS.AUXILIARY;
}
export function stopPropagation(event) {
    event.stopPropagation();
}
export function isControlledKeyboardEvent(e) {
    return e.ctrlKey || e.metaKey || e.altKey;
}
export function on(element, event, handler, options) {
    const dispose = () => {
        element.removeEventListener(event, handler, options);
    };
    element.addEventListener(event, handler, options);
    return dispose;
}
export function once(element, event, handler, options) {
    const onceHandler = (e) => {
        dispose();
        handler(e);
    };
    const dispose = () => {
        element.removeEventListener(event, onceHandler, options);
    };
    element.addEventListener(event, onceHandler, options);
    return dispose;
}
export function delayCallback(callback, delay = 0) {
    const timeoutId = setTimeout(callback, delay);
    return () => clearTimeout(timeoutId);
}
/**
 * A wrapper around `requestAnimationFrame` that only calls the callback if the
 * element is still connected to the DOM.
 */
export function requestConnectedFrame(callback, element) {
    return requestAnimationFrame(() => {
        // If element is not provided, fallback to `requestAnimationFrame`
        if (element === undefined) {
            callback();
            return;
        }
        // Only calls callback if element is still connected to the DOM
        if (element.isConnected)
            callback();
    });
}
/**
 * A wrapper around `requestConnectedFrame` that only calls at most once in one frame
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function batchToAnimationFrame(func, element) {
    let raqId = undefined;
    return new Proxy(func, {
        apply(target, thisArg, args) {
            if (raqId === undefined) {
                raqId = requestConnectedFrame(() => {
                    target.apply(thisArg, args);
                    raqId = undefined;
                }, element);
            }
        },
    });
}
//# sourceMappingURL=event.js.map