import { TABLE_EVENT_TYPE } from "@visactor/vtable";

function isInteger(value) {
    return Math.floor(value) === value;
}

export class TableCarouselAnimationPlugin {
    constructor(options = {}) {
        var _a, _b, _c, _d, _e, _f, _g, _h;
        this.id = "table-carousel-animation", this.name = "Table Carousel Animation", this.runTime = [ TABLE_EVENT_TYPE.INITIALIZED ], 
        this.willUpdateRow = !1, this.willUpdateCol = !1, this.id = null !== (_a = options.id) && void 0 !== _a ? _a : this.id, 
        this.rowCount = null !== (_b = null == options ? void 0 : options.rowCount) && void 0 !== _b ? _b : void 0, 
        this.colCount = null !== (_c = null == options ? void 0 : options.colCount) && void 0 !== _c ? _c : void 0, 
        this.animationDuration = null !== (_d = null == options ? void 0 : options.animationDuration) && void 0 !== _d ? _d : 500, 
        this.animationDelay = null !== (_e = null == options ? void 0 : options.animationDelay) && void 0 !== _e ? _e : 1e3, 
        this.animationEasing = null !== (_f = null == options ? void 0 : options.animationEasing) && void 0 !== _f ? _f : "linear", 
        this.autoPlay = null !== (_g = null == options ? void 0 : options.autoPlay) && void 0 !== _g && _g, 
        this.autoPlayDelay = null !== (_h = null == options ? void 0 : options.autoPlayDelay) && void 0 !== _h ? _h : 0, 
        this.customDistColFunction = options.customDistColFunction, this.customDistRowFunction = options.customDistRowFunction;
    }
    run(...args) {
        this.table || (this.table = args[2]), this.reset(), this.autoPlay && setTimeout((() => {
            this.play();
        }), this.autoPlayDelay);
    }
    reset() {
        this.playing = !1, this.row = this.table.frozenRowCount, this.col = this.table.frozenColCount;
    }
    play() {
        if (!this.table) throw new Error("table is not initialized");
        this.playing = !0, this.rowCount && !this.willUpdateRow ? this.updateRow() : this.colCount && !this.willUpdateCol && this.updateCol();
    }
    pause() {
        this.playing = !1;
    }
    updateRow() {
        var _a;
        if (!this.playing || this.table.isReleased) return;
        let animation = !0;
        const screenTopRow = Math.max(this.table.scenegraph.proxy.screenTopRow, this.table.frozenRowCount), customRow = this.customDistRowFunction && this.customDistRowFunction(this.row, this.table);
        customRow ? (this.row = customRow.distRow, animation = null === (_a = customRow.animation) || void 0 === _a || _a) : isInteger(this.row) && screenTopRow !== this.row ? (this.row = this.table.frozenRowCount, 
        animation = !1) : isInteger(this.row) || screenTopRow === Math.floor(this.row) ? this.row += this.rowCount : (this.row = this.table.frozenRowCount, 
        animation = !1), this.table.scrollToRow(this.row, animation ? {
            duration: this.animationDuration,
            easing: this.animationEasing
        } : void 0), this.willUpdateRow = !0, setTimeout((() => {
            this.willUpdateRow = !1, this.updateRow();
        }), this.animationDuration + this.animationDelay);
    }
    updateCol() {
        var _a;
        if (!this.playing || this.table.isReleased) return;
        let animation = !0;
        const customCol = this.customDistColFunction && this.customDistColFunction(this.col, this.table);
        customCol ? (this.col = customCol.distCol, animation = null === (_a = customCol.animation) || void 0 === _a || _a) : isInteger(this.col) && this.table.scenegraph.proxy.screenLeftCol !== this.col ? (this.col = this.table.frozenColCount, 
        animation = !1) : isInteger(this.col) || this.table.scenegraph.proxy.screenLeftCol === Math.floor(this.col) ? this.col += this.colCount : (this.col = this.table.frozenColCount, 
        animation = !1), this.table.scrollToCol(this.col, animation ? {
            duration: this.animationDuration,
            easing: this.animationEasing
        } : void 0), this.willUpdateCol = !0, setTimeout((() => {
            this.willUpdateCol = !1, this.updateCol();
        }), this.animationDuration + this.animationDelay);
    }
    release() {}
}
//# sourceMappingURL=table-carousel-animation.js.map