'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.savePrefs = exports.loadPrefs = exports.setSearchPanelPageSize = exports.setSearchPageSize = exports.setKeywordSearchVocabulary = exports.setKeywordSearchRecordType = exports.collapsePanel = exports.storageKey = exports.SET_SEARCH_PANEL_PAGE_SIZE = exports.SET_SEARCH_PAGE_SIZE = exports.SET_KEYWORD_SEARCH_VOCABULARY = exports.SET_KEYWORD_SEARCH_RECORD_TYPE = 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_KEYWORD_SEARCH_RECORD_TYPE = exports.SET_KEYWORD_SEARCH_RECORD_TYPE = 'SET_KEYWORD_SEARCH_RECORD_TYPE';
var SET_KEYWORD_SEARCH_VOCABULARY = exports.SET_KEYWORD_SEARCH_VOCABULARY = 'SET_KEYWORD_SEARCH_VOCABULARY';
var SET_SEARCH_PAGE_SIZE = exports.SET_SEARCH_PAGE_SIZE = 'SET_SEARCH_PAGE_SIZE';
var SET_SEARCH_PANEL_PAGE_SIZE = exports.SET_SEARCH_PANEL_PAGE_SIZE = 'SET_SEARCH_PANEL_PAGE_SIZE';

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

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

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

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

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

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

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

  var serializedPrefs = window.localStorage.getItem(storageKey);

  var prefs = null;

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

  return {
    type: PREFS_LOADED,
    payload: prefs
  };
};

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 prefs = (0, _reducers.getPrefs)(getState());

    window.localStorage.setItem(storageKey, JSON.stringify(prefs.toJS()));
  };
};