//
//function promiseSqrt(){
//    return new Promise(function (fulfill, reject){
//        setMenu();
//    });
//}

let getToken=()=>{
   let authorization;
   let auth = window.location.search;  
   if(auth!==null && auth.includes('%20')){
       authorization =auth.split('%20');
   }else{
       authorization[1] ="";
   }
   return authorization[1];
}
const httpRequest = {
  getNewToken() {
    return ajaxRequestGeneral({
      url: '../RedireccionarServlet',
      type: 'POST',
      headers: {
        "Authorization":"Bearer "+getToken()
      }
    })
  }
}
let obtenerToken=()=>{
    httpRequest.getNewToken()
        .then(data => {
          if (data.status) {
            delete data["status"]
            Cookies.set('Authorization', `Bearer ${data.token}`, {expiry: 36000000})
            Cookies.set('menu', `${JSON.stringify(data.menu)}`, {expiry: 36000000})
            console.log("MENU main.js"+data.menu);
//            window.history.replaceState( {} , '', 'main.jsp' );
            window.location.href = "main.jsp";
          } else {
            alert('No tienes autorización')
          }
          console.log(data)
        })
//                .then(()=>{
//            promiseSqrt();
//        })
        .catch(err => {
            console.log(err)
//            window.location.href = "index.jsp";
        })
    
}


//let getNewToken = () =>{
//    let auth = window.location.search; 
//    console.log("auth"+auth);
//      if(auth!==null && auth.includes('%20')){
//        let authorization =auth.split('%20');
//        console.log("TOKEN+ "+authorization[1])
//        $.ajax({
//            url: '../RedireccionarServlet',
//            type: 'POST',
//            data: {
//            },
//            headers: {
//                "Authorization":"Bearer "+authorization[1],
//                "Accept": 'application/json'
//            },
//            dataType: 'json',
//            success: function (data) {
//                Cookies.set('Authorization', `Bearer ${data.token}`, {expiry: 36000000})
//                Cookies.set('menu', `${JSON.stringify(data.menu)}`, {expiry: 36000000})
//                console.log("MENU main.js"+data.menu);
//                
//            },
//            error : function(xhr, errmsg, err) {
//                console.log(xhr.status + ": " + xhr.responseText);
//            }
//        });
//      }   
//    window.history.replaceState( {} , '', 'main.jsp' );
//}
//
//getNewToken();

//obtenerToken();