"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../../utils");
var d3 = require("d3");
var React = require("react");
var id = 1000;
var genId = function () {
    id++;
    return '__count__' + id;
};
/**
 * value: 需要抵达的数
 * last：动画开始的数字，默认为0
 * format: 数的格式
 *          number:整数（默认）
 *          decimal: 保留两位小数
 *          usd: 美国数字(三位后有一逗号分开)
 *          precent: 百分比，此时 value 应该为 0-1的数
 *          chinese: 中文字
 * duration: 换换时间，默认 2000毫秒
*/
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1(props) {
        var _this = _super.call(this, props) || this;
        _this.id = genId();
        _this.last = 0;
        _this.format = 'number';
        _this.duration = 2000;
        if (_this.props.format) {
            _this.format = _this.props.format;
        }
        if (_this.props.duration) {
            _this.duration = _this.props.duration;
        }
        return _this;
    }
    default_1.prototype.tranNumber = function (from, to) {
        d3.select('#' + this.id)
            .text(from)
            //@ts-ignore
            .transition()
            .duration(this.duration)
            .on('start', function () {
            d3.active(this).tween('text', function () {
                var that = d3.select(this);
                return function (p) {
                    that.text(Math.floor(from + p * (to - from)));
                };
            });
        });
    };
    default_1.prototype.tranChinese = function (from, to) {
        d3.select('#' + this.id)
            .text(utils_1.Format.formatNumber(from))
            //@ts-ignore
            .transition()
            .duration(this.duration)
            .on('start', function () {
            d3.active(this).tween('text', function () {
                var that = d3.select(this);
                return function (p) {
                    that.text(utils_1.Format.formatNumber(from + p * (to - from)));
                };
            });
        });
    };
    default_1.prototype.tranUsd = function (from, to) {
        d3.select('#' + this.id)
            .text(utils_1.Format.formatUSD(from))
            //@ts-ignore
            .transition()
            .duration(this.duration)
            .on('start', function () {
            d3.active(this).tween('text', function () {
                var that = d3.select(this);
                return function (p) {
                    that.text(utils_1.Format.formatUSD(Math.floor(from + p * (to - from))));
                };
            });
        });
    };
    default_1.prototype.tranDecimal = function (from, to) {
        d3.select('#' + this.id)
            .text(utils_1.Format.formatDecimal(from))
            //@ts-ignore
            .transition()
            .duration(this.duration)
            .on('start', function () {
            d3.active(this).tween('text', function () {
                var that = d3.select(this);
                return function (p) {
                    that.text(utils_1.Format.formatDecimal(from + p * (to - from)));
                };
            });
        });
    };
    default_1.prototype.tranPrecent = function (from, to) {
        d3.select('#' + this.id)
            .text(utils_1.Format.formatDecimal(from * 100) + '%')
            //@ts-ignore
            .transition()
            .duration(this.duration)
            .on('start', function () {
            d3.active(this).tween('text', function () {
                var that = d3.select(this);
                return function (p) {
                    that.text(utils_1.Format.formatDecimal((from + p * (to - from)) * 100) + '%');
                };
            });
        });
    };
    default_1.prototype.transition = function () {
        var from = this.props.last || this.last;
        var to = this.props.value;
        if (from > to) {
            from = to;
        }
        this.last = this.props.value;
        switch (this.format) {
            case 'decimal':
                this.tranDecimal(from, to);
                break;
            case 'usd':
                this.tranUsd(from, to);
                break;
            case 'precent':
                this.tranPrecent(from, to);
                break;
            case 'chinese':
                this.tranChinese(from, to);
                break;
            default:
                this.tranNumber(from, to);
                break;
        }
    };
    default_1.prototype.componentDidMount = function () {
        this.transition();
    };
    default_1.prototype.componentDidUpdate = function () {
        this.transition();
    };
    default_1.prototype.render = function () {
        return React.createElement("span", { id: this.id, className: "vapfont " + this.props.className }, this.last);
    };
    return default_1;
}(React.PureComponent));
exports.default = default_1;
// module.exports = {
//     usdNumber: function (selector, to, duration) {
//         var d = duration || 2000;
//         var lastTotal = parseInt($(selector).text().replace(/,/g, ''));
//         d3.select(selector)
//             .text(Format.formatUSD(lastTotal))
//             .transition()
//             .duration(d)
//             .on('start', function () {
//                 d3.active(this).tween('text', function () {
//                     var that = d3.select(this);
//                     return function (p) {
//                         that.text(Format.formatUSD(Math.floor(lastTotal + p * (to - lastTotal))));
//                     };
//                 });
//             });
//     },
//     percent: function (selector, to, limit, duration) {
//         var d = duration || 2000;
//         var l = limit || 2;
//         var lastTotal = parseFloat($(selector).text().replace('%', ''));
//         d3.select(selector)
//             .text(Format.formatDecimal(lastTotal, to) + '%')
//             .transition()
//             .duration(d)
//             .on('start', function () {
//                 d3.active(this).tween('text', function () {
//                     var that = d3.select(this);
//                     return function (p) {
//                         that.text(Format.formatDecimal(lastTotal + p * (to * 100 - lastTotal)) + '%');
//                     };
//                 });
//             });
//     }
// };
