import React, { useEffect, useState } from "react";

function usePaypal(call: boolean = true) {
  const domain = "http://localhost:3001";
  const api = `${domain}`;
  const [clientToken, setClientToken] = useState(null);

  useEffect(() => {
    call && generateToken();
  }, []);
  const generateToken = async () => {
    const response = await (
      await fetch(`${api}/v1/identity/generate-token`, {
        method: "POST",
      })
    ).json();

    setClientToken(response?.client_token || response?.clientToken);
  };
  const createOrder = async () => {
    const response = await (
      await fetch(`${api}/v2/checkout/orders`, {
        method: "POST",
      })
    ).json();
    return response;
  };
  const captureOrder = async (orderId: string) => {
    const response = await (
      await fetch(`${api}/v2/checkout/orders/${orderId}/capture`, {
        method: "POST",
      })
    ).json();
    return response;
  };
  const authToken = async () => {
    const response = await (
      await fetch(api, {
        method: "POST",
      })
    ).json();
    return response;
  };
  return { clientToken, generateToken, createOrder, captureOrder, authToken };
}

export default usePaypal;
