import { handleError } from ".";
import axios from "../lib/axios";
import { IConfig } from "../system/type";
import { removeFalsyValues } from "../utils";

export const CONFIG_KEYS = {
  MAIL_SCRAP_REPORT: "MAIL_SCRAP_REPORT",
};

export const getConfig = async (key: keyof typeof CONFIG_KEYS) => {
  return await axios({
    url: "configs/" + key,
    withCredentials: true,
    method: "GET",
  });
};

export const upsertConfig = async (data: IConfig) => {
  const { key_name, value, type } = removeFalsyValues(data, ["value"]);

  try {
    const { data } = await axios({
      url: "configs/upsert",
      withCredentials: true,
      method: "POST",
      data: { key_name, value: value || null, type },
    });

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