"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

function Storage(namespace) {
  var argument = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {};
  this.MAX_SIZE = argument.max || 128;
  this.namespace = namespace;
  this.list = [];
  localStorage && (this.list = this.init());
}

Storage.prototype.init = function () {
  try {
    return JSON.parse(localStorage.getItem(this.namespace) || "[]");
  } catch (e) {
    return [];
  }
};

Storage.prototype.get = function (key) {
  return this.list.find(function (item) {
    return item.key === key;
  });
};

Storage.prototype.set = function (key, value) {
  var data = {
    key: key,
    data: value
  };

  if (this.list.length < this.MAX_SIZE) {
    this.list.push(data);
  } else {
    var size = ~~(Math.random() * this.MAX_SIZE);
    this.list[size] = data;
  }

  try {
    localStorage.setItem(this.namespace, JSON.stringify(this.list));
  } catch (e) {
    console.warn(e);
  }
};

Storage.prototype.clear = function () {
  return localStorage.removeItem(this.namespace);
};

var _default = Storage;
exports["default"] = _default;