import AsyncStorage from "@react-native-async-storage/async-storage";
import ApiConnect from "../ApiConfig";

var token = "";
//Get All vendors
function getToken() {
  AsyncStorage.getItem("AccessToken")
    .then((pass) => {
      if (pass) {
        token = pass;
      }
    })
    .catch((error) => {
      console.log(error + "weeewwww");
    });
}
export const get_all_orders = async () => {
  // console.log(data)
  try {
    // if (!data.id) {
    //   throw new Error("data.id is empty or undefined");
    // }
    const result = await ApiConnect(`orders`, {
      method: "GET",
    });
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed get all orders api");

    return error;
  }
};
//Get a notification
export const get_order = async (data) => {
  // console.log(data)
  try {
    if (!data.id) {
      throw new Error("data.id is empty or undefined");
    }
    const result = await ApiConnect(`orders/${data.id}`, {
      method: "GET",
      //   headers: {
      //     Authorization: `Bearer ${data.token}`,
      //   },
    });
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed get order api");

    return error;
  }
};
export const get_orders_by_customer = async (data) => {
  // console.log(data)
  try {
    if (!data.id) {
      throw new Error("data.id is empty or undefined");
    }
    const result = await ApiConnect(`orders/customer/${data.id}`, {
      method: "GET",

      //   headers: {
      //     Authorization: `Bearer ${data.token}`,
      //   },
    });
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed get order apii");

    return error;
  }
};
//CREATE order
export const create_order = async (data) => {
  // console.log(data)
  // getToken();

  try {
    // var token2 = "";
    // AsyncStorage.getItem("AccessToken")
    //   .then((pass) => {
    //     if (pass) {
    //       token2 = pass;
    //     }
    //   })
    //   .catch((error) => {
    //     console.log(error + "weeewwww");
    //   });
    // // if (token2) {
    //   console.log(token2 + "weeewwww===============");

    const result = await ApiConnect(`orders`, {
      method: "POST",
      data: data.body,
      headers: {
        Authorization: `Bearer ${data.token}`,
      },
    });
    console.log("created");

    return result;
    // }
  } catch (error) {
    console.log(token);
    console.log(error);
    console.log(error + "failed create order api");

    return error;
  }
};
export const update_order = async (data) => {
  // console.log(data)
  getToken();
  try {
    if (!data.id) {
      throw new Error("data.id is empty or undefined");
    }
    const result = await ApiConnect(`orders/${data.id}`, {
      method: "PATCH",
      data: data.body,
      headers: {
        Authorization: `Bearer ${data.token}`,
      },
    });
    return result;
  } catch (error) {
    console.log(error + "failed update order api");
    return error;
  }
};
export const update_order_item = async (data) => {
  console.log(data.body.quantity);
  getToken();

  try {
    if (!data.orderId && data.itemId) {
      throw new Error("data.id is empty or undefined");
    }
    const result = await ApiConnect(
      `orders/${data.orderId}/items/${data.itemId}`,
      {
        method: "PUT",
        data: data.body,
        headers: {
          Authorization: `Bearer ${data.token}`,
        },
      }
    );
    return result;
  } catch (error) {
    console.log(error + "failed update order api");
    return error;
  }
};
export const update_order_item_by_reference_number = async (data) => {
  // console.log(data.body.quantity)
  getToken();

  try {
    if (!data.refId) {
      throw new Error("data.id is empty or undefined");
    }
    const result = await ApiConnect(
      `orders/updateBypaymentReference/${data.refId}`,
      {
        method: "PUT",
        data: data.body,
        headers: {
          Authorization: `Bearer ${data. token}`,
        },
      }
    );
    return result;
  } catch (error) {
    console.log(error + "failed update order api");
    return error;
  }
};
export const webhook = async (data) => {
  // console.log(data)
  try {
    // if (!data.id) {
    //   throw new Error("data.id is empty or undefined");
    // }
    const result = await ApiConnect(`orders/paymongo`, {
      method: "POST",
      data: data.body,
      //   headers: {
      //     Authorization: `Bearer ${data.token}`,
      //   },
    });
    return result;
  } catch (error) {
    console.log(error + "failed update order api");
    return error;
  }
};
export const delete_order = async (data) => {
  getToken();

  // console.log(data)
  try {
    if (!data.id) {
      throw new Error("data.id is empty or undefined");
    }
    const result = await ApiConnect(`orders/${data.id}`, {
      method: "DELETE",

      //   headers: {
      //     Authorization: `Bearer ${data.token}`,
      //   },
    });
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed delete order api");

    return error;
  }
};
