'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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

var _styleSvg2 = _interopRequireDefault(_styleSvg);

var Tree = require('paths-js/tree');

function children(x) {
    if (x.collapsed) {
        return [];
    } else {
        return x.children || [];
    }
}

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

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

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

    _createClass(TreeChart, [{
        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 that = this;

            var tree = Tree({
                data: this.props.data,
                children: children,
                width: options.chartWidth,
                height: options.chartHeight
            });
            var colors = (0, _styleSvg2['default'])({}, options);
            var curves = _lodash2['default'].map(tree.curves, function (c, i) {
                return _react2['default'].createElement('path', { key: "curves_" + i, d: c.connector.path.print(), fill: 'none', stroke: colors.stroke, strokeOpacity: colors.strokeOpacity });
            });

            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 r = options.r || 5;
            var nodes = _lodash2['default'].map(tree.nodes, function (n, index) {
                var position = "translate(" + n.point[0] + "," + n.point[1] + ")";

                function toggle() {
                    n.item.collapsed = !n.item.collapsed;
                    that.forceUpdate();
                };

                if (children(n.item).length > 0) {
                    var text = _react2['default'].createElement(
                        'text',
                        { style: textStyle, transform: 'translate(-10,0)', textAnchor: 'end' },
                        n.item.name
                    );
                } else {
                    var text = _react2['default'].createElement(
                        'text',
                        { style: textStyle, transform: 'translate(10,0)', textAnchor: 'start' },
                        n.item.name
                    );
                }

                return _react2['default'].createElement(
                    'g',
                    { key: "tree_" + index, transform: position },
                    _react2['default'].createElement('circle', _extends({ style: fillOpacityStyle }, colors, { r: r, cx: '0', cy: '0', onClick: toggle })),
                    text
                );
            });

            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 + ")" },
                    curves,
                    nodes
                )
            );
        }
    }]);

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

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