"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var utils_1 = require("../../utils");
var marginTop = 12;
var height = 40;
var checkOn = utils_1.IconUtil.iconUnicode('symbol-check-on');
var checkOff = utils_1.IconUtil.iconUnicode('symbol-check-off');
var default_1 = /** @class */ (function () {
    function default_1(group, pos, onSelect, width, duration) {
        if (pos === void 0) { pos = 1; }
        if (onSelect === void 0) { onSelect = null; }
        if (width === void 0) { width = 200; }
        if (duration === void 0) { duration = 1000; }
        this.width = 200;
        this.pos = 1;
        this.duration = 1000;
        this.onSelect = null;
        this.data = [];
        this.SelectKeys = null;
        this.AllKeyMap = new Map();
        this.group = group;
        this.width = width;
        this.pos = pos;
        this.duration = duration;
        this.onSelect = onSelect;
    }
    default_1.prototype.getIndex = function (key) {
        return this.AllKeyMap.has(key) ? this.AllKeyMap.get(key) : 'x';
    };
    //更新Legend
    default_1.prototype.update = function (data) {
        var _this = this;
        var sum = _.sumBy(data, 'value');
        sum = sum === 0 ? 0.1 : sum;
        if (this.SelectKeys === null) {
            this.AllKeyMap.clear();
            this.SelectKeys = data.map(function (item, i) {
                _this.AllKeyMap.set(item.key, i);
                return item.key;
            });
            data.map(function (item) {
                item.index = _this.AllKeyMap.has(item.key) ? _this.AllKeyMap.get(item.key) : 'x';
            });
        }
        var processWidth = this.width - 72;
        var legends = _.orderBy(data, 'value', 'desc');
        var group = this.group.selectAll('g._legend').data(legends, function (d) { return d['key']; });
        var groupAdd = group.enter()
            .append('g')
            .attr('class', function (d) { return "_legend _legend_" + d.index; })
            .classed('_legend_hover', this.onSelect !== undefined);
        switch (this.pos) {
            case 1:
                groupAdd.attr("transform", function (d, i) { return "translate(0,-200)"; });
                break;
            case 2:
                groupAdd.attr("transform", function (d, i) { return "translate(" + (-_this.width - 200) + "," + i * (height + marginTop) + ")"; });
                break;
            case 3:
                groupAdd.attr("transform", function (d, i) { return "translate(0," + (data.length * (height + marginTop) + 200) + ")"; });
                break;
            case 4:
                groupAdd.attr("transform", function (d, i) { return "translate(" + (_this.width + 250) + "," + i * (height + marginTop) + ")"; });
                break;
        }
        groupAdd.transition().duration(this.duration)
            .attr("transform", function (d, i) { return "translate(0," + (height + marginTop) * i + ")"; });
        group.transition().duration(this.duration)
            .attr("transform", function (d, i) { return "translate(0," + (height + marginTop) * i + ")"; });
        groupAdd.append('rect')
            .attr('class', '_legend_round')
            .attr('x', 0)
            .attr('y', 0)
            .attr('width', this.width)
            .attr('height', height);
        groupAdd.append('rect')
            .attr('class', '_legend_process_bkg')
            .attr('x', 12)
            .attr('y', 28)
            .attr('width', processWidth)
            .attr('height', 8);
        groupAdd.append('text')
            .attr('class', '_legend_status')
            .attr('x', 12)
            .attr('y', 18)
            .html(checkOn);
        groupAdd.append('text')
            .attr('class', '_legend_title')
            .attr('x', 30)
            .attr('y', 18)
            .text(function (d) { return d.label; });
        var process = groupAdd.append('rect')
            .attr('class', '_legend_process_val')
            .attr('x', 12)
            .attr('y', 28)
            .attr('width', 0)
            .attr('height', 8);
        groupAdd.append('text')
            .attr('class', '_legend_sum')
            .attr('x', this.width - 12)
            .attr('y', 18)
            .text(function (d) { return d.value; });
        groupAdd.append('text')
            .attr('class', '_legend_percent')
            .attr('x', this.width - 12)
            .attr('y', 36)
            .text(function (d) { return utils_1.Format.formatDecimal(d.value * 100 / sum) + '%'; });
        if (this.onSelect) {
            groupAdd.on('click', function (item) {
                var index = _.indexOf(_this.SelectKeys, item.key);
                if (index >= 0) {
                    _.remove(_this.SelectKeys, function (i) { return i == item.key; });
                }
                else {
                    _this.SelectKeys.push(item.key);
                }
                _this.SelectKeys = _.sortBy(_this.SelectKeys, function (a, b) { return _this.AllKeyMap.get(a) - _this.AllKeyMap.get(b); });
                var legend = _this.group.selectAll('g._legend');
                legend.classed('_legend_discard', function (d) { return _this.SelectKeys.indexOf(d['key']) < 0; });
                legend.select('text._legend_status').html(function (d) { return _this.SelectKeys.indexOf(d['key']) < 0 ? checkOff : checkOn; });
                _this.onSelect(_.concat([], _this.SelectKeys));
            });
        }
        process.transition().duration(this.duration).attr('width', function (d) { return d.value / sum * processWidth; });
        group.select('rect._legend_process_val').transition().duration(this.duration).attr('width', function (d) { return d.value / sum * processWidth; });
        group.select('text._legend_sum').text(function (d) { return d.value; });
        group.select('text._legend_percent').text(function (d) { return utils_1.Format.formatDecimal(d.value * 100 / sum) + '%'; });
        group.exit().remove();
    };
    return default_1;
}());
exports.default = default_1;
