'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

exports.getFromStorage = getFromStorage;
exports.saveToStorage = saveToStorage;
exports.saveObjToStorage = saveObjToStorage;
exports.getSettings = getSettings;

var _chromeStorageLocal = require('chrome-storage-local');

var _chromeStorageLocal2 = _interopRequireDefault(_chromeStorageLocal);

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

function getFromStorage(key) {
  if (_chromeStorageLocal2.default.isChromeStorage) return null;
  return _chromeStorageLocal2.default.getItem(key);
}

function saveToStorage(key, value) {
  var sValue = value;
  if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && !_chromeStorageLocal2.default.isChromeStorage) {
    sValue = JSON.stringify(value);
  }
  _chromeStorageLocal2.default.setItem(key, sValue);
  return value;
}

function saveObjToStorage(remove, keys, values) {
  if (remove) {
    keys.forEach(function (key) {
      _chromeStorageLocal2.default.removeItem('s:' + key);
    });
    return null;
  }

  var obj = {};
  keys.forEach(function (key) {
    _chromeStorageLocal2.default.setItem('s:' + key, values[key]);
    obj[key] = values[key];
  });
  return obj;
}

function getSettings() {
  if (!_chromeStorageLocal2.default.isChromeStorage) {
    var hostname = _chromeStorageLocal2.default.getItem('s:hostname');
    var port = _chromeStorageLocal2.default.getItem('s:port');
    var secure = _chromeStorageLocal2.default.getItem('s:secure');
    secure = secure === 'true';
    if (hostname && port) return { hostname: hostname, port: Number(port), secure: secure };
  }
  return null;
}