"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.bindScrollBarListener = void 0;

const util_1 = require("../../tools/util"), ts_types_1 = require("../../ts-types"), TABLE_EVENT_TYPE_1 = require("../../core/TABLE_EVENT_TYPE");

function bindScrollBarListener(eventManager) {
    const table = eventManager.table, stateManager = table.stateManager, scenegraph = table.scenegraph;
    scenegraph.component.vScrollBar.addEventListener("pointerover", (e => {
        stateManager.showVerticalScrollBar();
    })), scenegraph.component.hScrollBar.addEventListener("pointerover", (e => {
        stateManager.showHorizontalScrollBar();
    })), scenegraph.component.vScrollBar.addEventListener("pointerout", (e => {
        stateManager.interactionState !== ts_types_1.InteractionState.scrolling && stateManager.hideVerticalScrollBar();
    })), scenegraph.component.hScrollBar.addEventListener("pointerout", (e => {
        stateManager.interactionState !== ts_types_1.InteractionState.scrolling && stateManager.hideHorizontalScrollBar();
    })), scenegraph.component.vScrollBar.addEventListener("pointermove", (e => {
        scenegraph.table.stateManager.updateCursor("default"), e.stopPropagation();
    })), scenegraph.component.vScrollBar.addEventListener("pointerdown", (e => {
        e.stopPropagation(), scenegraph.table.hasListeners(TABLE_EVENT_TYPE_1.TABLE_EVENT_TYPE.MOUSEDOWN_TABLE) && scenegraph.table.fireListeners(TABLE_EVENT_TYPE_1.TABLE_EVENT_TYPE.MOUSEDOWN_TABLE, {
            event: e.nativeEvent
        });
    })), scenegraph.component.vScrollBar.addEventListener("scrollDown", (e => {
        var _a;
        scenegraph.table.eventManager.LastBodyPointerXY = {
            x: e.x,
            y: e.y
        }, scenegraph.table.eventManager.isDown = !0, stateManager.interactionState !== ts_types_1.InteractionState.scrolling && stateManager.updateInteractionState(ts_types_1.InteractionState.scrolling), 
        scenegraph.table.stateManager.hideMenu(), null === (_a = scenegraph.table.editorManager) || void 0 === _a || _a.completeEdit(), 
        scenegraph.table.hasListeners(TABLE_EVENT_TYPE_1.TABLE_EVENT_TYPE.MOUSEDOWN_TABLE) && scenegraph.table.fireListeners(TABLE_EVENT_TYPE_1.TABLE_EVENT_TYPE.MOUSEDOWN_TABLE, {
            event: e.nativeEvent
        });
    })), scenegraph.component.vScrollBar.addEventListener("pointerup", (() => {
        stateManager.fastScrolling = !1, scenegraph.table.eventManager.isDraging = !1, stateManager.interactionState === ts_types_1.InteractionState.scrolling && stateManager.updateInteractionState(ts_types_1.InteractionState.default);
    })), scenegraph.component.vScrollBar.addEventListener("pointerupoutside", (() => {
        stateManager.fastScrolling = !1, stateManager.interactionState === ts_types_1.InteractionState.scrolling && stateManager.updateInteractionState(ts_types_1.InteractionState.default);
    })), scenegraph.component.vScrollBar.addEventListener("scrollUp", (e => {
        scenegraph.table.eventManager.isDraging = !1;
    })), scenegraph.component.hScrollBar.addEventListener("pointermove", (e => {
        scenegraph.table.stateManager.updateCursor(), e.stopPropagation();
    })), scenegraph.component.hScrollBar.addEventListener("pointerdown", (e => {
        e.stopPropagation(), scenegraph.table.hasListeners(TABLE_EVENT_TYPE_1.TABLE_EVENT_TYPE.MOUSEDOWN_TABLE) && scenegraph.table.fireListeners(TABLE_EVENT_TYPE_1.TABLE_EVENT_TYPE.MOUSEDOWN_TABLE, {
            event: e.nativeEvent
        });
    })), scenegraph.component.hScrollBar.addEventListener("scrollDown", (e => {
        var _a;
        scenegraph.table.eventManager.LastBodyPointerXY = {
            x: e.x,
            y: e.y
        }, scenegraph.table.eventManager.isDown = !0, stateManager.interactionState !== ts_types_1.InteractionState.scrolling && stateManager.updateInteractionState(ts_types_1.InteractionState.scrolling), 
        scenegraph.table.stateManager.hideMenu(), null === (_a = scenegraph.table.editorManager) || void 0 === _a || _a.completeEdit(), 
        scenegraph.table.hasListeners(TABLE_EVENT_TYPE_1.TABLE_EVENT_TYPE.MOUSEDOWN_TABLE) && scenegraph.table.fireListeners(TABLE_EVENT_TYPE_1.TABLE_EVENT_TYPE.MOUSEDOWN_TABLE, {
            event: e.nativeEvent
        });
    })), scenegraph.component.hScrollBar.addEventListener("pointerup", (() => {
        stateManager.fastScrolling = !1, scenegraph.table.eventManager.isDraging = !1, stateManager.interactionState === ts_types_1.InteractionState.scrolling && stateManager.updateInteractionState(ts_types_1.InteractionState.default);
    })), scenegraph.component.hScrollBar.addEventListener("pointerupoutside", (() => {
        stateManager.fastScrolling = !1, stateManager.interactionState === ts_types_1.InteractionState.scrolling && stateManager.updateInteractionState(ts_types_1.InteractionState.default);
    })), scenegraph.component.hScrollBar.addEventListener("scrollUp", (e => {
        scenegraph.table.eventManager.isDraging = !1;
    }));
    const throttleVerticalWheel = (0, util_1.throttle)(stateManager.updateVerticalScrollBar, 20), throttleHorizontalWheel = (0, 
    util_1.throttle)(stateManager.updateHorizontalScrollBar, 20);
    scenegraph.component.vScrollBar.addEventListener("scrollDrag", (e => {
        scenegraph.table.eventManager.isDown && (scenegraph.table.eventManager.isDraging = !0), 
        stateManager.fastScrolling = !0, stateManager.interactionState !== ts_types_1.InteractionState.scrolling && stateManager.updateInteractionState(ts_types_1.InteractionState.scrolling);
        const ratio = e.detail.value[0] / (1 - e.detail.value[1] + e.detail.value[0]);
        throttleVerticalWheel(ratio, e);
    })), scenegraph.component.hScrollBar.addEventListener("scrollDrag", (e => {
        scenegraph.table.eventManager.isDown && (scenegraph.table.eventManager.isDraging = !0), 
        stateManager.fastScrolling = !0, stateManager.interactionState !== ts_types_1.InteractionState.scrolling && stateManager.updateInteractionState(ts_types_1.InteractionState.scrolling);
        const ratio = e.detail.value[0] / (1 - e.detail.value[1] + e.detail.value[0]);
        throttleHorizontalWheel(ratio);
    }));
}

exports.bindScrollBarListener = bindScrollBarListener;
//# sourceMappingURL=scroll-bar.js.map
