'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.savePrefs = exports.loadPrefs = exports.setUploadType = exports.setForm = exports.setSearchToRelatePageSize = exports.setSearchResultPagePageSize = exports.setSearchPanelPageSize = exports.setQuickSearchVocabulary = exports.setQuickSearchRecordType = exports.setSearchPageVocabulary = exports.setSearchPageRecordType = exports.setRecordBrowserNavBarItems = exports.setAdminTab = exports.collapsePanel = exports.storageKey = exports.SET_UPLOAD_TYPE = exports.SET_FORM = exports.SET_SEARCH_TO_RELATE_PAGE_SIZE = exports.SET_SEARCH_RESULT_PAGE_PAGE_SIZE = exports.SET_SEARCH_PANEL_PAGE_SIZE = exports.SET_QUICK_SEARCH_VOCABULARY = exports.SET_QUICK_SEARCH_RECORD_TYPE = exports.SET_SEARCH_PAGE_VOCABULARY = exports.SET_SEARCH_PAGE_RECORD_TYPE = exports.SET_RECORD_BROWSER_NAV_BAR_ITEMS = exports.SET_ADMIN_TAB = exports.COLLAPSE_PANEL = exports.PREFS_LOADED = undefined;

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

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

/* global window */

const PREFS_LOADED = exports.PREFS_LOADED = 'PREFS_LOADED';
const COLLAPSE_PANEL = exports.COLLAPSE_PANEL = 'COLLAPSE_PANEL';
const SET_ADMIN_TAB = exports.SET_ADMIN_TAB = 'SET_ADMIN_TAB';
const SET_RECORD_BROWSER_NAV_BAR_ITEMS = exports.SET_RECORD_BROWSER_NAV_BAR_ITEMS = 'SET_RECORD_BROWSER_NAV_BAR_ITEMS';
const SET_SEARCH_PAGE_RECORD_TYPE = exports.SET_SEARCH_PAGE_RECORD_TYPE = 'SET_SEARCH_PAGE_RECORD_TYPE';
const SET_SEARCH_PAGE_VOCABULARY = exports.SET_SEARCH_PAGE_VOCABULARY = 'SET_SEARCH_PAGE_VOCABULARY';
const SET_QUICK_SEARCH_RECORD_TYPE = exports.SET_QUICK_SEARCH_RECORD_TYPE = 'SET_QUICK_SEARCH_RECORD_TYPE';
const SET_QUICK_SEARCH_VOCABULARY = exports.SET_QUICK_SEARCH_VOCABULARY = 'SET_QUICK_SEARCH_VOCABULARY';
const SET_SEARCH_PANEL_PAGE_SIZE = exports.SET_SEARCH_PANEL_PAGE_SIZE = 'SET_SEARCH_PANEL_PAGE_SIZE';
const SET_SEARCH_RESULT_PAGE_PAGE_SIZE = exports.SET_SEARCH_RESULT_PAGE_PAGE_SIZE = 'SET_SEARCH_RESULT_PAGE_PAGE_SIZE';
const SET_SEARCH_TO_RELATE_PAGE_SIZE = exports.SET_SEARCH_TO_RELATE_PAGE_SIZE = 'SET_SEARCH_TO_RELATE_PAGE_SIZE';
const SET_FORM = exports.SET_FORM = 'SET_FORM';
const SET_UPLOAD_TYPE = exports.SET_UPLOAD_TYPE = 'SET_UPLOAD_TYPE';

const storageKey = exports.storageKey = 'cspace-ui';

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

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

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

const setSearchPageRecordType = exports.setSearchPageRecordType = value => ({
  type: SET_SEARCH_PAGE_RECORD_TYPE,
  payload: value
});

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

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

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

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

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

const setSearchToRelatePageSize = exports.setSearchToRelatePageSize = pageSize => ({
  type: SET_SEARCH_TO_RELATE_PAGE_SIZE,
  payload: pageSize
});

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

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

const loadPrefs = exports.loadPrefs = 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 = _immutable2.default.fromJS(JSON.parse(serializedPrefs)[username]);
      } catch (error) {
        userPrefs = null;
      }
    }
  }

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

const savePrefs = exports.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));
  }
};