import {createReducer} from 'redux-starter-kit';
import * as Actions from '../actions/release_version_actions';
import * as Constants from '../../constants';

const versionLookup = Constants.RELEASE_TYPES.map(k=> {
  return {
    [k]: ['unavailable']
  };
}).reduce((map, obj) => {
  return {
    ...map,
    ...obj,
  };
});

export const releaseVersionReducer = createReducer({
  InstalledVersion: 'none',
  LocationsLookup: {},
  Release: 2,
  ReleaseUpgradeAvailable: false,
  UpgradeAvailable: false,
  UpgradeData: null,
  UpgradeDismissed: false,
  Version: -1,
  VersionLookup: versionLookup,
}, {
  [Actions.CLEAR_UI_UPGRADE]: state => {
    return {
      ...state,
      UpgradeAvailable: false,
      UpgradeDismissed: false,
      UpgradeData: null,
    };
  },
  [Actions.SET_ACTIVE_RELEASE]: (state, action) => {
    return {
      ...state,
      Release: action.payload.release,
      Version: action.payload.version
    };
  },
  [Actions.setDismissUIUpgrade]: (state, action) => {
    return {
      ...state,
      UpgradeDismissed: action.payload,
    };
  },
  [Actions.setInstalledVersion]: (state, action) => {
    return {
      ...state,
      InstalledVersion: action.payload,
    }
  },
  [Actions.SET_RELEASE_DATA]: (state, action) => {
    return {
      ...state,
      LocationsLookup: action.payload.locations,
      VersionLookup: action.payload.versions,
    };
  },
  [Actions.setReleaseUpgradeAvailable]: (state, action) => {
    return {
      ...state,
      ReleaseUpgradeAvailable: action.payload,
    };
  },
  [Actions.setUIUpgradeData]: (state, action) => {
    return {
      ...state,
      UpgradeAvailable: true,
      UpgradeData: action.payload,
      UpgradeDismissed: false,
    };
  }
});