import { GlobalFunctions, TransferAreaProperties } from "@/GlobalFunctions";
import axios from "axios";
const endPoint: string = 'v1/login/';

export class RedirectService{
    
    public static loadTransferArea(gsikey: number): Promise<any>{
        return axios.post(`${import.meta.env.VITE_BASE_URL_OPEN_CODEX_APIs}${endPoint}login_by_gsikey`,{
            "gsikey" : gsikey
        });    
    }

    public static async validaAcessoApp(urlDestino: string): Promise<any>{
        //let token = await GlobalFunctions.getToken();
        let usuLogado = GlobalFunctions.getUsuarioLogado();
        let usuCod: string = GlobalFunctions.getTransferProperty(TransferAreaProperties.UsuarioCodigo);
        let estCod: string = usuLogado.EID;

        let urlDestinoPost: string = urlDestino;//urlDestino.trim().length < 1 ? GlobalFunctions.getTransferProperty(TransferAreaProperties.urlDestino) : urlDestino;
        if(urlDestinoPost.trim().length < 1){
            try {
                let pathSplit = window.location.pathname.split('/');

                urlDestinoPost = `/${pathSplit[pathSplit.length-2]}/${pathSplit[pathSplit.length-1]}`;
            } catch (error) {
            }
        }
        //const heads = { headers: {'Content-Type':'application/json; charset=utf-8','Authorization': `Bearer ${token}`} };
        const heads = { headers: {'Content-Type':'application/json; charset=utf-8'} };
        return axios.post(`${import.meta.env.VITE_BASE_URL_HGTX_CORE_API}v1/aplicativos/validar_acesso_app?usuCod=${usuCod}&estCod=${estCod}`,{
            "urlDestino": urlDestinoPost
        },heads);    
    }    
}
