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

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

export function handleWhell(event, state, isWheelEvent = !0) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
    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 !== InteractionState.scrolling && state.updateInteractionState(InteractionState.scrolling);
    const visible1 = null === (_a = state.table.theme.scrollStyle) || void 0 === _a ? void 0 : _a.visible, horizontalAutoHide = "scrolling" === (null !== (_c = null === (_b = state.table.theme.scrollStyle) || void 0 === _b ? void 0 : _b.horizontalVisible) && void 0 !== _c ? _c : visible1), verticalAutoHide = "scrolling" === (null !== (_e = null === (_d = state.table.theme.scrollStyle) || void 0 === _d ? void 0 : _d.verticalVisible) && void 0 !== _e ? _e : visible1);
    let usedFrozenHorizontal = !1, usedRightFrozenHorizontal = !1, blockedFrozenHorizontal = !1, blockedRightFrozenHorizontal = !1;
    if (optimizedDeltaX) {
        const pxEvent = event.x, pyEvent = event.y, pxFallback = null === (_g = null === (_f = state.table.eventManager) || void 0 === _f ? void 0 : _f.LastBodyPointerXY) || void 0 === _g ? void 0 : _g.x, pyFallback = null === (_j = null === (_h = state.table.eventManager) || void 0 === _h ? void 0 : _h.LastBodyPointerXY) || void 0 === _j ? void 0 : _j.y, px = "number" == typeof pxEvent ? pxEvent : pxFallback, py = "number" == typeof pyEvent ? pyEvent : pyFallback, relativeX = "number" == typeof px ? px - state.table.tableX : NaN, relativeY = "number" == typeof py ? py - state.table.tableY : NaN, isInTable = isFinite(relativeX) && isFinite(relativeY) && relativeX >= 0 && relativeY >= 0 && relativeX <= state.table.tableNoFrameWidth && relativeY <= state.table.tableNoFrameHeight, frozenColsScrollable = state.table.options.scrollFrozenCols && state.table.getFrozenColsOffset() > 0, rightFrozenColsScrollable = state.table.options.scrollRightFrozenCols && state.table.getRightFrozenColsOffset() > 0, isInFrozenViewport = isInTable && relativeX > 0 && relativeX < state.table.getFrozenColsWidth(), isInRightFrozenViewport = isInTable && relativeX > state.table.tableNoFrameWidth - state.table.getRightFrozenColsWidth();
        if (frozenColsScrollable && isInFrozenViewport) {
            const maxFrozenScrollLeft = state.table.getFrozenColsOffset(), nextFrozenScrollLeft = state.scroll.frozenHorizontalBarPos + optimizedDeltaX;
            optimizedDeltaX < 0 && state.scroll.frozenHorizontalBarPos > 0 || optimizedDeltaX > 0 && state.scroll.frozenHorizontalBarPos < maxFrozenScrollLeft ? (state.setFrozenColsScrollLeft(nextFrozenScrollLeft), 
            usedFrozenHorizontal = !0, state.showHorizontalScrollBar(horizontalAutoHide, "frozen")) : state.table.options.scrollFrozenColsPassThroughToBody ? (state.setScrollLeft(state.scroll.horizontalBarPos + optimizedDeltaX, event), 
            state.showHorizontalScrollBar(horizontalAutoHide, "body")) : (blockedFrozenHorizontal = !0, 
            state.showHorizontalScrollBar(horizontalAutoHide, "frozen"));
        } else if (rightFrozenColsScrollable && isInRightFrozenViewport) {
            const maxRightFrozenScrollLeft = state.table.getRightFrozenColsOffset(), rightFrozenDelta = -optimizedDeltaX, nextRightFrozenScrollLeft = state.scroll.rightFrozenHorizontalBarPos + rightFrozenDelta;
            rightFrozenDelta < 0 && state.scroll.rightFrozenHorizontalBarPos > 0 || rightFrozenDelta > 0 && state.scroll.rightFrozenHorizontalBarPos < maxRightFrozenScrollLeft ? (state.setRightFrozenColsScrollLeft(nextRightFrozenScrollLeft), 
            usedRightFrozenHorizontal = !0, state.showHorizontalScrollBar(horizontalAutoHide, "rightFrozen")) : state.table.options.scrollFrozenColsPassThroughToBody ? (state.setScrollLeft(state.scroll.horizontalBarPos + optimizedDeltaX, event), 
            state.showHorizontalScrollBar(horizontalAutoHide, "body")) : (blockedRightFrozenHorizontal = !0, 
            state.showHorizontalScrollBar(horizontalAutoHide, "rightFrozen"));
        } else state.setScrollLeft(state.scroll.horizontalBarPos + optimizedDeltaX, event), 
        state.showHorizontalScrollBar(horizontalAutoHide, "body");
    }
    optimizedDeltaY && (state.setScrollTop(state.scroll.verticalBarPos + optimizedDeltaY, event), 
    state.showVerticalScrollBar(verticalAutoHide)), isWheelEvent && state.resetInteractionState(state.interactionStateBeforeScroll), 
    (null === (_k = event.nativeEvent) || void 0 === _k ? void 0 : _k.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 && !blockedFrozenHorizontal && !blockedRightFrozenHorizontal && (usedFrozenHorizontal ? state.table.getFrozenColsOffset() > 0 : usedRightFrozenHorizontal ? state.table.getRightFrozenColsOffset() > 0 : 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)) ];
}

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

