"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listenPointerEvent = exports.makeSyntheticPointerEvent = exports.applyContextTransformToPointerEvent = exports.POINTER_EVENT_NAMES = void 0;
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 } = event;
    const tx = clientX - $x;
    const ty = clientY - $y;
    const stx = tx / $sx;
    const sty = ty / $sy;
    const t = { x: stx, y: sty };
    const fp = (0, geometry_1.rotateVector)(t, -$rz);
    const clientX_ = fp.x;
    const clientY_ = fp.y;
    return {
        clientX: clientX_,
        clientY: clientY_,
        offsetX: t.x,
        offsetY: t.y,
        screenX: clientX,
        screenY: clientY,
        // screenX,
        // screenY,
    };
};
exports.applyContextTransformToPointerEvent = applyContextTransformToPointerEvent;
function makeSyntheticPointerEvent(context, event) {
    const { pointerId, pointerType } = event;
    return Object.assign(Object.assign({}, (0, exports.applyContextTransformToPointerEvent)(context, event)), { pointerId,
        pointerType });
}
exports.makeSyntheticPointerEvent = makeSyntheticPointerEvent;
function listenPointerEvent(type, component, listener, _global = false) {
    const { $element } = component;
    const pointerEventListener = (_event) => {
        const { $context } = component;
        const event = makeSyntheticPointerEvent($context, _event);
        listener(event, _event);
    };
    const unlisten = (0, __1._addEventListener)(type, $element, pointerEventListener, _global);
    const { $listenCount } = component;
    $listenCount[type] = $listenCount[type] || 0;
    $listenCount[type]++;
    return () => {
        $listenCount[type]--;
        if ($listenCount[type] === 0) {
            delete $listenCount[type];
        }
        unlisten();
    };
}
exports.listenPointerEvent = listenPointerEvent;
//# sourceMappingURL=index.js.map