import ApiConnect from "../ApiConfig";

//Get All vendors

export const get_all_products = async () => {
  // console.log(data)
  try {
    // if (!data.id) {
    //   throw new Error("data.id is empty or undefined");
    // }
    const result = await ApiConnect(`products`, {
      method: "GET",
    });
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed get all products api");

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

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

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

    return error;
  }
};
//CREATE product
export const create_product = async (data) => {
  // console.log(data)
  try {
    const result = await ApiConnect(`products`, {
      method: "POST",
      data: data.body,
      //   headers: {
      //     Authorization: `Bearer ${data.token}`,
      //   },
    });
    console.log("created");
    return result;
  } catch (error) {
    console.log(error);
    console.log(error + "failed create product api");

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