"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 Content_1 = require("./Content");
var ToolTip_1 = require("../../charts/common/ToolTip");
var d3 = require("d3");
var _ = require("lodash");
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.CurrentArea = '';
        return _this;
    }
    default_1.prototype.resize = function (group) {
        var _this = this;
        var path = d3.geoPath().projection(this.PROJECTION);
        group.selectAll('g._item path')
            .attr('d', path);
        group.selectAll('g._item text')
            .attr('x', function (d) {
            var point = _this.PROJECTION(_this.BUILDER.getPoint(d.properties.name));
            return point[0];
        })
            .attr('y', function (d) {
            var point = _this.PROJECTION(_this.BUILDER.getPoint(d.properties.name));
            return point[1];
        });
        group.selectAll('g._item circle')
            .attr('cx', function (d) { return _this.PROJECTION(_this.BUILDER.getPoint(d.properties.name))[0]; })
            .attr('cy', function (d) { return _this.PROJECTION(_this.BUILDER.getPoint(d.properties.name))[1] - 12; });
        group.selectAll('g._ext')
            .attr('style', function (d) {
            var tran = _this.PROJECTION(d.pos);
            return "transform:translate(" + tran[0] + "px," + tran[1] + "px)";
        });
    };
    default_1.prototype.setData = function (group, data) {
        var _this = this;
        var AREAS = group.selectAll('g._item')
            .data(data)
            .enter()
            .append('g')
            .attr('class', '_item')
            .attr('id', function (d) { return 'area-' + d.properties.id; });
        var path = d3.geoPath().projection(this.PROJECTION);
        group.selectAll('g._item_area_selected').classed('_item_area_selected', false).select('circle').remove();
        AREAS.append('path')
            .attr('d', path);
        AREAS.append('text')
            .attr('x', function (d) {
            var point = _this.PROJECTION(_this.BUILDER.getPoint(d.properties.name));
            return point[0];
        })
            .attr('y', function (d) {
            var point = _this.PROJECTION(_this.BUILDER.getPoint(d.properties.name));
            return point[1];
        })
            .text(function (d) { return _this.props.nameFormat ? _this.props.nameFormat(d.properties.name) : d.properties.name; });
        if (_.isFunction(this.props.onSelected)) {
            AREAS.on('click', function (d) {
                var selected = group.select('#area-' + d.properties.id).classed('_item_area_selected');
                if (selected) {
                    group.selectAll('g._item_area_selected').classed('_item_area_selected', false).select('circle').remove();
                    _this.CurrentArea = '';
                }
                else {
                    group.selectAll('g._item_area_selected').classed('_item_area_selected', false).select('circle').remove();
                    _this.CurrentArea = d.properties.name;
                    var areaContainer = group.select('#area-' + d.properties.id).classed('_item_area_selected', true);
                    areaContainer.append('circle')
                        .attr('class', '_area_select_point')
                        .attr('cx', function (d) { return _this.PROJECTION(_this.BUILDER.getPoint(d.properties.name))[0]; })
                        .attr('cy', function (d) { return _this.PROJECTION(_this.BUILDER.getPoint(d.properties.name))[1] - 12; })
                        .attr('r', 10)
                        .append('animate')
                        .attr('attributeName', 'r')
                        .attr('attributeType', 'xml')
                        .attr('form', 10)
                        .attr('to', 25)
                        .attr('begin', '0s')
                        .attr('dur', '1s')
                        .attr('repeatCount', 'indefinite');
                }
                _this.props.onSelected.call(null, _this.CurrentArea);
            });
        }
        if (_.isFunction(this.props.tip)) {
            AREAS.on('mouseover', function (d) { return ToolTip_1.default.show(_this.props.tip(d.properties.name)); })
                .on('mouseout', function () { return ToolTip_1.default.hide(); });
        }
        if (!_.isArray(this.props.extIcons) || this.props.extIcons.length == 0) {
            return;
        }
        var exts = group.selectAll('g._ext')
            .data(this.props.extIcons)
            .enter()
            .append('g')
            .attr('class', function (d, i) { return "_ext _ext_" + i + " " + (d.className ? d.className : ''); })
            .attr('style', function (d) {
            var tran = _this.PROJECTION(d.pos);
            return "transform:translate(" + tran[0] + "px," + tran[1] + "px)";
        })
            .attr('id', function (d) { return "area-" + d.name; });
        exts.append('path')
            .attr('d', function (d) { return d.icon; });
        exts.append('text')
            .text(function (d) { return _this.props.nameFormat ? _this.props.nameFormat(d.name) : d.name; })
            .attr('x', 12)
            .attr('y', 4);
        if (_.isFunction(this.props.tip)) {
            exts.on('mouseover', function (d) { return ToolTip_1.default.show(_this.props.tip(d.name)); })
                .on('mouseout', function () { return ToolTip_1.default.hide(); });
        }
        if (_.isFunction(this.props.onSelected)) {
            exts.on('click', function (d) {
                var selected = group.select('#area-' + d.name).classed('_item_area_selected');
                if (selected) {
                    group.selectAll('g._item_area_selected').classed('_item_area_selected', false).select('circle').remove();
                    _this.CurrentArea = '';
                }
                else {
                    group.selectAll('g._item_area_selected').classed('_item_area_selected', false).select('circle').remove();
                    _this.CurrentArea = d.name;
                    var areaContainer = group.select('#area-' + d.name).classed('_item_area_selected', true);
                    areaContainer.append('circle')
                        .attr('class', '_area_select_point')
                        .attr('cx', 0)
                        .attr('cy', 0)
                        .attr('r', 10)
                        .append('animate')
                        .attr('attributeName', 'r')
                        .attr('attributeType', 'xml')
                        .attr('form', 10)
                        .attr('to', 25)
                        .attr('begin', '0s')
                        .attr('dur', '1s')
                        .attr('repeatCount', 'indefinite');
                }
                _this.props.onSelected.call(null, _this.CurrentArea);
            });
        }
    };
    return default_1;
}(Content_1.default));
exports.default = default_1;
