'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 */

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

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

var collapsePanel = exports.collapsePanel = function collapsePanel(recordType, name, collapsed) {
  return {
    type: COLLAPSE_PANEL,
    payload: collapsed,
    meta: {
      recordType: recordType,
      name: name
    }
  };
};

var setAdminTab = exports.setAdminTab = function setAdminTab(tabName) {
  return {
    type: SET_ADMIN_TAB,
    payload: tabName
  };
};

var setRecordBrowserNavBarItems = exports.setRecordBrowserNavBarItems = function setRecordBrowserNavBarItems(recordType, navBarItems) {
  return {
    type: SET_RECORD_BROWSER_NAV_BAR_ITEMS,
    payload: navBarItems,
    meta: {
      recordType: recordType
    }
  };
};

var setSearchPageRecordType = exports.setSearchPageRecordType = function setSearchPageRecordType(value) {
  return {
    type: SET_SEARCH_PAGE_RECORD_TYPE,
    payload: value
  };
};

var setSearchPageVocabulary = exports.setSearchPageVocabulary = function setSearchPageVocabulary(value) {
  return {
    type: SET_SEARCH_PAGE_VOCABULARY,
    payload: value
  };
};

var setQuickSearchRecordType = exports.setQuickSearchRecordType = function setQuickSearchRecordType(value) {
  return {
    type: SET_QUICK_SEARCH_RECORD_TYPE,
    payload: value
  };
};

var setQuickSearchVocabulary = exports.setQuickSearchVocabulary = function setQuickSearchVocabulary(value) {
  return {
    type: SET_QUICK_SEARCH_VOCABULARY,
    payload: value
  };
};

var setSearchPanelPageSize = exports.setSearchPanelPageSize = function setSearchPanelPageSize(recordType, name, pageSize) {
  return {
    type: SET_SEARCH_PANEL_PAGE_SIZE,
    payload: pageSize,
    meta: {
      recordType: recordType,
      name: name
    }
  };
};

var setSearchResultPagePageSize = exports.setSearchResultPagePageSize = function setSearchResultPagePageSize(pageSize) {
  return {
    type: SET_SEARCH_RESULT_PAGE_PAGE_SIZE,
    payload: pageSize
  };
};

var setSearchToRelatePageSize = exports.setSearchToRelatePageSize = function setSearchToRelatePageSize(pageSize) {
  return {
    type: SET_SEARCH_TO_RELATE_PAGE_SIZE,
    payload: pageSize
  };
};

var setForm = exports.setForm = function setForm(recordType, formName) {
  return {
    type: SET_FORM,
    payload: formName,
    meta: {
      recordType: recordType
    }
  };
};

var setUploadType = exports.setUploadType = function setUploadType(type) {
  return {
    type: SET_UPLOAD_TYPE,
    payload: type
  };
};

var loadPrefs = exports.loadPrefs = function loadPrefs(username) {
  return function (dispatch) {
    // TODO: Load prefs from server (requires adding services layer support).
    // For now, just load from local storage.

    var userPrefs = null;

    if (username) {
      var 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
    });
  };
};

var savePrefs = exports.savePrefs = function savePrefs() {
  return function (dispatch, getState) {
    // TODO: Save prefs to server (requires adding services layer support).
    // For now, just save to local storage.

    var username = (0, _reducers.getUserUsername)(getState());

    var prefs = void 0;

    if (username) {
      var 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));
    }
  };
};