"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 d3 = require("d3");
var D3Chart_1 = require("../D3Chart");
var common_1 = require("../common");
var _ = require("lodash");
var BarHeight = 12;
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_bar_top';
        _this.__Grid = {
            left: 12,
            right: 12,
            top: 0,
            bottom: 0,
        };
        _this.itemHeight = 40;
        _this.widthScale = null;
        return _this;
    }
    default_1.prototype.init = function () {
        if (_.has(this.props, "height") && this.props.height != 0) {
            this.HEIGHT = this.HEIGHT > this.props.height ? this.HEIGHT : this.props.height;
        }
    };
    default_1.prototype.sort = function () {
        var _this = this;
        this.DATA = _.orderBy(this.props.data, function (d) { return parseFloat(d[_this.props.valueField]); }, 'desc');
        if (this.DATA.length > this.MAXCOUNT && !_.has(this.props, "isLimite"))
            this.DATA.length = this.MAXCOUNT;
        var _a = __read(this.extent(this.DATA, function (d) { return d[_this.props.valueField]; }), 2), min = _a[0], max = _a[1];
        this.itemHeight = this.HEIGHT / this.DATA.length;
        this.widthScale = d3.scaleLinear().domain([min, max]).range([0.4 * this.LAYOUT.w, 0.95 * this.LAYOUT.w - (max + '').length * 14]);
    };
    default_1.prototype.update = function () {
        var _this = this;
        var group = this.SVG.selectAll('g._item')
            .data(this.DATA, function (d) { return d[_this.props.labelField]; });
        var enter = group.enter().append('g')
            .attr('transform', "translate(0," + (this.HEIGHT + 100) + ")");
        enter.merge(group)
            .attr('class', function (d, i) { return _this.itemClass(d, i); })
            .transition()
            .duration(this.DURATION)
            .attr('transform', function (d, i) { return "translate(0," + _this.itemHeight * i + ")"; });
        enter.append('text')
            .attr('class', '_index')
            .attr('x', this.LAYOUT.x)
            .merge(group.select('text._index'))
            .attr('y', this.itemHeight / 2 - 6)
            .text(function (d, i) { return (i + 1) + '.'; });
        enter.append('text')
            .attr('class', '_label')
            .text(function (d) { return _this.FormatLabel(d[_this.props.labelField]); })
            .attr('x', 24)
            .merge(group.select('text._label'))
            .attr('y', this.itemHeight / 2 - 8);
        enter.append('text')
            .attr('class', '_value')
            .merge(group.select('text._value'))
            .attr('x', 2 + this.LAYOUT.w)
            .text(function (d) { return d[_this.props.valueField]; })
            .attr('y', this.itemHeight / 2 + 11);
        enter.append('rect')
            .attr('class', '_content')
            .attr('width', 0)
            .attr('height', BarHeight)
            .attr('x', 24)
            .merge(group.select('rect._content'))
            .attr('y', this.itemHeight / 2)
            .transition()
            .duration(this.DURATION)
            .attr('width', function (d, i) { return _this.widthScale(d[_this.props.valueField]); });
        enter.on('mouseover', function (d) {
            if (_.has(_this.props, 'tip')) {
                common_1.ToolTip.show(_this.props.tip(d));
            }
            else {
                common_1.ToolTip.show("<h2>" + _this.FormatLabel(d[_this.props.labelField]) + " : <small class=\"vapfont\">" + d[_this.props.valueField] + "</small></h2>");
            }
        }).on('mouseout', function (d) {
            common_1.ToolTip.hide();
        });
        if (this.props.onClick) {
            enter.on('click', function (d) { return _this.props.onClick(d); });
        }
        group.exit()
            .transition()
            .duration(this.DURATION)
            .attr('transform', "translate(0,-150)")
            .remove();
    };
    return default_1;
}(D3Chart_1.default));
exports.default = default_1;
