import { generateNestParams, handleError, handleSuccess } from ".";
import axios from "../lib/axios";
import { IBid, IMetadata, IWebBid } from "../system/type";
import { removeFalsyValues } from "../utils";

export const getBids = async (params: Record<string, string | number>) => {
  return await axios({
    url: "bids",
    params: generateNestParams(params),
    withCredentials: true,
    method: "GET",
  });
};

export const createBid = async (
  bid: Omit<IBid, "id" | "created_at" | "updated_at" | "is_system_account">
) => {
  const newData = removeFalsyValues(bid);

  try {
    const { data } = await axios({
      url: "bids",
      withCredentials: true,
      method: "POST",
      data: newData,
    });

    handleSuccess(data);

    return data;
  } catch (error) {
    handleError(error);
  }
};

export const updateBid = async (bid: Partial<IBid>) => {
  const { plus_price, max_price, quantity, metadata } = removeFalsyValues(bid, [
    "plus_price",
  ]);

  try {
    const { data } = await axios({
      url: "bids/" + bid.id,
      withCredentials: true,
      method: "PUT",
      data: { plus_price, max_price, quantity, metadata },
    });

    handleSuccess(data);

    return data;
  } catch (error) {
    handleError(error);
  }
};

export const toggleBid = async (bid: Partial<IBid>) => {
  try {
    const { data } = await axios({
      url: "bids/toggle/" + bid.id,
      withCredentials: true,
      method: "POST",
    });

    handleSuccess(data);

    return data;
  } catch (error) {
    handleError(error);
  }
};

export const deleteBid = async (bid: IBid) => {
  try {
    const { data } = await axios({
      url: "bids/" + bid.id,
      withCredentials: true,
      method: "DELETE",
    });

    handleSuccess(data);

    return data;
  } catch (error) {
    handleError(error);
  }
};

export const deletesBid = async (bids: IBid[]) => {
  const ids = bids.reduce((prev, cur) => {
    prev.push(cur.id);
    return prev;
  }, [] as number[]);
  try {
    const { data } = await axios({
      url: "bids/deletes",
      withCredentials: true,
      method: "POST",
      data: {
        ids,
      },
    });

    handleSuccess(data);

    return data;
  } catch (error) {
    handleError(error);
  }
};

export const getImagesWorking = async (
  values: (IBid | IWebBid) & { type: string }
) => {
  try {
    const { data } = await axios({
      url: `bids/images-working/${values.type
        .toLocaleLowerCase()
        .replace("_", "-")}/${values.id}`,
      withCredentials: true,
      method: "GET",
    });

    return data;
  } catch (error) {
    console.log("%csrc/apis/bid.ts:118 error", "color: #007acc;", error);
  }
};

export const getRecords = async (values: IBid) => {
  try {
    const { data } = await axios({
      url: `bids/records/${values.id}`,
      withCredentials: true,
      method: "GET",
    });

    return data;
  } catch (error) {
    console.log("%csrc/apis/bid.ts:118 error", "color: #007acc;", error);
  }
};

export const deleteRecord = async (name: string) => {
  try {
    const { data } = await axios({
      url: `bids/record/${name}`,
      withCredentials: true,
      method: "DELETE",
    });

    return data;
  } catch (error) {
    console.log("%csrc/apis/bid.ts:154 error", "color: #007acc;", error);
  }
};

export const deleteMetadata = async (id: IMetadata["id"]) => {
  try {
    const { data } = await axios({
      url: `bid-metadatas/${id}`,
      withCredentials: true,
      method: "DELETE",
    });

    return data;
  } catch (error) {
    console.log("%csrc/apis/bid.ts:154 error", "color: #007acc;", error);
  }
};

export const updateMetadata = async (id: IMetadata["id"], value: string) => {
  try {
    const { data } = await axios({
      url: `bid-metadatas/${id}`,
      withCredentials: true,
      method: "PUT",
      data: {
        value,
      },
    });

    return data;
  } catch (error) {
    console.log("%csrc/apis/bid.ts:154 error", "color: #007acc;", error);
  }
};
