"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

var uid = require("./uniqueId");

function createConversationId() {
  // Save data to sessionStorage
  sessionStorage.setItem('convId', uid.createUniqueId());
}

function getConversationId() {
  // Get saved data from sessionStorage
  var cid = sessionStorage.getItem('convId');
  console.log("Conv Id -", cid);
  return cid;
}

function createTransactionId() {
  var tid = uid.createUniqueId();
  console.log("Trans Id -", tid);
  return tid;
}

function cachePreviousState(data) {
  if (_typeof(data) === 'object') {
    if (getConversationId() === data.convId) {
      var prevData = {};
      prevData.userId = data.userId;
      prevData.transId = data.transId;
      prevData.convId = data.convId;
      prevData.previousIntentSummary = data.previousIntentSummary;
      prevData.sessionAttributes = data.sessionAttributes;
      data = JSON.stringify(prevData);
      sessionStorage.setItem('previousState', data);
    } else {
      console.log("manageSession.js > cachePreviousState - conversation id missmatch");
      throw Error("conversation id missmatch");
    }
  }
}

function getPreviousState(data) {
  var ps = sessionStorage.getItem('previousState', data);
  return JSON.parse(ps);
}

function removeSessionId(key) {
  // Remove saved data from sessionStorage
  sessionStorage.removeItem(key);
}

function clearSession() {
  // Remove all saved data from sessionStorage
  sessionStorage.clear();
}

module.exports = {
  createConversationId: createConversationId,
  getConversationId: getConversationId,
  createTransactionId: createTransactionId,
  cachePreviousState: cachePreviousState,
  getPreviousState: getPreviousState,
  removeSessionId: removeSessionId,
  clearSession: clearSession
};