'use strict';

exports.__esModule = 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 _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _chart = require('chart.js');

var _chart2 = _interopRequireDefault(_chart);

var _coreuiChartjs = require('@coreui/coreui-chartjs/dist/js/coreui-chartjs.js');

require('@coreui/coreui-chartjs/dist/css/coreui-chartjs.css');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
//import classNames from 'classnames';
//import Chart from 'react-chartjs-2';
//import { Bar, Doughnut, Line, Pie, Polar, Radar } from 'react-chartjs-2';


//component - CoreUI / CCharts

var CCharts = function CCharts(props) {
  var innerRef = props.innerRef,
      datasets = props.datasets,
      labels = props.labels,
      options = props.options,
      plugins = props.plugins,
      type = props.type,
      attributes = _objectWithoutProperties(props, ['innerRef', 'datasets', 'labels', 'options', 'plugins', 'type']);

  //console.log(Chart);

  var compData = (0, _react.useRef)({ firstRun: true }).current;

  var _useState = (0, _react.useState)(),
      chart = _useState[0],
      setChart = _useState[1];

  var ref = (0, _react.useRef)();

  var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  //  const type = ''

  // methods

  var renderChart = function renderChart() {
    destroyChart();
    setChart(new _chart2.default(ref.current.getContext('2d'), chartConfig));
  };
  var updateChart = function updateChart() {
    Object.assign(chart, chartConfig);
    chart.update();
  };
  var destroyChart = function destroyChart() {
    if (chart) {
      chart.destroy();
    }
  };

  // vars

  var _uid = '';
  var safeId = function () {
    // as long as this._uid() works there is no need to generate the key
    var key = function key() {
      return Math.random().toString(36).replace('0.', '');
    };
    return '__safe_id__' + _uid || key();
  }();
  var computedDatasets = function () {
    return datasets;
  }();
  //
  var computedLabels = function () {
    if (labels && typeof labels !== 'string') {
      return labels;
    } else if (!datasets || !datasets[0] || !datasets[0].data) {
      return [];
    }
    var emptyLabels = Array(datasets[0].data.length).fill('');
    if (labels === 'indexes') {
      return emptyLabels.map(function (u, i) {
        return i + 1;
      });
    } else if (labels === 'months') {
      return emptyLabels.map(function (u, i) {
        return months[i % 12];
      });
    }
    return emptyLabels;
  }();
  var computedData = function () {
    return {
      datasets: computedDatasets,
      labels: computedLabels
    };
  }();
  var customTooltips = function () {
    if (options && options.tooltips) {
      return;
    }
    return {
      tooltips: {
        enabled: false,
        custom: customTooltips,
        intersect: true,
        mode: 'index',
        position: 'nearest',
        callbacks: {
          labelColor: function labelColor(tooltipItem, chart) {
            function getValue(prop) {
              return (typeof prop === 'undefined' ? 'undefined' : _typeof(prop)) === 'object' ? prop[tooltipItem.index] : prop;
            }
            var dataset = chart.data.datasets[tooltipItem.datasetIndex];
            //tooltipLabelColor is coreUI custom prop used only here
            var backgroundColor = getValue(dataset.tooltipLabelColor || dataset.pointHoverBackgroundColor || dataset.borderColor || dataset.backgroundColor);
            return {
              backgroundColor: backgroundColor
            };
          }
        }
      }
    };
  }();
  var computedOptions = function () {
    return Object.assign({}, options, customTooltips || {});
  }();
  var chartConfig = function () {
    return {
      type: type,
      data: computedData,
      options: computedOptions || options,
      plugins: plugins
    };
  }();

  //watch

  //chartConfig
  (0, _react.useMemo)(function () {
    if (compData.firstRun) return;
    updateChart();
  }, [chartConfig]);

  // effect

  (0, _react.useEffect)(function () {
    renderChart();
    compData.firstRun = false;
    return function () {
      destroyChart();
    };
  }, []);

  // render

  return _react2.default.createElement(
    'div',
    _extends({}, attributes, { ref: innerRef }),
    _react2.default.createElement('canvas', { id: safeId, ref: ref })
  );
};

CCharts.propTypes = process.env.NODE_ENV !== "production" ? {
  //
  innerRef: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.func, _propTypes2.default.string]),
  datasets: _propTypes2.default.array,
  labels: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.string]),
  options: _propTypes2.default.object,
  plugins: _propTypes2.default.array,
  type: _propTypes2.default.string
} : {};

CCharts.defaultProps = {};

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