import ext from "./utils/ext";
import storage from "./utils/storage";

ext.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if(request.action === "perform-save") {
      var data = JSON.parse(request.data);
      var url;
      var token;
      storage.get('url', function(resp) {
        url = resp.url;
        storage.get('token', function(resp) {
          token = resp.token;
          console.log("Extension Type: ", "/* @echo extension */");
          console.log("PERFORM AJAX", request.data);
          var destination = url+"?v=1&u="+encodeURIComponent(data.url)+"&t="+encodeURIComponent(data.title)+"&m="+encodeURIComponent(token);
    
          console.log("Destination: ", destination);
          fetch(destination)
          .then(function(response) {
            console.log(response);
          });
          
          sendResponse({ action: "saved" });
        });
      });
      
      
    }
  }
);