export default function setRecentItem(_ref) {
  var recent = _ref.value;

  if (recent) {
    var recents = localStorage.envRecentItem ? JSON.parse(localStorage.envRecentItem) : [];
    var recentItem = saveRecent(recents, recent, 10);
    localStorage.envRecentItem = JSON.stringify(recentItem);
  }
}

function saveRecent() {
  var collection = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var value = arguments.length > 1 ? arguments[1] : undefined;
  var number = arguments.length > 2 ? arguments[2] : undefined;
  var index = findDataIndex(collection, value);

  if (index !== -1) {
    collection.splice(index, 1);
  }

  collection.unshift(value);
  return collection.slice(0, number);
}

function findDataIndex(collection, value) {
  return collection ? collection.findIndex(function (_ref2) {
    var id = _ref2.id,
        projectId = _ref2.projectId,
        organizationId = _ref2.organizationId,
        code = _ref2.code;
    return (id === value.id || code === value.code) && String(organizationId) === String(value.organizationId) && String(projectId) === String(value.projectId);
  }) : -1;
}