import { Granularity } from "../types";

export const formatDate = (
  date: Date,
  granularity: Granularity,
  variant: "short" | "long" = "short",
  locale: string = "sv-SE",
) => {
  let options: Intl.DateTimeFormatOptions = {};
  if (granularity === "day") {
    options = {
      day: "numeric",
      month: "long",
      ...(variant === "long" && { year: "numeric" }),
    };
  } else if (granularity === "month") {
    options = {
      month: "long",
      year: "numeric",
    };
  } else if (granularity === "year") {
    options = {
      year: "numeric",
    };
  }

  return new Intl.DateTimeFormat(locale, options).format(date);
};
