'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-292cc870.js');

const styleCss = "x-number{display:inline-block}";
const XNumberStyle0 = styleCss;

const XNumber = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.value = 0;
        this.from = 0;
        this.fixed = 0;
        this.thousandsSeparator = ',';
        this.interval = 1;
        this.withoutAnimation = false;
        this.lastNumber = undefined;
        this.actualNumber = undefined;
        this.typingTimer = undefined;
    }
    /**
     * Watch changes in value property
     */
    onChangeNumber() {
        if (this.withoutAnimation) {
            this.actualNumber = this.numberWithCommas(this.value);
            this.lastNumber = this.value;
        }
        else {
            clearTimeout(this.typingTimer);
            this.typingTimer = setTimeout(this.animateNumber, this.interval * 1000);
        }
    }
    /**
     * On component will load hook
     */
    componentWillLoad() {
        this.lastNumber = this.from;
        this.actualNumber = this.from.toFixed(this.fixed);
    }
    /**
     * On component did loaded hook
     */
    componentDidLoad() {
        if (this.withoutAnimation) {
            this.actualNumber = this.numberWithCommas(this.value);
        }
        else {
            setTimeout(() => {
                this.animateNumber();
            }, 300);
        }
    }
    /**
     * Start number animation
     */
    animateNumber() {
        let startTimestamp = null;
        const step = (timestamp) => {
            if (!startTimestamp) {
                startTimestamp = timestamp;
            }
            const progress = Math.min((timestamp - startTimestamp) / 1000, 1);
            this.actualNumber = this.numberWithCommas(progress * (this.value - this.lastNumber) + this.lastNumber);
            if (progress < 1) {
                window.requestAnimationFrame(step);
            }
            else {
                this.lastNumber = this.value;
            }
        };
        window.requestAnimationFrame(step);
    }
    /**
     * Format number human readable
     *
     * @param n
     * @returns
     */
    numberWithCommas(n) {
        return parseFloat(n)
            .toFixed(this.fixed)
            .replace(/\B(?=(\d{3})+(?!\d))/g, this.thousandsSeparator);
    }
    /**
     * Render component
     */
    render() {
        return index.h(index.Host, { key: '67aee761100050823da210036536ae5ea84b9e0e' }, this.actualNumber);
    }
    static get watchers() { return {
        "value": ["onChangeNumber"]
    }; }
};
XNumber.style = XNumberStyle0;

exports.x_number = XNumber;
