"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.InertiaScroll = exports.isHorizontalExistScrollBar = exports.isVerticalExistScrollBar = exports.isHorizontalScrollable = exports.isVerticalScrollable = exports.handleWhell = void 0;

const ts_types_1 = require("../ts-types"), vrender_1 = require("./../vrender");

function handleWhell(event, state, isWheelEvent = !0) {
    var _a, _b, _c;
    let {deltaX: deltaX, deltaY: deltaY} = event;
    event.shiftKey && event.deltaY && (deltaX = deltaY, deltaY = 0);
    const [optimizedDeltaX, optimizedDeltaY] = optimizeScrollXY(deltaX, deltaY, {
        horizontal: 1,
        vertical: 1
    });
    (optimizedDeltaX || optimizedDeltaY) && state.interactionState !== ts_types_1.InteractionState.scrolling && state.updateInteractionState(ts_types_1.InteractionState.scrolling);
    const autoHide = "scrolling" === (null === (_b = null === (_a = state.table.options.theme) || void 0 === _a ? void 0 : _a.scrollStyle) || void 0 === _b ? void 0 : _b.visible);
    optimizedDeltaX && (state.setScrollLeft(state.scroll.horizontalBarPos + optimizedDeltaX, event), 
    state.showHorizontalScrollBar(autoHide)), optimizedDeltaY && (state.setScrollTop(state.scroll.verticalBarPos + optimizedDeltaY, event), 
    state.showVerticalScrollBar(autoHide)), isWheelEvent && state.resetInteractionState(state.interactionStateBeforeScroll), 
    (null === (_c = event.nativeEvent) || void 0 === _c ? void 0 : _c.cancelable) && ("none" === state.table.internalProps.overscrollBehavior && (deltaY && isVerticalExistScrollBar(state) || deltaX && isHorizontalExistScrollBar(state)) || Math.abs(deltaY) >= Math.abs(deltaX) && 0 !== deltaY && isVerticalScrollable(deltaY, state) || Math.abs(deltaY) <= Math.abs(deltaX) && 0 !== deltaX && isHorizontalScrollable(deltaX, state)) && event.nativeEvent.preventDefault();
}

function optimizeScrollXY(x, y, ratio) {
    var _a, _b;
    const angle = Math.abs(x / y), deltaX = angle <= .5 ? 0 : x, deltaY = angle > 2 ? 0 : y;
    return [ Math.ceil(deltaX * (null !== (_a = ratio.horizontal) && void 0 !== _a ? _a : 0)), Math.ceil(deltaY * (null !== (_b = ratio.vertical) && void 0 !== _b ? _b : 0)) ];
}

function isVerticalScrollable(deltaY, state) {
    return 0 != state.table.getAllRowsHeight() - state.table.scenegraph.height && (!isScrollToTop(deltaY, state) && !isScrollToBottom(deltaY, state));
}

function isHorizontalScrollable(deltaX, state) {
    return 0 != state.table.getAllColsWidth() - state.table.scenegraph.width && (!isScrollToLeft(deltaX, state) && !isScrollToRight(deltaX, state));
}

function isVerticalExistScrollBar(state) {
    return !(state.table.getAllRowsHeight() - state.table.scenegraph.height <= 0);
}

function isHorizontalExistScrollBar(state) {
    return !(state.table.getAllColsWidth() - state.table.scenegraph.width <= 0);
}

function isScrollToTop(deltaY, state) {
    return 0 !== state.table.getAllRowsHeight() - state.table.scenegraph.height && deltaY <= 0 && state.scroll.verticalBarPos < 1;
}

function isScrollToBottom(deltaY, state) {
    var _a;
    const sizeTolerance = (null === (_a = state.table.options.customConfig) || void 0 === _a ? void 0 : _a._disableColumnAndRowSizeRound) ? 1 : 0, totalHeight = state.table.getAllRowsHeight() - state.table.scenegraph.height;
    return 0 !== totalHeight && deltaY >= 0 && Math.abs(state.scroll.verticalBarPos - totalHeight) < 1 + sizeTolerance;
}

function isScrollToLeft(deltaX, state) {
    return 0 !== state.table.getAllColsWidth() - state.table.scenegraph.width && deltaX <= 0 && state.scroll.horizontalBarPos < 1;
}

function isScrollToRight(deltaX, state) {
    var _a;
    const sizeTolerance = (null === (_a = state.table.options.customConfig) || void 0 === _a ? void 0 : _a._disableColumnAndRowSizeRound) ? 1 : 0, totalWidth = state.table.getAllColsWidth() - state.table.scenegraph.width;
    return 0 !== totalWidth && deltaX >= 0 && Math.abs(state.scroll.horizontalBarPos - totalWidth) < 1 + sizeTolerance;
}

exports.handleWhell = handleWhell, exports.isVerticalScrollable = isVerticalScrollable, 
exports.isHorizontalScrollable = isHorizontalScrollable, exports.isVerticalExistScrollBar = isVerticalExistScrollBar, 
exports.isHorizontalExistScrollBar = isHorizontalExistScrollBar;

class InertiaScroll {
    constructor(stateManager) {
        this.stateManager = stateManager;
    }
    setScrollHandle(scrollHandle) {
        this.scrollHandle = scrollHandle;
    }
    startInertia(speedX, speedY, friction) {
        this.lastTime = Date.now(), this.speedX = speedX, this.speedY = speedY, this.friction = friction, 
        this.runingId || (this.runingId = vrender_1.vglobal.getRequestAnimationFrame()(this.inertia.bind(this)));
    }
    inertia() {
        var _a;
        const now = Date.now(), dffTime = now - this.lastTime;
        let stopped = !0;
        const f = Math.pow(this.friction, dffTime / 16), newSpeedX = f * this.speedX, newSpeedY = f * this.speedY;
        let dx = 0, dy = 0;
        Math.abs(newSpeedX) > .05 && (stopped = !1, dx = (this.speedX + newSpeedX) / 2 * dffTime), 
        Math.abs(newSpeedY) > .05 && (stopped = !1, dy = (this.speedY + newSpeedY) / 2 * dffTime), 
        null === (_a = this.scrollHandle) || void 0 === _a || _a.call(this, dx, dy), stopped ? this.runingId = null : (this.lastTime = now, 
        this.speedX = newSpeedX, this.speedY = newSpeedY, this.runingId = vrender_1.vglobal.getRequestAnimationFrame()(this.inertia.bind(this)));
    }
    endInertia() {
        vrender_1.vglobal.getCancelAnimationFrame()(this.runingId), this.runingId = null;
    }
    isInertiaScrolling() {
        return !!this.runingId;
    }
}

exports.InertiaScroll = InertiaScroll;
//# sourceMappingURL=scroll.js.map