import axios from 'axios';
import uuid from 'react-uuid';
import type { TransactionDebit } from '../interface/post';
import { Sign } from '../utility/encript';
import { validateJwt } from '../hooks/validateJwt';

const getHeaders = (data:TransactionDebit) => {
    const uid = uuid();
    const sign = new Sign(data, "https://api-test.pichincha.com/payment-execution/v2/direct-transfers", uid);
    const XApigeeCredentials = "ZkdlUk5HYXFtMFdKQ2ZtdG85a25hUFNSNWVhRDlBaG86dVNjR2ZkeGg1VlRMZElPeA==";
    const signature = sign.getSignature();
    const headers = {
      "X-Apigee-Credentials": XApigeeCredentials,
      "X-Device-IP": "200.10.89.34",
      "X-Guid": uid,
      "X-Device": "29xp3L6xJCaTSmWsQHsIsIqiyoU7Z0Az+EfYl2Bq5e0=",
      "X-Session": "3e80ddc9-29fb-4c99-b82e-403287fe4ce5",
      "X-Signature": signature,
    }
    return headers;
}
export const sendTransaccion = async (token:string,datas: TransactionDebit) => {
    try{
    const tokenvalidate=await validateJwt(token)
    if(tokenvalidate && tokenvalidate.validate){
    const headers = getHeaders(datas);
    console.log(datas)
    const response=await axios.post("https://api-test.pichincha.com/payment-execution/v2/direct-transfers", datas, {headers: headers});
    return response;
    }
    else{
        throw new Error("Token invalido no se puede realizar la transaccion")
    }
    }
    catch(err:any){
        throw new Error("no es valido")
    }
}

