"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getStickyFields = exports.isSearchResultSidebarOpen = exports.isRecordSidebarOpen = exports.getToolTab = exports.getAdminTab = exports.getUploadType = exports.getForm = exports.getRecordBrowserNavBarItems = exports.isPanelCollapsed = exports.getSearchToSelectPageSize = exports.getSearchResultPagePageSize = exports.getSearchPanelPageSize = exports.getQuickSearchVocabulary = exports.getQuickSearchRecordType = exports.getSearchPageVocabulary = exports.getSearchPageRecordType = exports.getAdvancedSearchBooleanOp = exports.default = void 0;

var _immutable = _interopRequireDefault(require("immutable"));

var _searchOperators = require("../constants/searchOperators");

var _actionCodes = require("../constants/actionCodes");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const handleAdvancedSearchConditionChange = (state, action) => {
  const condition = action.payload;
  const op = condition ? condition.get('op') : null;
  return op === _searchOperators.OP_AND || op === _searchOperators.OP_OR ? state.set('advancedSearchBooleanOp', op) : state;
};

const handleToggleRecordSidebar = state => {
  let isOpen = state.get('recordSidebarOpen');

  if (typeof isOpen === 'undefined') {
    isOpen = true;
  }

  return state.set('recordSidebarOpen', !isOpen);
};

const handleToggleSearchResultSidebar = state => {
  let isOpen = state.get('searchResultSidebarOpen');

  if (typeof isOpen === 'undefined') {
    isOpen = true;
  }

  return state.set('searchResultSidebarOpen', !isOpen);
};

const setStickyFields = (state, action) => {
  const data = action.payload;
  const {
    recordTypeConfig
  } = action.meta;
  return state.setIn(['stickyFields', recordTypeConfig.name], data);
};

var _default = function _default() {
  let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _immutable.default.Map();
  let action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _actionCodes.PREFS_LOADED:
      return action.payload || _immutable.default.Map();

    case _actionCodes.COLLAPSE_PANEL:
      return state.setIn(['panels', action.meta.recordType, action.meta.name, 'collapsed'], action.payload);

    case _actionCodes.SET_ADMIN_TAB:
      return state.set('adminTab', action.payload);

    case _actionCodes.SET_TOOL_TAB:
      return state.set('toolTab', action.payload);

    case _actionCodes.SET_RECORD_BROWSER_NAV_BAR_ITEMS:
      return state.setIn(['recordBrowserNavBarItems', action.meta.recordType], action.payload);

    case _actionCodes.SET_SEARCH_PAGE_RECORD_TYPE:
      return state.setIn(['searchPage', 'recordType'], action.payload);

    case _actionCodes.SET_SEARCH_PAGE_VOCABULARY:
      return state.setIn(['searchPage', 'vocabulary', state.getIn(['searchPage', 'recordType'])], action.payload);

    case _actionCodes.SET_QUICK_SEARCH_RECORD_TYPE:
      return state.setIn(['quickSearch', 'recordType'], action.payload);

    case _actionCodes.SET_QUICK_SEARCH_VOCABULARY:
      return state.setIn(['quickSearch', 'vocabulary', state.getIn(['quickSearch', 'recordType'])], action.payload);

    case _actionCodes.SET_SEARCH_PANEL_PAGE_SIZE:
      return state.setIn(['panels', action.meta.recordType, action.meta.name, 'pageSize'], action.payload);

    case _actionCodes.SET_SEARCH_RESULT_PAGE_PAGE_SIZE:
      return state.set('searchResultPagePageSize', action.payload);

    case _actionCodes.SET_SEARCH_TO_SELECT_PAGE_SIZE:
      return state.set('searchToSelectPageSize', action.payload);

    case _actionCodes.SET_FORM:
      return state.setIn(['form', action.meta.recordType], action.payload);

    case _actionCodes.SET_UPLOAD_TYPE:
      return state.set('uploadType', action.payload);

    case _actionCodes.SET_SEARCH_PAGE_ADVANCED:
    case _actionCodes.SET_SEARCH_TO_SELECT_ADVANCED:
      return handleAdvancedSearchConditionChange(state, action);

    case _actionCodes.TOGGLE_RECORD_SIDEBAR:
      return handleToggleRecordSidebar(state, action);

    case _actionCodes.TOGGLE_SEARCH_RESULT_SIDEBAR:
      return handleToggleSearchResultSidebar(state, action);

    case _actionCodes.SET_STICKY_FIELDS:
      return setStickyFields(state, action);

    default:
      return state;
  }
};

exports.default = _default;

const getAdvancedSearchBooleanOp = state => state.get('advancedSearchBooleanOp');

exports.getAdvancedSearchBooleanOp = getAdvancedSearchBooleanOp;

const getSearchPageRecordType = state => state.getIn(['searchPage', 'recordType']);

exports.getSearchPageRecordType = getSearchPageRecordType;

const getSearchPageVocabulary = (state, recordType) => state.getIn(['searchPage', 'vocabulary', recordType]);

exports.getSearchPageVocabulary = getSearchPageVocabulary;

const getQuickSearchRecordType = state => state.getIn(['quickSearch', 'recordType']);

exports.getQuickSearchRecordType = getQuickSearchRecordType;

const getQuickSearchVocabulary = (state, recordType) => state.getIn(['quickSearch', 'vocabulary', recordType]);

exports.getQuickSearchVocabulary = getQuickSearchVocabulary;

const getSearchPanelPageSize = (state, recordType, name) => state.getIn(['panels', recordType, name, 'pageSize']);

exports.getSearchPanelPageSize = getSearchPanelPageSize;

const getSearchResultPagePageSize = state => state.get('searchResultPagePageSize');

exports.getSearchResultPagePageSize = getSearchResultPagePageSize;

const getSearchToSelectPageSize = state => state.get('searchToSelectPageSize');

exports.getSearchToSelectPageSize = getSearchToSelectPageSize;

const isPanelCollapsed = (state, recordType, name) => state.getIn(['panels', recordType, name, 'collapsed']);

exports.isPanelCollapsed = isPanelCollapsed;

const getRecordBrowserNavBarItems = (state, recordType) => state.getIn(['recordBrowserNavBarItems', recordType]);

exports.getRecordBrowserNavBarItems = getRecordBrowserNavBarItems;

const getForm = (state, recordType) => state.getIn(['form', recordType]);

exports.getForm = getForm;

const getUploadType = state => state.get('uploadType');

exports.getUploadType = getUploadType;

const getAdminTab = state => state.get('adminTab');

exports.getAdminTab = getAdminTab;

const getToolTab = state => state.get('toolTab');

exports.getToolTab = getToolTab;

const isRecordSidebarOpen = state => state.get('recordSidebarOpen');

exports.isRecordSidebarOpen = isRecordSidebarOpen;

const isSearchResultSidebarOpen = state => state.get('searchResultSidebarOpen');

exports.isSearchResultSidebarOpen = isSearchResultSidebarOpen;

const getStickyFields = (state, recordType) => {
  if (typeof recordType === 'undefined') {
    return state.get('stickyFields');
  }

  return state.getIn(['stickyFields', recordType]);
};

exports.getStickyFields = getStickyFields;