import { Record, List } from 'immutable';
import { Utils } from '@kineticdata/bundle-common';
var withPayload = Utils.withPayload;
var ns = Utils.namespaceBuilder('techbar/metrics');
export var 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')
};
export var 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)
};
export var 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;
  }
};
export var State = Record({
  data: null,
  error: null
});
export var 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', 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;
  }
};