'use strict';

Object.defineProperty(exports, '__esModule', {
    value: true
});

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _palleteColorsJs = require('../pallete/Colors.js');

var _palleteColorsJs2 = _interopRequireDefault(_palleteColorsJs);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _componentOptionsJs = require('../component/Options.js');

var _componentOptionsJs2 = _interopRequireDefault(_componentOptionsJs);

var _fontAdapterJs = require('../fontAdapter.js');

var _fontAdapterJs2 = _interopRequireDefault(_fontAdapterJs);

var Bar = require('paths-js/bar');

var Axis = require('../component/Axis');

function cyclic(coll, i) {
    return coll[i % coll.length];
}
function identity(key) {
    return function (x) {
        return x[key];
    };
};
function color(key) {
    return function (x) {
        return x[key];
    };
};

var BarChart = (function (_React$Component) {
    _inherits(BarChart, _React$Component);

    function BarChart(props) {
        _classCallCheck(this, BarChart);

        _get(Object.getPrototypeOf(BarChart.prototype), 'constructor', this).call(this, props);
        this.state = { finished: true };
    }

    _createClass(BarChart, [{
        key: 'color',
        value: function color(i) {
            var color = this.props.options.color;
            if (!_lodash2['default'].isString(this.props.options.color)) color = color.color;
            var pallete = this.props.pallete || _palleteColorsJs2['default'].mix(color || '#9ac7f7');
            return _palleteColorsJs2['default'].string(cyclic(pallete, i));
        }
    }, {
        key: 'getMaxAndMin',
        value: function getMaxAndMin(values, scale) {
            var maxValue = 0;
            var minValue = 0;

            var max = _lodash2['default'].max(values);
            if (max > maxValue) maxValue = max;
            var min = _lodash2['default'].min(values);
            if (min < minValue) minValue = min;

            return {
                minValue: minValue,
                maxValue: maxValue,
                min: scale(minValue),
                max: scale(maxValue)
            };
        }

        //componentWillReceiveProps(nextProps) {
        //    if (this.props.data !== nextProps.data) this.setState({ finished:false});
        //}
    }, {
        key: 'render',
        value: function render() {
            var noDataMsg = this.props.noDataMessage || "No data available";
            if (this.props.data === undefined) return _react2['default'].createElement(
                'span',
                null,
                noDataMsg
            );

            var options = new _componentOptionsJs2['default'](this.props);
            var accessor = this.props.accessor || identity(this.props.accessorKey);

            var chart = Bar({
                data: this.props.data,
                gutter: this.props.options.gutter || 10,
                width: options.chartWidth,
                height: options.chartHeight,
                accessor: accessor
            });

            var values = _lodash2['default'].map(chart.curves, function (curve) {
                return accessor(curve.item);
            });

            var chartArea = { x: { minValue: 0, maxValue: 200, min: 0, max: options.chartWidth }, y: this.getMaxAndMin(values, chart.scale) };

            var sec = options.animate.fillTransition || 0;
            var fillOpacityStyle = { fillOpacity: this.state.finished ? 1 : 0, transition: this.state.finished ? 'fill-opacity ' + sec + 's' : '' };

            var textStyle = (0, _fontAdapterJs2['default'])(options.axisX.label);

            var lines = chart.curves.map(function (c, i) {
                var color = this.color(i % 3);
                var stroke = _palleteColorsJs2['default'].darkenColor(color);
                return _react2['default'].createElement(
                    'g',
                    { key: "lines" + i },
                    _react2['default'].createElement('path', { d: c.line.path.print(), style: fillOpacityStyle, stroke: stroke, fill: color }),
                    options.axisX.showLabels ? _react2['default'].createElement(
                        'text',
                        { style: textStyle, transform: "translate(" + c.line.centroid[0] + "," + (chartArea.y.min + 25) + ")rotate(45)", textAnchor: 'middle' },
                        c.item.name
                    ) : null
                );
            }, this);

            return _react2['default'].createElement(
                'svg',
                { ref: 'vivus', width: options.width, height: options.height },
                _react2['default'].createElement(
                    'g',
                    { transform: "translate(" + options.margin.left + "," + options.margin.top + ")" },
                    _react2['default'].createElement(Axis, { scale: chart.scale, options: options.axisY, chartArea: chartArea }),
                    lines
                )
            );
        }
    }]);

    return BarChart;
})(_react2['default'].Component);

exports['default'] = BarChart;
;
module.exports = exports['default'];