import { DefaultTimeline, DefaultTicker, ACustomAnimate, createRect, Generator, Animate } from "./../vrender";

import { isBoolean, isNumber } from "@visactor/vutils";

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

class Animateaaa extends ACustomAnimate {
    onUpdate(end, ratio, out) {
        if (this.from.x !== this.to.x) {
            const x = end ? this.to.x : this.from.x + Math.floor((this.to.x - this.from.x) * ratio);
            this.params.table.scrollLeft = x;
        }
        if (this.from.y !== this.to.y) {
            const y = end ? this.to.y : this.from.y + Math.floor((this.to.y - this.from.y) * ratio);
            this.params.table.scrollTop = y;
        }
    }
}

export class TableAnimationManager {
    constructor(table) {
        this.table = table, this.timeline = new DefaultTimeline, this.ticker = new DefaultTicker(this.table.scenegraph.stage), 
        this.ticker.addTimeline(this.timeline);
        this.ticker.setFPS(60), this.tempGraphic = createRect({});
    }
    scrollTo(position, animationOption) {
        var _a, _b;
        const from = {
            x: this.table.scrollLeft,
            y: this.table.scrollTop
        }, {col: col, row: row} = position;
        let colDecimal, rowDecimal, colInt = col, rowInt = row;
        isNumber(col) && !isInteger(col) && (colInt = Math.floor(col), colDecimal = col - colInt), 
        isNumber(row) && !isInteger(row) && (rowInt = Math.floor(row), rowDecimal = row - rowInt);
        const cellRect = this.table.getCellRect(null != colInt ? colInt : 0, null != rowInt ? rowInt : 0);
        let {left: left, top: top} = cellRect;
        colDecimal && (left += colDecimal * cellRect.width), rowDecimal && (top += rowDecimal * cellRect.height);
        const to = {
            x: isNumber(col) ? left - this.table.getFrozenColsWidth() : this.table.scrollLeft,
            y: isNumber(row) ? top - this.table.getFrozenRowsHeight() : this.table.scrollTop
        }, duration = isBoolean(animationOption) ? animationOption ? 3e3 : 0 : null !== (_a = null == animationOption ? void 0 : animationOption.duration) && void 0 !== _a ? _a : 3e3, easing = isBoolean(animationOption) ? animationOption ? "linear" : "" : null !== (_b = null == animationOption ? void 0 : animationOption.easing) && void 0 !== _b ? _b : "linear";
        new Animate(Generator.GenAutoIncrementId(), this.timeline).bind(this.tempGraphic).play(new Animateaaa(from, to, duration, easing, {
            graphic: this.tempGraphic,
            table: this.table
        }));
        this.ticker.start();
    }
    clear() {
        this.timeline.clear(), this.ticker.stop();
    }
}
//# sourceMappingURL=animation.js.map