"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 __());
    };
})();
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var _ = require("lodash");
var d3 = require("d3");
var common_1 = require("./common");
var ORGIN = function (v) { return v; };
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        /**
         * 内部样式，如果需要添加className 可以重写此属性
        */
        _this.__ClassName = '';
        _this.__Grid = {
            top: 12,
            left: 12,
            right: 12,
            bottom: 12,
        };
        /**
         * 是否等到有数据时才build
        */
        _this._WAIT = false;
        /**
         * 最后更新时年数据
         */
        _this._last_data = null;
        /**
         * 默认的最大显示条数，默认为10
        */
        _this.MAXCOUNT = 10;
        /**
         * 自增的id
        */
        _this.id = common_1.DefUtil.genId();
        /**
         * 是否已经loaded
        */
        _this.loaded = false;
        /**
         * 格式化标签
        */
        _this.FormatLabel = ORGIN;
        /**
         * 格式化值
        */
        _this.FormatValue = common_1.Format.buildFormat('number');
        /**
         * 处理后的数据
        */
        _this.DATA = null;
        /**
         * SVG 根结点对象
        */
        _this.SVG = null;
        /**
         * 过渡时间
        */
        _this.DURATION = 2000;
        /**
         * 总宽度
         */
        _this.WIDTH = 0;
        /**
         * 总高度
         */
        _this.HEIGHT = 0;
        /**
         * 展示空间
         * */
        _this.LAYOUT = { x: 0, y: 0, w: 0, h: 0 };
        return _this;
    }
    /**
     * 最最小最大值，可用来替代 d3.extent 方法
    */
    default_1.prototype.extent = function (data, fn) {
        var _a = __read(d3.extent(data, fn), 2), min = _a[0], max = _a[1];
        // @ts-ignore
        min = (min === undefined ? 0 : min);
        // @ts-ignore
        max = max === undefined ? 1 : max;
        // @ts-ignore
        return [min, max];
    };
    default_1.prototype.itemClass = function (d, i) {
        return "_item _item_" + i + " " + (this.props.onClick ? '_item_hover' : '');
    };
    /**
     * build 方法会在图表标签导入时自动调用，必须实现此方法
    */
    default_1.prototype.build = function () { this.update(); };
    /**
     * resize方法会在宽高发生变化的情况下自动调用,默认重写
    */
    default_1.prototype.resize = function () {
        if (this.loaded) {
            this.sort();
            this.update();
        }
    };
    /**
     * init 方法会在build前自动调用
    */
    default_1.prototype.init = function () { };
    /**
     * sort方法会在 build前 或 update前 自动调用
    */
    default_1.prototype.sort = function () {
        this.DATA = this.props.data;
    };
    /**
     * 是否需要resize
    */
    default_1.prototype._needResize = function () {
        var _a;
        var root = document.getElementById(this.id);
        if (root == null) {
            return false;
        }
        var _b = __read([Math.floor(root.offsetWidth), Math.floor(root.offsetHeight)], 2), w = _b[0], h = _b[1];
        if (this.WIDTH != w || this.HEIGHT != h) {
            _a = __read([w, h], 2), this.WIDTH = _a[0], this.HEIGHT = _a[1];
            var def = _.extend({}, this.__Grid, this.props.grid);
            this.LAYOUT = {
                x: def.left,
                y: def.top,
                w: this.WIDTH - def.left - def.right,
                h: this.HEIGHT - def.top - def.bottom,
            };
            this.SVG.attr('width', this.WIDTH)
                .attr('height', this.HEIGHT);
            return true;
        }
        return false;
    };
    /**
     * 是否需要update
    */
    default_1.prototype._needUpdate = function () {
        if (this._last_data == this.props.data) {
            return false;
        }
        this._last_data = this.props.data;
        return true;
    };
    default_1.prototype.__initSVG = function () {
        var _this = this;
        this._WAIT = false;
        var root = document.getElementById(this.id);
        this._last_data = this.props.data;
        this.init();
        this.SVG = d3.select(root).append('svg')
            .attr('width', this.WIDTH)
            .attr('height', this.HEIGHT)
            .style('display', 'block');
        this.loaded = true;
        this.sort();
        this.build();
        window.setTimeout(function () {
            if (_this._needResize()) {
                _this.resize();
            }
        }, 20);
        window.addEventListener('resize', function (ev) {
            if (_this._needResize()) {
                _this.resize();
            }
        });
    };
    default_1.prototype.componentDidMount = function () {
        var _a;
        var root = document.getElementById(this.id);
        var def = _.extend({}, this.__Grid, this.props.grid);
        _a = __read([Math.floor(root.offsetWidth), Math.floor(root.offsetHeight)], 2), this.WIDTH = _a[0], this.HEIGHT = _a[1];
        if (this.props.maxCount)
            this.MAXCOUNT = this.props.maxCount;
        this.LAYOUT = {
            x: def.left,
            y: def.top,
            w: this.WIDTH - def.left - def.right,
            h: this.HEIGHT - def.top - def.bottom,
        };
        if (_.has(this.props, 'duration')) {
            this.DURATION = this.props.duration;
        }
        if (_.has(this.props, 'format')) {
            this.FormatValue = common_1.Format.buildFormat(this.props.format);
        }
        if (_.has(this.props, 'labelFormat')) {
            this.FormatLabel = this.props.labelFormat;
        }
        this._WAIT = this.props.data === null;
        if (this._WAIT) {
            return;
        }
        this.__initSVG();
    };
    default_1.prototype.componentDidUpdate = function () {
        if (this._WAIT) {
            if (this.props.data === null) {
                return;
            }
            this.__initSVG();
            return;
        }
        if (this._needUpdate()) {
            // this.DATA = this.props.data;
            this.sort();
            this.update();
        }
    };
    /**
     * 不要实现render方法
    */
    default_1.prototype.render = function () {
        return React.createElement("div", { id: this.id, className: "vap-chart " + this.__ClassName }, this.props.children);
    };
    return default_1;
}(React.PureComponent));
exports.default = default_1;
