import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { Record, List } from 'immutable';
import { Utils } from '@kineticdata/bundle-common';
var withPayload = Utils.withPayload;
var ns = Utils.namespaceBuilder('techbar/app');
export var enableLocationServices = function enableLocationServices(techBars) {
  return techBars.size > 1 && techBars.filter(function (t) {
    return !!t.values['Latitude'] && !!t.values['Longitude'];
  }).size > 0;
};
export var mapTechBarsForDistance = function mapTechBarsForDistance(userLocation) {
  return function (techBar) {
    var distance = techBar.values['Latitude'] && techBar.values['Longitude'] ? calcDistance(userLocation, {
      latitude: techBar.values['Latitude'],
      longitude: techBar.values['Longitude']
    }) : null;
    return _objectSpread(_objectSpread({}, techBar), {}, {
      distance: distance
    });
  };
};
export var sortTechBarsByDistance = function sortTechBarsByDistance(a, b) {
  if (a.distance === null && b.distance === null || a.distance === b.distance) {
    return 0;
  } else if (a.distance !== null && b.distance !== null) {
    return a.distance < b.distance ? -1 : 1;
  } else if (b.distance === null) {
    return -1;
  } else {
    return 1;
  }
};

var calcDistance = function calcDistance(start, end) {
  var lat1 = parseFloat(start.latitude);
  var lon1 = parseFloat(start.longitude);
  var lat2 = parseFloat(end.latitude);
  var lon2 = parseFloat(end.longitude);

  if (!lat1 || !lon1 || !lat2 || !lon2) {
    return null;
  }

  var radlat1 = Math.PI * lat1 / 180;
  var radlat2 = Math.PI * lat2 / 180;
  var theta = lon1 - lon2;
  var radtheta = Math.PI * theta / 180;
  var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);

  if (dist > 1) {
    dist = 1;
  }

  dist = Math.acos(dist);
  dist = dist * 180 / Math.PI;
  dist = dist * 60 * 1.1515;
  return dist;
};

export var Settings = function Settings() {
  var object = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return {
    submissionId: object.id,
    feedbackIdentitifcation: object.values && object.values['Feedback Identification'] || 'Optional',
    allowWalkIns: (object.values && object.values['Allow Walk-Ins'] || 'Yes') === 'Yes'
  };
};
export var types = {
  FETCH_APP_DATA_REQUEST: ns('FETCH_APP_DATA_REQUEST'),
  FETCH_APP_DATA_SUCCESS: ns('FETCH_APP_DATA_SUCCESS'),
  FETCH_APP_DATA_FAILURE: ns('FETCH_APP_DATA_FAILURE'),
  UPDATE_TECH_BAR_SETTINGS_SUCCESS: ns('UPDATE_TECH_BAR_SETTINGS_SUCCESS'),
  FETCH_DISPLAY_TEAM_REQUEST: ns('FETCH_DISPLAY_TEAM_REQUEST'),
  FETCH_DISPLAY_TEAM_SUCCESS: ns('FETCH_DISPLAY_TEAM_SUCCESS'),
  FETCH_DISPLAY_TEAM_FAILURE: ns('FETCH_DISPLAY_TEAM_FAILURE'),
  CREATE_DISPLAY_TEAM_MEMBERSHIP_REQUEST: ns('CREATE_DISPLAY_TEAM_MEMBERSHIP_REQUEST'),
  CREATE_DISPLAY_TEAM_USER_REQUEST: ns('CREATE_DISPLAY_TEAM_USER_REQUEST'),
  DELETE_DISPLAY_TEAM_MEMBERSHIP_REQUEST: ns('DELETE_DISPLAY_TEAM_MEMBERSHIP_REQUEST')
};
export var actions = {
  fetchAppDataRequest: withPayload(types.FETCH_APP_DATA_REQUEST),
  fetchAppDataSuccess: withPayload(types.FETCH_APP_DATA_SUCCESS),
  fetchAppDataFailure: withPayload(types.FETCH_APP_DATA_FAILURE),
  updateTechBarSettingsSuccess: withPayload(types.UPDATE_TECH_BAR_SETTINGS_SUCCESS),
  fetchDisplayTeamRequest: withPayload(types.FETCH_DISPLAY_TEAM_REQUEST),
  fetchDisplayTeamSuccess: withPayload(types.FETCH_DISPLAY_TEAM_SUCCESS),
  fetchDisplayTeamFailure: withPayload(types.FETCH_DISPLAY_TEAM_FAILURE),
  createDisplayTeamMembershipRequest: withPayload(types.CREATE_DISPLAY_TEAM_MEMBERSHIP_REQUEST),
  createDisplayTeamUserRequest: withPayload(types.CREATE_DISPLAY_TEAM_USER_REQUEST),
  deleteDisplayTeamMembershipRequest: withPayload(types.DELETE_DISPLAY_TEAM_MEMBERSHIP_REQUEST)
};
export var State = Record({
  loading: true,
  error: null,
  schedulers: null,
  forms: null,
  displayTeamError: null,
  displayTeam: 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_APP_DATA_REQUEST:
      return state.set('error', null);

    case types.FETCH_APP_DATA_SUCCESS:
      return state.set('schedulers', List(payload.schedulers)).set('forms', List(payload.forms)).set('loading', false);

    case types.FETCH_APP_DATA_FAILURE:
      return state.set('error', payload);

    case types.UPDATE_TECH_BAR_SETTINGS_SUCCESS:
      var index = state.get('schedulers').findIndex(function (scheduler) {
        return scheduler.id === payload.techBarId;
      });

      if (index > -1) {
        return state.setIn(['schedulers', index, 'settings'], Settings(payload.submission));
      } else {
        return state;
      }

    case types.FETCH_DISPLAY_TEAM_REQUEST:
      return state.set('displayTeam', null).set('displayTeamError', null);

    case types.FETCH_DISPLAY_TEAM_SUCCESS:
      return state.set('displayTeam', payload);

    case types.FETCH_DISPLAY_TEAM_FAILURE:
      return state.set('displayTeamError', payload);

    default:
      return state;
  }
};