/**
 * Independent time operation tool to facilitate subsequent switch to dayjs https://dayjs.fenxianglu.cn
 */
import dayjs from "dayjs";

const DATE_TIME_FORMAT = "YYYY-MM-DD HH:mm:ss";
const DATE_FORMAT = "YYYY-MM-DD";

export function formatToDateTime({
  type,
  unit,
  date,
  format = DATE_TIME_FORMAT
}: {
  type?: string;
  unit?: dayjs.ManipulateType;
  date?: dayjs.ConfigType;
  format?: string;
}): string {
  if (type == "start" && unit) {
    return dayjs(date).startOf(unit).format(format);
  }
  if (type == "end" && unit) {
    return dayjs(date).endOf(unit).format(format);
  }
  return dayjs(date).format(format);
}

export function formatToDate(date?: dayjs.ConfigType, format = DATE_FORMAT): string {
  return dayjs(date).format(format);
}

export const dateUtil = dayjs;
