'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 _animateJs = require('../animate.js');

var _animateJs2 = _interopRequireDefault(_animateJs);

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 Pie = require('paths-js/pie');

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 PieChart = (function (_React$Component) {
    _inherits(PieChart, _React$Component);

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

        _get(Object.getPrototypeOf(PieChart.prototype), 'constructor', this).call(this, props);
        this.animateState = _animateJs2['default'].Mixin.animateState;
        this.state = {
            expanded: this.defaultRange,
            finished: true
        };
    }

    _createClass(PieChart, [{
        key: 'translate',
        value: function translate(p) {
            return "translate(" + p[0] + "," + p[1] + ")";
        }
    }, {
        key: 'move',
        value: function move(point, perc) {
            return this.translate([point[0] * perc / 3, point[1] * perc / 3]);
        }

        //
        //grad(i) { return "grad-" + i }
        //
        //fill(i) { return "url(#grad-" + i  +")" }

    }, {
        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: 'lighten',
        value: function lighten(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(_palleteColorsJs2['default'].lighten(cyclic(pallete, i)));
        }
    }, {
        key: 'expand',
        value: function expand(i) {
            var self = this;

            return function () {
                var target = self.defaultRange;
                target[i] = 1;
                self.animateState({ expanded: target });
                //self.setState({ expanded: target });
            };
        }
    }, {
        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 chart = Pie({
                center: this.props.options.center || [0, 0],
                r: this.props.options.r || radius / 2,
                R: this.props.options.R || radius,
                data: this.props.data,
                accessor: this.props.accessor || identity(this.props.accessorKey)
            });

            var self = this;
            var coefficients = this.state.expanded;
            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.label);

            var slices = chart.curves.map(function (c, i) {
                var fill = self.color(i);
                var stroke = _palleteColorsJs2['default'].darkenColor(fill);
                return _react2['default'].createElement(
                    'g',
                    { key: i, transform: self.move(c.sector.centroid, coefficients[i]) },
                    _react2['default'].createElement('path', { onClick: self.expand(i), style: fillOpacityStyle, d: c.sector.path.print(), stroke: stroke, fill: fill }),
                    _react2['default'].createElement(
                        'text',
                        { style: textStyle, textAnchor: 'middle', transform: self.translate(c.sector.centroid) },
                        c.item.name
                    )
                );
            });
            var selected = _lodash2['default'].find(this.props.data, function (c, i) {
                return coefficients[i] === 1;
            });

            var legendClassName = "legend " + options.legendPosition;

            var table = selected ? _react2['default'].createElement(
                'div',
                { className: legendClassName },
                _react2['default'].createElement(
                    'h4',
                    null,
                    selected.name
                ),
                _react2['default'].createElement(
                    'p',
                    null,
                    _react2['default'].createElement(
                        'span',
                        { className: 'label label-info' },
                        selected.population
                    )
                )
            ) : null;

            return _react2['default'].createElement(
                'div',
                { className: 'pie' },
                _react2['default'].createElement(
                    'svg',
                    { ref: 'vivus', width: options.width, height: options.height },
                    _react2['default'].createElement(
                        'g',
                        { transform: "translate(" + (options.margin.left + x) + "," + (options.margin.top + y) + ")" },
                        slices
                    )
                ),
                table
            );
        }
    }, {
        key: 'defaultRange',
        get: function get() {
            return _lodash2['default'].map(Array(this.props.data && this.props.data.length), function () {
                return 0;
            });
        }
    }]);

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

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