"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyContextTransformToPointerEvent = exports.POINTER_EVENT_NAMES = void 0;
exports.makeSyntheticPointerEvent = makeSyntheticPointerEvent;
exports.listenPointerEvent = listenPointerEvent;
const __1 = require("..");
const geometry_1 = require("../../util/geometry");
exports.POINTER_EVENT_NAMES = [
    'pointerdown',
    'pointermove',
    'pointerup',
    'pointerenter',
    'pointerleave',
    'pointercancel',
    'pointerover',
    'pointerout',
];
const applyContextTransformToPointerEvent = (context, event) => {
    const { $x, $y, $sx, $sy, $rz } = context;
    const { clientX, clientY, offsetX, offsetY, pageX, pageY } = event;
    const ctx = clientX - $x;
    const cty = clientY - $y;
    const cstx = ctx / $sx;
    const csty = cty / $sy;
    const sox = offsetX;
    const soy = offsetY;
    const c = { x: cstx, y: csty };
    const o = { x: sox, y: soy };
    const rc = (0, geometry_1.rotateVector)(c, -$rz);
    const ro = (0, geometry_1.rotateVector)(o, -$rz);
    return {
        clientX: rc.x,
        clientY: rc.y,
        offsetX: ro.x,
        offsetY: ro.y,
        screenX: clientX,
        screenY: clientY,
        pageX,
        pageY,
    };
};
exports.applyContextTransformToPointerEvent = applyContextTransformToPointerEvent;
function makeSyntheticPointerEvent(context, event) {
    const { pointerId, pointerType } = event;
    return {
        ...(0, exports.applyContextTransformToPointerEvent)(context, event),
        pointerId,
        pointerType,
    };
}
function listenPointerEvent(type, component, listener, _global = false) {
    const { $element } = component;
    const pointerEventListener = (_event) => {
        const { $context } = component;
        if (!$context) {
            return;
        }
        const event = makeSyntheticPointerEvent($context, _event);
        listener(event, _event);
    };
    const unlisten = (0, __1._addEventListener)(type, $element, pointerEventListener, _global);
    return unlisten;
}
//# sourceMappingURL=index.js.map