"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.savePrefs = exports.loadPrefs = exports.setStickyFields = exports.toggleSearchResultSidebar = exports.toggleRecordSidebar = exports.setUploadType = exports.setForm = exports.setSearchToSelectPageSize = exports.setSearchResultPagePageSize = exports.setSearchPanelPageSize = exports.setQuickSearchVocabulary = exports.setQuickSearchRecordType = exports.setSearchPageVocabulary = exports.setSearchPageRecordType = exports.setRecordBrowserNavBarItems = exports.setToolTab = exports.setAdminTab = exports.collapsePanel = exports.storageKey = void 0;

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

var _get = _interopRequireDefault(require("lodash/get"));

var _recordDataHelpers = require("../helpers/recordDataHelpers");

var _reducers = require("../reducers");

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

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

/* global window */
const storageKey = 'cspace-ui';
exports.storageKey = storageKey;

const collapsePanel = (recordType, name, collapsed) => ({
  type: _actionCodes.COLLAPSE_PANEL,
  payload: collapsed,
  meta: {
    recordType,
    name
  }
});

exports.collapsePanel = collapsePanel;

const setAdminTab = tabName => ({
  type: _actionCodes.SET_ADMIN_TAB,
  payload: tabName
});

exports.setAdminTab = setAdminTab;

const setToolTab = tabName => ({
  type: _actionCodes.SET_TOOL_TAB,
  payload: tabName
});

exports.setToolTab = setToolTab;

const setRecordBrowserNavBarItems = (recordType, navBarItems) => ({
  type: _actionCodes.SET_RECORD_BROWSER_NAV_BAR_ITEMS,
  payload: navBarItems,
  meta: {
    recordType
  }
});

exports.setRecordBrowserNavBarItems = setRecordBrowserNavBarItems;

const setSearchPageRecordType = value => (dispatch, getState) => {
  if (value !== (0, _reducers.getSearchPageRecordType)(getState())) {
    dispatch({
      type: _actionCodes.SET_SEARCH_PAGE_RECORD_TYPE,
      payload: value
    });
  }
};

exports.setSearchPageRecordType = setSearchPageRecordType;

const setSearchPageVocabulary = value => ({
  type: _actionCodes.SET_SEARCH_PAGE_VOCABULARY,
  payload: value
});

exports.setSearchPageVocabulary = setSearchPageVocabulary;

const setQuickSearchRecordType = value => ({
  type: _actionCodes.SET_QUICK_SEARCH_RECORD_TYPE,
  payload: value
});

exports.setQuickSearchRecordType = setQuickSearchRecordType;

const setQuickSearchVocabulary = value => ({
  type: _actionCodes.SET_QUICK_SEARCH_VOCABULARY,
  payload: value
});

exports.setQuickSearchVocabulary = setQuickSearchVocabulary;

const setSearchPanelPageSize = (recordType, name, pageSize) => ({
  type: _actionCodes.SET_SEARCH_PANEL_PAGE_SIZE,
  payload: pageSize,
  meta: {
    recordType,
    name
  }
});

exports.setSearchPanelPageSize = setSearchPanelPageSize;

const setSearchResultPagePageSize = pageSize => ({
  type: _actionCodes.SET_SEARCH_RESULT_PAGE_PAGE_SIZE,
  payload: pageSize
});

exports.setSearchResultPagePageSize = setSearchResultPagePageSize;

const setSearchToSelectPageSize = pageSize => ({
  type: _actionCodes.SET_SEARCH_TO_SELECT_PAGE_SIZE,
  payload: pageSize
});

exports.setSearchToSelectPageSize = setSearchToSelectPageSize;

const setForm = (recordType, formName) => ({
  type: _actionCodes.SET_FORM,
  payload: formName,
  meta: {
    recordType
  }
});

exports.setForm = setForm;

const setUploadType = type => ({
  type: _actionCodes.SET_UPLOAD_TYPE,
  payload: type
});

exports.setUploadType = setUploadType;

const toggleRecordSidebar = () => ({
  type: _actionCodes.TOGGLE_RECORD_SIDEBAR
});

exports.toggleRecordSidebar = toggleRecordSidebar;

const toggleSearchResultSidebar = () => ({
  type: _actionCodes.TOGGLE_SEARCH_RESULT_SIDEBAR
});

exports.toggleSearchResultSidebar = toggleSearchResultSidebar;

const setStickyFields = (recordTypeConfig, csid) => (dispatch, getState) => {
  const data = (0, _reducers.getRecordData)(getState(), csid);

  if (data) {
    const stickyData = (0, _recordDataHelpers.getStickyFieldValues)(recordTypeConfig, data);
    dispatch({
      type: _actionCodes.SET_STICKY_FIELDS,
      payload: stickyData,
      meta: {
        recordTypeConfig
      }
    });
  }
};

exports.setStickyFields = setStickyFields;

const loadPrefs = (config, username) => dispatch => {
  // TODO: Load prefs from server (requires adding services layer support).
  // For now, just load from local storage.
  let userPrefs = null;

  if (username) {
    const serializedPrefs = window.localStorage.getItem(storageKey);

    if (serializedPrefs) {
      try {
        userPrefs = _immutable.default.fromJS(JSON.parse(serializedPrefs)[username]);
      } catch (error) {
        userPrefs = null;
      }
    }
  }

  const defaultUserPrefs = (0, _get.default)(config, 'defaultUserPrefs');

  if (defaultUserPrefs) {
    userPrefs = _immutable.default.fromJS(defaultUserPrefs).mergeDeep(userPrefs);
  }

  dispatch({
    type: _actionCodes.PREFS_LOADED,
    payload: userPrefs
  });
};

exports.loadPrefs = loadPrefs;

const savePrefs = () => (dispatch, getState) => {
  // TODO: Save prefs to server (requires adding services layer support).
  // For now, just save to local storage.
  const username = (0, _reducers.getUserUsername)(getState());
  let prefs;

  if (username) {
    const serializedPrefs = window.localStorage.getItem(storageKey);

    if (serializedPrefs) {
      try {
        prefs = JSON.parse(serializedPrefs);
      } catch (error) {
        prefs = null;
      }
    }

    if (!prefs) {
      prefs = {};
    }

    prefs[username] = (0, _reducers.getPrefs)(getState());
    window.localStorage.setItem(storageKey, JSON.stringify(prefs));
  }
};

exports.savePrefs = savePrefs;