const defaults = {
    startVal: 0,
    decimalPlaces: 0,
    duration: 2,
    useEasing: true,
    useGrouping: true,
    smartEasingThreshold: 999,
    smartEasingAmount: 333,
    separator: ',',
    decimal: '.',
    prefix: '',
    suffix: ''
};
export class CountUp {
    constructor(target, endVal, options) {
        this.version = '2.0.4';
        this.startTime = 0;
        this.remaining = 0;
        this.finalEndVal = 0;
        this.useEasing = true;
        this.countDown = false;
        this.error = '';
        this.startVal = 0;
        this.duration = 0;
        this.paused = true;
        this.count = (timestamp) => {
            if (!this.startTime) {
                this.startTime = timestamp;
            }
            const progress = timestamp - this.startTime;
            this.remaining = this.duration - progress;
            if (this.useEasing) {
                if (this.countDown) {
                    this.frameVal = this.startVal - this.easingFn(progress, 0, this.startVal - this.endVal, this.duration);
                }
                else {
                    this.frameVal = this.easingFn(progress, this.startVal, this.endVal - this.startVal, this.duration);
                }
            }
            else {
                if (this.countDown) {
                    this.frameVal = this.startVal - ((this.startVal - this.endVal) * (progress / this.duration));
                }
                else {
                    this.frameVal = this.startVal + (this.endVal - this.startVal) * (progress / this.duration);
                }
            }
            if (this.countDown) {
                this.frameVal = (this.frameVal < this.endVal) ? this.endVal : this.frameVal;
            }
            else {
                this.frameVal = (this.frameVal > this.endVal) ? this.endVal : this.frameVal;
            }
            this.frameVal = Math.round(this.frameVal * this.decimalMult) / this.decimalMult;
            this.printValue(this.frameVal);
            if (progress < this.duration) {
                this.rAF = requestAnimationFrame(this.count);
            }
            else if (this.finalEndVal !== null) {
                this.update(this.finalEndVal);
            }
            else {
                if (this.callback) {
                    this.callback();
                }
            }
        };
        this.formatNumber = (num) => {
            const neg = (num < 0) ? '-' : '';
            let result, x, x1, x2, x3;
            result = Math.abs(num).toFixed(this.options.decimalPlaces);
            result += '';
            x = result.split('.');
            x1 = x[0];
            x2 = x.length > 1 ? this.options.decimal + x[1] : '';
            if (this.options.useGrouping) {
                x3 = '';
                for (let i = 0, len = x1.length; i < len; ++i) {
                    if (i !== 0 && (i % 3) === 0) {
                        x3 = this.options.separator + x3;
                    }
                    x3 = x1[len - i - 1] + x3;
                }
                x1 = x3;
            }
            if (this.options.numerals && this.options.numerals.length) {
                var numerals = this.options.numerals;
                x1 = x1.replace(/[0-9]/g, (w) => numerals[+w]);
                x2 = x2.replace(/[0-9]/g, (w) => numerals[+w]);
            }
            return neg + this.options.prefix + x1 + x2 + this.options.suffix;
        };
        this.easeOutExpo = (t, b, c, d) => c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
        this.options = { ...defaults, ...options, };
        this.formattingFn = (this.options.formattingFn) ?
            this.options.formattingFn : this.formatNumber;
        this.easingFn = (this.options.easingFn) ?
            this.options.easingFn : this.easeOutExpo;
        this.startVal = this.validateValue(this.options.startVal);
        this.frameVal = this.startVal;
        this.endVal = this.validateValue(endVal);
        this.options.decimalPlaces = Math.max(0 || this.options.decimalPlaces);
        this.decimalMult = Math.pow(10, this.options.decimalPlaces);
        this.resetDuration();
        this.options.separator = String(this.options.separator);
        this.useEasing = this.options.useEasing;
        if (this.options.separator === '') {
            this.options.useGrouping = false;
        }
        this.el = (typeof target === 'string') ? document.getElementById(target) : target;
        if (this.el) {
            this.printValue(this.startVal);
        }
        else {
            throw Error.new(this.error = '[CountUp] target is null or undefined');
        }
    }
    determineDirectionAndSmartEasing() {
        const end = (this.finalEndVal) ? this.finalEndVal : this.endVal;
        this.countDown = (this.startVal > end);
        const animateAmount = end - this.startVal;
        if (Math.abs(animateAmount) > this.options.smartEasingThreshold) {
            this.finalEndVal = end;
            const up = (this.countDown) ? 1 : -1;
            this.endVal = end + (up * this.options.smartEasingAmount);
            this.duration = this.duration / 2;
        }
        else {
            this.endVal = end;
            this.finalEndVal = 0;
        }
        if (this.finalEndVal) {
            this.useEasing = false;
        }
        else {
            this.useEasing = this.options.useEasing;
        }
    }
    start(callback) {
        if (this.error) {
            return;
        }
        this.callback = callback;
        if (this.duration > 0) {
            this.determineDirectionAndSmartEasing();
            this.paused = false;
            this.rAF = requestAnimationFrame(this.count);
        }
        else {
            this.printValue(this.endVal);
        }
    }
    pauseResume() {
        if (!this.paused) {
            cancelAnimationFrame(this.rAF);
        }
        else {
            this.startTime = 0;
            this.duration = this.remaining;
            this.startVal = this.frameVal;
            this.determineDirectionAndSmartEasing();
            this.rAF = requestAnimationFrame(this.count);
        }
        this.paused = !this.paused;
    }
    reset() {
        cancelAnimationFrame(this.rAF);
        this.paused = true;
        this.resetDuration();
        this.startVal = this.validateValue(this.options.startVal);
        this.frameVal = this.startVal;
        this.printValue(this.startVal);
    }
    update(newEndVal) {
        cancelAnimationFrame(this.rAF);
        this.startTime = 0;
        this.endVal = this.validateValue(newEndVal);
        if (this.endVal === this.frameVal) {
            return;
        }
        this.startVal = this.frameVal;
        if (!this.finalEndVal) {
            this.resetDuration();
        }
        this.determineDirectionAndSmartEasing();
        this.rAF = requestAnimationFrame(this.count);
    }
    printValue(val) {
        const result = this.formattingFn(val);
        if (this.el.tagName === 'INPUT') {
            const input = this.el;
            input.value = result;
        }
        else if (this.el.tagName === 'text' || this.el.tagName === 'tspan') {
            this.el.textContent = result;
        }
        else {
            this.el.innerHTML = result;
        }
    }
    ensureNumber(n) {
        return (typeof n === 'number' && !isNaN(n));
    }
    validateValue(value) {
        const newValue = Number(value);
        if (!this.ensureNumber(newValue)) {
            this.error = `[CountUp] invalid start or end value: ${value}`;
            return -1;
        }
        else {
            return newValue;
        }
    }
    resetDuration() {
        this.startTime = 0;
        this.duration = Number(this.options.duration) * 1000;
        this.remaining = this.duration;
    }
}
