"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.types = exports.reducer = exports.actions = exports.State = exports.MetricsData = void 0;
var _immutable = require("immutable");
var _bundleCommon = require("@kineticdata/bundle-common");
var withPayload = _bundleCommon.Utils.withPayload;
var ns = _bundleCommon.Utils.namespaceBuilder('techbar/metrics');
var types = exports.types = {
  FETCH_METRICS_REQUEST: ns('FETCH_METRICS_REQUEST'),
  FETCH_METRICS_SUCCESS: ns('FETCH_METRICS_SUCCESS'),
  FETCH_METRICS_FAILURE: ns('FETCH_METRICS_FAILURE'),
  FETCH_METRICS_RESET: ns('FETCH_METRICS_RESET')
};
var actions = exports.actions = {
  fetchMetricsRequest: withPayload(types.FETCH_METRICS_REQUEST),
  fetchMetricsSuccess: withPayload(types.FETCH_METRICS_SUCCESS),
  fetchMetricsFailure: withPayload(types.FETCH_METRICS_FAILURE),
  fetchMetricsReset: withPayload(types.FETCH_METRICS_RESET)
};
var MetricsData = exports.MetricsData = function MetricsData(object) {
  try {
    return {
      schedulerId: object.values['Scheduler Id'],
      periodType: object.values['Period Type'],
      period: object.values['Period'],
      data: JSON.parse(object.values['Data'])
    };
  } catch (e) {
    console.warn('Fetched Metrics record is invalid. Please contact an administrator.');
    return null;
  }
};
var State = exports.State = (0, _immutable.Record)({
  data: null,
  error: null
});
var reducer = exports.reducer = function reducer() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : State();
  var _ref = arguments.length > 1 ? arguments[1] : undefined,
    type = _ref.type,
    payload = _ref.payload;
  switch (type) {
    case types.FETCH_METRICS_REQUEST:
      return state.set('error', null).set('data', null);
    case types.FETCH_METRICS_SUCCESS:
      return state.set('data', (0, _immutable.List)(payload.map(function (submission) {
        return MetricsData(submission);
      }).filter(function (d) {
        return d;
      })));
    case types.FETCH_METRICS_FAILURE:
      return state.set('error', payload);
    case types.FETCH_METRICS_RESET:
      return state.set('data', null).set('error', null);
    default:
      return state;
  }
};