"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 __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 common_1 = require("./common");
/**
 * 定义 echart 的规范
*/
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.id = '';
        _this.WIDTH = 0;
        _this.HEIGHT = 0;
        _this.DATA = {};
        _this.chart = null;
        return _this;
    }
    //是否需要resize
    default_1.prototype._needResize = function () {
        var _a;
        var root = document.getElementById(this.id);
        var _b = __read([root.offsetWidth, 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];
            return true;
        }
        return false;
    };
    /**
     * resize方法会在宽高发生变化的情况下自动调用
    */
    default_1.prototype.resize = function () {
        this.chart.resize();
    };
    /**
     * init 方法会在build前自动调用
    */
    default_1.prototype.init = function () {
        var _this = this;
        if (JSON.stringify(this.DATA) != "{}") {
            this.sort();
            this.chart.setOption(this.DATA);
            if (this.props.onClick) {
                if (this.props.onClickType) {
                    this.chart.on("click", this.props.onClickType, function (d) {
                        _this.props.onClick(d);
                    });
                }
                else {
                    this.chart.on("click", function (d) {
                        _this.props.onClick(d);
                    });
                }
            }
        }
    };
    /**
     * sort方法会在 build前 或 update前 自动调用
    */
    default_1.prototype.sort = function () {
        this.DATA = this.props.data;
    };
    default_1.prototype.componentWillMount = function () {
        this.id = common_1.DefUtil.genId();
    };
    default_1.prototype.componentDidMount = function () {
        var _a;
        var _this = this;
        var root = document.getElementById(this.id);
        _a = __read([root.offsetWidth, root.offsetHeight], 2), this.WIDTH = _a[0], this.HEIGHT = _a[1];
        this.chart = echarts.init(document.getElementById(this.id));
        this.sort();
        this.init();
        window.addEventListener('resize', function () {
            if (_this._needResize()) {
                _this.resize();
            }
        });
    };
    default_1.prototype.componentDidUpdate = function () {
        var _this = this;
        this.DATA = this.props.data;
        if (JSON.stringify(this.DATA) != "{}") {
            this.sort();
            this.chart.setOption(this.DATA);
            this.chart.resize();
            if (this.props.onClick) {
                this.chart.on("click", function (d) {
                    _this.props.onClick(d);
                });
            }
        }
        else if (this.chart["_chartsViews"].length > 0) {
            this.chart.clear();
        }
    };
    default_1.prototype.render = function () {
        var param = {
            className: '_vce_chart',
        };
        if (this.props.className) {
            param.className = param.className + ' ' + this.props.className;
        }
        return React.createElement("div", __assign({ id: this.id }, param));
    };
    return default_1;
}(React.PureComponent));
exports.default = default_1;
