'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 Stock = require('paths-js/stock');
var Axis = require('../component/Axis');
var Path = require('paths-js/path');

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

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

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

    _createClass(Scatterplot, [{
        key: 'getMaxAndMin',
        value: function getMaxAndMin(chart, key, scale) {
            var maxValue;
            var minValue;
            _lodash2['default'].each(chart.curves, function (serie) {
                var values = _lodash2['default'].map(serie.item, function (item) {
                    return item[key];
                });

                var max = _lodash2['default'].max(values);
                if (maxValue === undefined || max > maxValue) maxValue = max;
                var min = _lodash2['default'].min(values);
                if (minValue === undefined || min < minValue) minValue = min;
            });
            return {
                minValue: minValue,
                maxValue: maxValue,
                min: scale(minValue),
                max: scale(maxValue)
            };
        }
    }, {
        key: 'onEnter',
        value: function onEnter(index, event) {
            this.props.data[0][index].selected = true;
            this.setState({ data: this.props.data });
        }
    }, {
        key: 'onLeave',
        value: function onLeave(index, event) {
            this.props.data[0][index].selected = false;
            this.setState({ data: this.props.data });
        }
    }, {
        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 palette = this.props.palette || ["#3E90F0", "#7881C2", "#707B82"];
            var accessor = function accessor(key) {
                return function (x) {
                    return x[key];
                };
            };
            var chart = Stock({
                data: this.props.data,
                xaccessor: accessor(this.props.xKey),
                yaccessor: accessor(this.props.yKey),
                width: options.chartWidth,
                height: options.chartHeight,
                closed: false
            });

            var chartArea = {
                x: this.getMaxAndMin(chart, this.props.xKey, chart.xscale),
                y: this.getMaxAndMin(chart, this.props.yKey, chart.yscale),
                margin: options.margin
            };

            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 colors = (0, _styleSvg2['default'])({}, options);
            var points = _lodash2['default'].map(chart.curves, function (c, i) {
                return _lodash2['default'].map(c.line.path.points(), function (p, j) {
                    var item = c.item[j];
                    return _react2['default'].createElement(
                        'g',
                        { key: 'k' + j, transform: "translate(" + p[0] + "," + p[1] + ")" },
                        _react2['default'].createElement('circle', _extends({}, colors, { cx: 0, cy: 0, r: options.r || 5, style: fillOpacityStyle, onMouseEnter: this.onEnter.bind(this, j), onMouseLeave: this.onLeave.bind(this, j) })),
                        item.selected ? _react2['default'].createElement(
                            'text',
                            { style: textStyle, transform: 'translate(15, 5)', 'text-anchor': 'start' },
                            item.title
                        ) : null
                    );
                }, this);
            }, 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 + ")" },
                    points,
                    _react2['default'].createElement(Axis, { scale: chart.xscale, options: options.axisX, chartArea: chartArea }),
                    _react2['default'].createElement(Axis, { scale: chart.yscale, options: options.axisY, chartArea: chartArea })
                )
            );
        }
    }]);

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

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