import { vglobal } from "./../../vrender";

import { handleWhell, isHorizontalExistScrollBar, isHorizontalScrollable, isVerticalExistScrollBar, isVerticalScrollable } from "../scroll";

import { IconFuncTypeEnum } from "../../ts-types";

export function bindTouchListener(eventManager) {
    const table = eventManager.table, stateManager = table.stateManager, scenegraph = table.scenegraph;
    if (!1 === vglobal.envContribution.supportsTouchEvents) return;
    vglobal.addEventListener("contextmenu", (e => {
        e.stopPropagation(), e.preventDefault();
    }), {
        capture: !0
    }), eventManager.touchMovePoints = [], table.scenegraph.tableGroup.addEventListener("touchstart", (e => {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        if (e.target.isChildOf(scenegraph.component.vScrollBar) || e.target.isChildOf(scenegraph.component.vScrollBar)) return;
        eventManager.isTouchdown = !0;
        const touchEvent = e.nativeEvent;
        eventManager.touchMovePoints.push({
            x: table.rotateDegree ? null !== (_c = null === (_b = null === (_a = touchEvent.changedTouches) || void 0 === _a ? void 0 : _a[0]) || void 0 === _b ? void 0 : _b._canvasX) && void 0 !== _c ? _c : null === (_d = e.canvas) || void 0 === _d ? void 0 : _d.x : e.page.x,
            y: table.rotateDegree ? null !== (_g = null === (_f = null === (_e = touchEvent.changedTouches) || void 0 === _e ? void 0 : _e[0]) || void 0 === _f ? void 0 : _f._canvasY) && void 0 !== _g ? _g : null === (_h = e.canvas) || void 0 === _h ? void 0 : _h.y : e.page.y,
            timestamp: Date.now()
        });
    }));
    const globalTouchMoveCallback = e => {
        var _a, _b;
        if (eventManager.isLongTouch && e.preventDefault(), eventManager.isTouchdown && isTouchEvent(e)) if (eventManager.isTouchMove = !0, 
        (null === (_b = null === (_a = eventManager.downIcon) || void 0 === _a ? void 0 : _a.attribute) || void 0 === _b ? void 0 : _b.funcType) === IconFuncTypeEnum.dragReorder) e.preventDefault(); else if (eventManager.touchMovePoints.length > 4 && eventManager.touchMovePoints.shift(), 
        eventManager.touchMovePoints.push({
            x: table.rotateDegree ? e.changedTouches[0]._canvasX : e.changedTouches[0].pageX,
            y: table.rotateDegree ? e.changedTouches[0]._canvasY : e.changedTouches[0].pageY,
            timestamp: Date.now()
        }), eventManager._enableTableScroll) {
            const deltaX = -eventManager.touchMovePoints[eventManager.touchMovePoints.length - 1].x + eventManager.touchMovePoints[eventManager.touchMovePoints.length - 2].x, deltaY = -eventManager.touchMovePoints[eventManager.touchMovePoints.length - 1].y + eventManager.touchMovePoints[eventManager.touchMovePoints.length - 2].y;
            handleWhell({
                deltaX: deltaX,
                deltaY: deltaY
            }, stateManager), e.cancelable && ("none" === table.internalProps.overscrollBehavior && (deltaY && isVerticalExistScrollBar(stateManager) || deltaX && isHorizontalExistScrollBar(stateManager)) || Math.abs(deltaY) >= Math.abs(deltaX) && 0 !== deltaY && isVerticalScrollable(deltaY, stateManager) || Math.abs(deltaY) <= Math.abs(deltaX) && 0 !== deltaX && isHorizontalScrollable(deltaX, stateManager)) && e.preventDefault();
        }
    };
    vglobal.addEventListener("touchmove", globalTouchMoveCallback, {
        passive: !1
    }), eventManager.globalEventListeners.push({
        name: "touchmove",
        env: "vglobal",
        callback: globalTouchMoveCallback
    });
    const globalTouchEndCallback = e => {
        var _a, _b, _c, _d;
        if (eventManager.touchEnd = !0, eventManager.isLongTouch = !1, eventManager.isTouchdown && isTouchEvent(e)) {
            if ((null === (_b = null === (_a = eventManager.downIcon) || void 0 === _a ? void 0 : _a.attribute) || void 0 === _b ? void 0 : _b.funcType) === IconFuncTypeEnum.dragReorder) e.preventDefault(); else if ((null === (_c = eventManager.touchMovePoints) || void 0 === _c ? void 0 : _c.length) && (eventManager.touchMovePoints.length > 4 && eventManager.touchMovePoints.shift(), 
            eventManager.touchMovePoints.push({
                x: table.rotateDegree ? e.changedTouches[0]._canvasX : e.changedTouches[0].pageX,
                y: table.rotateDegree ? e.changedTouches[0]._canvasY : e.changedTouches[0].pageY,
                timestamp: Date.now()
            }), eventManager._enableTableScroll)) {
                const firstPoint = eventManager.touchMovePoints[0], lastPoint = eventManager.touchMovePoints[(null === (_d = eventManager.touchMovePoints) || void 0 === _d ? void 0 : _d.length) - 1], vX = (lastPoint.x - firstPoint.x) / (lastPoint.timestamp - firstPoint.timestamp), vY = (lastPoint.y - firstPoint.y) / (lastPoint.timestamp - firstPoint.timestamp);
                eventManager.inertiaScroll.startInertia(vX, vY, .95), table.eventManager.inertiaScroll.setScrollHandle(((dx, dy) => {
                    handleWhell({
                        deltaX: -dx,
                        deltaY: -dy
                    }, table.stateManager);
                }));
            }
            eventManager.isTouchdown = !1, eventManager.isTouchMove = !1, eventManager.isDraging = !1, 
            eventManager.touchMovePoints = [];
        }
    };
    vglobal.addEventListener("touchend", globalTouchEndCallback), eventManager.globalEventListeners.push({
        name: "touchend",
        env: "vglobal",
        callback: globalTouchEndCallback
    });
    const globalTouchCancelCallback = e => {
        eventManager.touchEnd = !0, eventManager.isLongTouch = !1, eventManager.isTouchdown && (eventManager.isTouchdown = !1, 
        eventManager.isTouchMove = !1, eventManager.touchMovePoints = [], eventManager.isDraging = !1);
    };
    vglobal.addEventListener("touchcancel", globalTouchCancelCallback), eventManager.globalEventListeners.push({
        name: "touchcancel",
        env: "vglobal",
        callback: globalTouchCancelCallback
    });
}

function isTouchEvent(e) {
    return !!e.changedTouches;
}
//# sourceMappingURL=touch.js.map
