"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 Content_1 = require("./Content");
var d3 = require("d3");
var _ = require("lodash");
var utils_1 = require("../../utils");
var LegendHeight = 52;
var ItemHeight = 40;
var LegendWidth = 248;
var ProcessWidth = LegendWidth - 72;
var PADDING = 12;
var checkOn = utils_1.IconUtil.iconUnicode('symbol-check-on');
var checkOff = utils_1.IconUtil.iconUnicode('symbol-check-off');
/**
 * 地图增色组件
*/
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.group = null;
        _this.checked = [];
        _this.data = [];
        _this.legend = false;
        return _this;
    }
    default_1.prototype.resize = function () {
        if (this.group) {
            var _a = __read(this.BUILDER.getCoord(), 2), width = _a[0], height = _a[1];
            //@ts-ignore
            var pos = this.props.colors.pos || 'bottom-left';
            //@ts-ignore
            var _b = __read([this.props.colors.labelFields, this.props.colors.valueFields], 2), labelFields = _b[0], valueFields = _b[1];
            // var legend
            var start_1 = PADDING;
            if (pos == 'bottom-left' || pos == 'top-left') {
                if (pos == 'bottom-left') {
                    start_1 = height - PADDING - LegendHeight * labelFields.length + (LegendHeight - ItemHeight);
                }
                this.group.attr('transform', function (d, i) { return "translate(" + PADDING + "," + (start_1 + i * LegendHeight) + ")"; });
            }
            else {
                var x_1 = width - LegendWidth - PADDING;
                var start_2 = PADDING;
                if (pos == 'bottom-right') {
                    start_2 = height - PADDING - LegendHeight * labelFields.length + (LegendHeight - ItemHeight);
                }
                this.group.attr('transform', function (d, i) { return "translate(" + x_1 + "," + (start_2 + i * LegendHeight) + ")"; });
            }
        }
    };
    default_1.prototype.updateLegend = function () {
        var _this = this;
        // // this.BUILDER
        // let legend = group.append('g').attr('class', '_legends');
        // //@ts-ignore
        // let pos = this.props.colors.pos || 'bottom-left';
        // var [width, height] = this.BUILDER.getCoord();
        //@ts-ignore
        var _a = __read([this.props.colors.labelFields, this.props.colors.valueFields], 2), labelFields = _a[0], valueFields = _a[1];
        // //@ts-ignore
        var sum = valueFields.map(function (field) {
            // @ts-ignore
            return _.sumBy(_this.data, field);
        });
        var all = _.sum(sum);
        all = all == 0 ? 0.01 : all;
        this.group.select('text._legend_sum').text(function (d, i) {
            return sum[i];
        });
        this.group.select('text._legend_percent').text(function (d, i) {
            return utils_1.Format.formatDecimal(sum[i] * 100 / all) + '%';
        });
        this.group.select('rect._legend_process_val')
            .attr('width', 0)
            .transition()
            .duration(2000)
            .attr('width', function (d, i) {
            return ProcessWidth * sum[i] / all;
        });
    };
    default_1.prototype.buildLegend = function (group) {
        var _this = this;
        this.legend = true;
        var legend = group.append('g').attr('class', '_legends');
        //@ts-ignore
        var pos = this.props.colors.pos || 'bottom-left';
        var _a = __read(this.BUILDER.getCoord(), 2), width = _a[0], height = _a[1];
        //@ts-ignore
        var _b = __read([this.props.colors.labelFields, this.props.colors.valueFields], 2), labelFields = _b[0], valueFields = _b[1];
        this.group = legend.selectAll('g._legend').data(labelFields).enter().append('g').attr('class', '_legend _legend_hover');
        // var legend
        if (pos == 'bottom-left' || pos == 'top-left') {
            var start_3 = PADDING;
            if (pos == 'bottom-left') {
                start_3 = height - PADDING - LegendHeight * labelFields.length + (LegendHeight - ItemHeight);
            }
            this.group.attr('transform', function (d, i) { return "translate(" + PADDING + "," + (start_3 + i * LegendHeight) + ")"; });
        }
        else {
            var x_2 = width - LegendWidth - PADDING;
            var start_4 = PADDING;
            if (pos == 'bottom-right') {
                start_4 = height - PADDING - LegendHeight * labelFields.length + (LegendHeight - ItemHeight);
            }
            this.group.attr('transform', function (d, i) { return "translate(" + x_2 + "," + (start_4 + i * LegendHeight) + ")"; });
        }
        this.group.append('rect')
            .attr('class', '_legend_round')
            .attr('x', 0)
            .attr('y', 0)
            .attr('width', LegendWidth)
            .attr('height', ItemHeight);
        this.group.append('rect')
            .attr('class', '_legend_process_bkg')
            .attr('x', 12)
            .attr('y', 28)
            .attr('width', ProcessWidth)
            .attr('height', 8);
        this.group.append('rect')
            .attr('class', '_legend_process_val')
            .attr('x', 12)
            .attr('y', 28)
            .attr('width', 0)
            .attr('height', 8);
        this.group.append('text')
            .attr('class', '_legend_status')
            .attr('x', 12)
            .attr('y', 20)
            .html(checkOn);
        this.group.append('text')
            .attr('class', '_legend_title')
            .attr('x', 30)
            .attr('y', 18)
            .text(function (d) { return d; });
        this.group.append('text')
            .attr('class', '_legend_sum')
            .attr('x', LegendWidth - 12)
            .attr('y', 18)
            .text('0');
        this.group.append('text')
            .attr('class', '_legend_percent')
            .attr('x', LegendWidth - 12)
            .attr('y', 36)
            .text('0%');
        this.updateLegend();
        this.group.on('click', function (d, i) {
            var index = _.indexOf(_this.checked, valueFields[i]);
            if (index >= 0) {
                _.remove(_this.checked, function (key) { return key == valueFields[i]; });
            }
            else {
                _this.checked.push(valueFields[i]);
            }
            _this.group.classed('_discard', function (d, i) {
                return _.indexOf(_this.checked, valueFields[i]) < 0;
            });
            _this.group.select('text._legend_status').html(function (d, i) {
                return _.indexOf(_this.checked, valueFields[i]) < 0 ? checkOff : checkOn;
            });
            _this.data.map(function (item) {
                var sum = 0;
                _this.checked.map(function (field) { return sum += item[field]; });
                item.__sum = sum;
            });
            _this.changeColor(group, '__sum');
        });
    };
    default_1.prototype.changeColor = function (group, label) {
        var _this = this;
        var extent = d3.extent(this.data, function (item) { return item[label]; });
        extent[0] = extent[0] / 2;
        extent[1] = extent[1] == 0 ? 0.01 : extent[1];
        // @ts-ignore
        var scaleClass = d3.scaleQuantile().domain(extent).range(['_vc_map_c0', '_vc_map_c1', '_vc_map_c2', '_vc_map_c3', '_vc_map_c4']);
        group.selectAll('g._item path').attr('class', function (d) {
            var tmp = _.find(_this.data, { name: d.properties.name });
            if (tmp) {
                var c = scaleClass(tmp[label]);
                return c;
            }
            return '';
        });
    };
    default_1.prototype.setData = function (group, data) {
        var _this = this;
        var areaGroup = group.select('g._group_area');
        if (_.isArray(data)) {
            this.data = data;
            this.changeColor(areaGroup, 'value');
        }
        else {
            this.data = data.data;
            this.checked = data.valueFields;
            this.data.map(function (item) {
                var sum = 0;
                _this.checked.map(function (field) { return sum += item[field]; });
                item.__sum = sum;
            });
            if (!this.legend) {
                this.buildLegend(group);
            }
            else {
                this.updateLegend();
            }
            this.changeColor(group, '__sum');
        }
    };
    return default_1;
}(Content_1.default));
exports.default = default_1;