export function isHorizontalScrollable(deltaX, state) {
    var _a, _b, _c, _d, _e, _f;
    const frozenOffset = null !== (_c = null === (_b = (_a = state.table).getFrozenColsOffset) || void 0 === _b ? void 0 : _b.call(_a)) && void 0 !== _c ? _c : 0, rightFrozenOffset = null !== (_f = null === (_e = (_d = state.table).getRightFrozenColsOffset) || void 0 === _e ? void 0 : _e.call(_d)) && void 0 !== _f ? _f : 0;
    return 0 != state.table.getAllColsWidth() - state.table.scenegraph.width - frozenOffset - rightFrozenOffset && (!isScrollToLeft(deltaX, state) && !isScrollToRight(deltaX, state));
}

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

export function isHorizontalExistScrollBar(state) {
    var _a, _b, _c, _d, _e, _f;
    const frozenOffset = null !== (_c = null === (_b = (_a = state.table).getFrozenColsOffset) || void 0 === _b ? void 0 : _b.call(_a)) && void 0 !== _c ? _c : 0, rightFrozenOffset = null !== (_f = null === (_e = (_d = state.table).getRightFrozenColsOffset) || void 0 === _e ? void 0 : _e.call(_d)) && void 0 !== _f ? _f : 0;
    return !(state.table.getAllColsWidth() - state.table.scenegraph.width - frozenOffset - rightFrozenOffset <= 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) {
    var _a, _b, _c, _d, _e, _f;
    const frozenOffset = null !== (_c = null === (_b = (_a = state.table).getFrozenColsOffset) || void 0 === _b ? void 0 : _b.call(_a)) && void 0 !== _c ? _c : 0, rightFrozenOffset = null !== (_f = null === (_e = (_d = state.table).getRightFrozenColsOffset) || void 0 === _e ? void 0 : _e.call(_d)) && void 0 !== _f ? _f : 0;
    return 0 !== state.table.getAllColsWidth() - state.table.scenegraph.width - frozenOffset - rightFrozenOffset && deltaX <= 0 && state.scroll.horizontalBarPos < 1;
}

function isScrollToRight(deltaX, state) {
    var _a, _b, _c, _d, _e, _f, _g;
    const sizeTolerance = (null === (_a = state.table.options.customConfig) || void 0 === _a ? void 0 : _a._disableColumnAndRowSizeRound) ? 1 : 0, frozenOffset = null !== (_d = null === (_c = (_b = state.table).getFrozenColsOffset) || void 0 === _c ? void 0 : _c.call(_b)) && void 0 !== _d ? _d : 0, rightFrozenOffset = null !== (_g = null === (_f = (_e = state.table).getRightFrozenColsOffset) || void 0 === _f ? void 0 : _f.call(_e)) && void 0 !== _g ? _g : 0, totalWidth = state.table.getAllColsWidth() - state.table.scenegraph.width - frozenOffset - rightFrozenOffset;
    return 0 !== totalWidth && deltaX >= 0 && Math.abs(state.scroll.horizontalBarPos - totalWidth) < 1 + sizeTolerance;
}

export 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 = 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 = vglobal.getRequestAnimationFrame()(this.inertia.bind(this)));
    }
    endInertia() {
        vglobal.getCancelAnimationFrame()(this.runingId), this.runingId = null;
    }
    isInertiaScrolling() {
        return !!this.runingId;
    }
}
//# sourceMappingURL=scroll.js.map