'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 _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 _styleSvgJs = require('../styleSvg.js');

var _styleSvgJs2 = _interopRequireDefault(_styleSvgJs);

var Radar = require('paths-js/radar');

function identity(key) {
    return function (x) {
        return x[key];
    };
};
function accessKeys(keys) {
    var a = {};
    for (var i in keys) {
        var key = keys[i];
        a[key] = identity(key);
    }
    return a;
}

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

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

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

    _createClass(RadarChart, [{
        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 x = options.chartWidth / 2;
            var y = options.chartHeight / 2;
            var radius = Math.min(x, y);

            var center = this.props.center || [x, y];

            var keys = Object.keys(this.props.data[0]);
            var chart = Radar({
                center: this.props.center || [x, y],
                r: this.props.options.r || radius,
                data: this.props.data,
                accessor: this.props.accessor || accessKeys(keys),
                max: this.props.options.max
            });
            var self = this;
            var colors = (0, _styleSvgJs2['default'])({}, self.props.options);
            var curves = chart.curves.map(function (c, i) {
                return _react2['default'].createElement('path', { key: i, d: c.polygon.path.print(), fill: colors.fill, fillOpacity: colors.fillOpacity });
            });

            //var rings = chart.rings.map(function(r, i) {
            //    return (<path key={ i } d={ r.path.print() } stroke={ self.props.stroke } />)
            //});
            var length = chart.rings.length;

            var rings = chart.rings.map(function (r, i) {
                if (i !== length - 1) {
                    return _react2['default'].createElement('path', { key: "rings" + i, d: r.path.print(), stroke: colors.stroke, strokeOpacity: colors.strokeOpacity });
                }
            });
            //
            var textStyle = (0, _fontAdapterJs2['default'])(options.label);

            var labels = chart.rings[length - 1].path.points().map(function (p, i) {
                return _react2['default'].createElement(
                    'g',
                    { key: "label" + i },
                    _react2['default'].createElement('line', { x1: p[0], y1: p[1], x2: center[0], y2: center[1], stroke: colors.stroke, strokeOpacity: colors.strokeOpacity }),
                    _react2['default'].createElement(
                        'text',
                        { style: textStyle, textAnchor: 'middle', transform: "translate(" + Math.floor(p[0]) + "," + Math.floor(p[1]) + ")" },
                        keys[i]
                    )
                );
            });
            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 + ")" },
                    labels,
                    _react2['default'].createElement(
                        'g',
                        { fill: 'none', stroke: 'none' },
                        rings,
                        _react2['default'].createElement(
                            'g',
                            { opacity: '0.6' },
                            this.state.finished ? curves : null
                        )
                    )
                )
            );
        }
    }]);

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

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