'use strict';

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var capitalize = require('../../node_modules/lodash-es/capitalize.js');
var filter = require('../../node_modules/lodash-es/filter.js');
var omit = require('../../node_modules/lodash-es/omit.js');
var merge = require('../../node_modules/lodash-es/merge.js');
var isNil = require('../../node_modules/lodash-es/isNil.js');
var isEmpty = require('../../node_modules/lodash-es/isEmpty.js');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.array.find.js');
require('core-js/modules/es.array.iterator.js');
require('core-js/modules/es.array.map.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.set.js');
require('core-js/modules/es.string.iterator.js');
require('core-js/modules/web.dom-collections.for-each.js');
require('core-js/modules/web.dom-collections.iterator.js');
var React = require('react');
var cardUtilityFunctions = require('../../utils/cardUtilityFunctions.js');
var CardPropTypes = require('../../constants/CardPropTypes.js');
var timeSeriesUtils = require('../TimeSeriesCard/timeSeriesUtils.js');
var LayoutConstants = require('../../constants/LayoutConstants.js');
var dayjs = require('../../utils/dayjs.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefault(_toConsumableArray);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }

/**
 * Formats and maps the colors to their corresponding datasets in the carbon charts tabular data format
 * @param {Array} series an array of dataset group classifications
 * @returns {Object} colors - formatted
 */
var formatColors = function formatColors() {
  var series = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var colors = {
    scale: {}
  };
  if (Array.isArray(series)) {
    series.forEach(function (dataset, index) {
      colors.scale[dataset.label] = dataset.color || CardPropTypes.CHART_COLORS[index % CardPropTypes.CHART_COLORS.length];
    });
  } else {
    colors.scale[series.label] = series.color || CardPropTypes.CHART_COLORS[0];
  }
  return colors;
};

/**
 * Configures the axes object for the comboChart
 */

var configureAxes = function configureAxes(content) {
  var domainRange = content.domainRange,
    xLabel = content.xLabel,
    yLabel = content.yLabel,
    unit = content.unit,
    chartType = content.chartType,
    decimalPrecision = content.decimalPrecision,
    includeZeroOnXaxis = content.includeZeroOnXaxis,
    interval = content.interval,
    locale = content.locale,
    series = content.series,
    showTimeInGMT = content.showTimeInGMT,
    size = content.size,
    includeZeroOnYaxis = content.includeZeroOnYaxis,
    previousTick = content.previousTick,
    thresholds = content.thresholds;

  /** *
   * timestamp of current value
   * index of current value
   * ticks: array of current ticks or a single string with part of the date, e.g. month or year & month
   */
  var formatTick = function formatTick(timestamp, index, ticks) {
    var previousTimestamp = previousTick.current;
    // store current in the previous tick
    previousTick.current = timestamp;
    return timeSeriesUtils.formatGraphTick(timestamp, index, ticks, interval, locale, previousTimestamp, showTimeInGMT);
  };

  // Checks size property against new size naming convention and reassigns to closest supported size if necessary.
  var newSize = cardUtilityFunctions.getUpdatedCardSize(size);
  var getMaxTicksPerSize = function getMaxTicksPerSize(s) {
    switch (s) {
      case LayoutConstants.CARD_SIZES.MEDIUMTHIN:
        return 2;
      case LayoutConstants.CARD_SIZES.MEDIUM:
        return 4;
      case LayoutConstants.CARD_SIZES.MEDIUMWIDE:
      case LayoutConstants.CARD_SIZES.LARGE:
        return 6;
      case LayoutConstants.CARD_SIZES.LARGEWIDE:
        return 14;
      default:
        return 10;
    }
  };
  return {
    bottom: _objectSpread({
      title: xLabel || ' ',
      mapsTo: 'date',
      scaleType: 'time',
      ticks: {
        max: getMaxTicksPerSize(newSize),
        formatter: formatTick
      },
      includeZero: includeZeroOnXaxis
    }, domainRange ? {
      domain: domainRange
    } : {}),
    left: {
      title: "".concat(yLabel || '', " ").concat(unit ? "(".concat(unit, ")") : ''),
      mapsTo: 'value',
      ticks: {
        formatter: function formatter(axisValue) {
          return cardUtilityFunctions.chartValueFormatter(axisValue, newSize, null, locale, decimalPrecision);
        }
      },
      stacked: chartType === LayoutConstants.TIME_SERIES_TYPES.BAR && series.length > 1,
      includeZero: includeZeroOnYaxis,
      scaleType: 'linear',
      thresholds: thresholds
    }
  };
};

/**
 * Translates our raw data into a language the carbon-charts understand
 * @param {string} timeDataSourceId, the field that identifies the timestamp value in the data
 * @param {array} series, an array of lines to create in our chart
 * @param {array} values, the array of values from our data layer
 *
 * TODO: Handle empty data lines gracefully and notify the user of data lines that did not
 * match the dataFilter
 *
 * @returns {object} with a labels array and a datasets array
 */

var useChartData = function useChartData(values, _ref) {
  var _ref$series = _ref.series,
    series = _ref$series === void 0 ? [] : _ref$series,
    timeDataSourceId = _ref.timeDataSourceId,
    showTimeInGMT = _ref.showTimeInGMT;
  // Carbon charts is incredibly slow with large datasets sometimes, so let's shave off a little time where we can
  return React.useMemo(function () {
    // Generate a set of unique timestamps for the values
    var timestamps = _toConsumableArray__default.default(new Set(values.map(function (val) {
      return val[timeDataSourceId];
    })));
    var data = [];
    var seriesArray = Array.isArray(series) ? series : [series];

    // Series is the different groups of datasets
    seriesArray.forEach(function (_ref2) {
      var dataSourceId = _ref2.dataSourceId,
        _ref2$dataFilter = _ref2.dataFilter,
        dataFilter = _ref2$dataFilter === void 0 ? {} : _ref2$dataFilter,
        label = _ref2.label;
      timestamps.forEach(function (timestamp) {
        // First filter based on on the dataFilter
        var filteredData = filter(values, dataFilter);
        if (!isEmpty(filteredData)) {
          // have to filter out null values from the dataset, as it causes Carbon Charts to break
          filteredData.filter(function (dataItem) {
            // only allow valid timestamp matches
            return !isNil(dataItem[timeDataSourceId]) && dataItem[timeDataSourceId] === timestamp;
          }).forEach(function (dataItem) {
            // Check to see if the data Item actually exists in this timestamp before adding to data (to support sparse data in the values)
            if (dataItem[dataSourceId]) {
              data.push({
                date: dataItem[timeDataSourceId] instanceof Date ? dataItem[timeDataSourceId] : showTimeInGMT ? dayjs.default.utc(dataItem[timeDataSourceId]) : dayjs.default(dataItem[timeDataSourceId]),
                value: dataItem[dataSourceId],
                group: label
              });
            }
          });
        }
      });
    });
    return data;
  }, [values, series, timeDataSourceId, showTimeInGMT]);
};
var useChartOptions = function useChartOptions(content) {
  return React.useMemo(function () {
    var comboChartTypes = content.comboChartTypes,
      decimalPrecision = content.decimalPrecision,
      i18n = content.i18n,
      isEditable = content.isEditable,
      isLoading = content.isLoading,
      locale = content.locale,
      series = content.series,
      showTimeInGMT = content.showTimeInGMT,
      size = content.size,
      timeDataSourceId = content.timeDataSourceId,
      tooltipDateFormatPattern = content.tooltipDateFormatPattern,
      defaultDateFormatPattern = content.defaultDateFormatPattern,
      unit = content.unit,
      values = content.values;
    var options = {
      experimental: true,
      animations: false,
      accessibility: true,
      axes: configureAxes(content),
      legend: {
        position: 'bottom',
        enabled: values.length > 1,
        clickable: !isEditable
      },
      containerResizable: true,
      comboChartTypes: comboChartTypes,
      series: series,
      color: formatColors(series),
      timeScale: {
        addSpaceOnEdges: 0
      },
      tooltip: {
        valueFormatter: function valueFormatter(tooltipValue) {
          return cardUtilityFunctions.chartValueFormatter(tooltipValue, size, unit, locale, decimalPrecision);
        },
        customHTML: function customHTML() {
          for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
          }
          return cardUtilityFunctions.handleTooltip.apply(void 0, args.concat([timeDataSourceId, showTimeInGMT, tooltipDateFormatPattern, locale]));
        },
        groupLabel: i18n.tooltipGroupLabel,
        totalLabel: i18n.tooltipTotalLabel
      },
      toolbar: {
        enabled: false
      },
      defaultDateFormatPattern: defaultDateFormatPattern
    };
    options.data = merge(options.data, {
      loading: isLoading
    });
    return _objectSpread(_objectSpread({}, options), content);
  }, [content]);
};
var extractDataAndColumnNames = function extractDataAndColumnNames(values, chartOptions) {
  var timeDataSourceId = chartOptions.timeDataSourceId,
    showTimeInGMT = chartOptions.showTimeInGMT,
    defaultDateFormatPattern = chartOptions.defaultDateFormatPattern;
  var maxColumnNames = [];
  var tableValues = values.map(function (value, index) {
    var currentValueColumns = Object.keys(omit(value, timeDataSourceId));
    maxColumnNames = currentValueColumns.length > maxColumnNames.length ? currentValueColumns : maxColumnNames;
    return {
      id: "dataindex-".concat(index),
      values: _objectSpread(_objectSpread({}, omit(value, timeDataSourceId)), {}, _defineProperty__default.default({}, timeDataSourceId, (showTimeInGMT ? dayjs.default.utc(value[timeDataSourceId]) : dayjs.default(value[timeDataSourceId])).format(defaultDateFormatPattern))),
      isSelectable: false
    };
  });
  return {
    tableData: tableValues,
    columnNames: maxColumnNames
  };
};
var useTableData = function useTableData(values, chartOptions) {
  var _useMemo = React.useMemo(function () {
      return extractDataAndColumnNames(values, chartOptions);
    }, [values, chartOptions]),
    tableData = _useMemo.tableData;
  return tableData;
};
var useTableColumns = function useTableColumns(values, chartOptions) {
  var timeDataSourceId = chartOptions.timeDataSourceId,
    decimalPrecision = chartOptions.decimalPrecision,
    i18n = chartOptions.i18n,
    size = chartOptions.size,
    series = chartOptions.series,
    unit = chartOptions.unit,
    locale = chartOptions.locale;
  var _extractDataAndColumn = extractDataAndColumnNames(values, chartOptions),
    columnNames = _extractDataAndColumn.columnNames;

  // In expanded mode we show the data underneath the linechart in a table so need to build the columns
  var tableColumns = React.useMemo(function () {
    // First column is timestamp
    var columns = [{
      id: timeDataSourceId,
      name: capitalize(timeDataSourceId),
      isSortable: true,
      type: 'TIMESTAMP'
    }];
    // then the rest in series order
    return columns.concat(columnNames.map(function (columnName) {
      var matchingDataSource = Array.isArray(series) ? series.find(function (d) {
        return d.dataSourceId === columnName;
      }) : series;
      return {
        id: columnName,
        // use the label if one exists as it will be the user-defined, readable name
        // UNLESS dataFilter is enabled as the matchingDataSource will only find the first match
        name: matchingDataSource ? matchingDataSource.dataFilter ? matchingDataSource.dataSourceId : matchingDataSource.label : columnName,
        isSortable: true,
        filter: {
          placeholderText: i18n.defaultFilterStringPlaceholdText
        },
        renderDataFunction: function renderDataFunction(_ref3) {
          var value = _ref3.value;
          if (typeof value === 'number' && !isNil(decimalPrecision)) {
            return cardUtilityFunctions.chartValueFormatter(value, size, unit, locale, decimalPrecision);
          }
          return value;
        }
      };
    }));
  }, [columnNames, decimalPrecision, i18n.defaultFilterStringPlaceholdText, locale, series, size, timeDataSourceId, unit]);
  return tableColumns;
};

exports.useChartData = useChartData;
exports.useChartOptions = useChartOptions;
exports.useTableColumns = useTableColumns;
exports.useTableData = useTableData;
