"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 _ = require("lodash");
var d3 = require("d3");
var D3Chart_1 = require("../D3Chart");
var common_1 = require("../common");
var cloud_1 = require("./cloud");
var Ball_1 = require("./Ball");
var level = function (v) { return v * 2 + _.sample([-1, 0, 1]); };
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.__ClassName = '_vc_wordcloud';
        _this.sizeScale = null;
        _this.levelScale = null;
        _this.textGroup = null;
        return _this;
    }
    default_1.prototype.sort = function () {
        var _this = this;
        this.DATA = this.props.data;
        var _a = __read(d3.extent(this.DATA, function (d) { return d[_this.props.valueField]; }), 2), min = _a[0], max = _a[1];
        this.sizeScale = d3.scaleQuantize().domain([parseInt(min), parseInt(max)]).range([14, 18, 24]);
        this.levelScale = d3.scaleQuantize().domain([parseInt(min), parseInt(max)]).range([1, 2, 3, 4, 5]);
    };
    default_1.prototype.words = function (words) {
        var _this = this;
        this.textGroup.selectAll("text").data([]).exit().remove();
        var txts = this.textGroup
            .selectAll("text")
            .data(words)
            .enter()
            .append("text");
        txts.attr('class', function (d) { return "_item _item_" + level(_this.levelScale(d[_this.props.valueField])) + " " + (_.has(_this.props, 'onClick') ? '_item_hover' : ''); })
            .style("font-size", function (d) { return _this.sizeScale(d[_this.props.valueField]); })
            // .style("font-family", "Impact")
            // .attr("text-anchor", "middle")
            // .style("fill", function (d, i) {
            //     if (COLOR[d.index] == null) {
            //         return THEME.lines[5];
            //     }
            //     return THEME.lines[d.index];
            // })
            // .attr("text-anchor", "middle")
            .attr("transform", function (d) { return "translate(" + d.x + ", " + d.y + ") rotate(0)"; })
            .text(function (d) { return d[_this.props.labelField]; });
        if (_.has(this.props, 'tip')) {
            txts.on('mouseover', function (d, i) {
                common_1.ToolTip.show(_this.props.tip(d, i));
                return;
            }).on('mouseout', function () {
                common_1.ToolTip.hide();
            });
        }
        if (_.has(this.props, 'onClick')) {
            txts.on('click', function (d) { return _this.props.onClick(d); });
        }
    };
    default_1.prototype.build = function () {
        var _this = this;
        this.textGroup = this.SVG.append("g")
            .attr("transform", "translate(" + this.WIDTH / 2 + "," + this.HEIGHT / 2 + ")");
        var layout = cloud_1.default()
            .size([this.WIDTH * 0.9, this.HEIGHT * 0.9])
            .words(this.DATA)
            .padding(5)
            .text(function (d) { return d[_this.props.labelField]; })
            .rotate(function () { return ~~(Math.random() * 2) * 90; })
            .fontSize(function (d) { return _this.sizeScale(d[_this.props.valueField]); })
            .on("end", function (words) { return _this.words(words); });
        layout.start();
        layout.stop();
    };
    default_1.prototype.update = function () {
        var _this = this;
        var layout = cloud_1.default()
            .size([this.WIDTH * 0.9, this.HEIGHT * 0.9])
            .words(this.DATA)
            .padding(5)
            .text(function (d) { return d[_this.props.labelField]; })
            .rotate(function () { return ~~(Math.random() * 2) * 90; })
            .fontSize(function (d) { return _this.sizeScale(d[_this.props.valueField]); })
            .on("end", function (words) { return _this.words(words); });
        layout.start();
        layout.stop();
    };
    default_1.Ball = Ball_1.default;
    return default_1;
}(D3Chart_1.default));
exports.default = default_1;
