export function sanitizeTranslationKeyInput(
  value: string,
): string {
  return value
    .toLowerCase()
    .replace(/\s+/g, ".")
    .replace(
      /[^a-z0-9._-]/g,
      "",
    )
    .replace(/\.{2,}/g, ".");
}

export function normalizeTranslationKey(
  value: string,
): string {
  return sanitizeTranslationKeyInput(
    value,
  ).replace(
    /^[._-]+|[._-]+$/g,
    "",
  );
}

export function sanitizePageInput(
  value: string,
): string {
  return value
    .toLowerCase()
    .replace(/\s+/g, "_")
    .replace(
      /[^a-z0-9_-]/g,
      "",
    )
    .replace(/_{2,}/g, "_")
    .replace(/-{2,}/g, "-");
}

export function normalizePage(
  value: string,
): string {
  return sanitizePageInput(
    value,
  ).replace(
    /^[_-]+|[_-]+$/g,
    "",
  );
}

export function formatTranslationDateTime(
  value:
    | string
    | null
    | undefined,
  locale: "en" | "km",
): string {
  if (!value) {
    return "-";
  }

  const date = new Date(value);

  if (
    Number.isNaN(
      date.getTime(),
    )
  ) {
    return value;
  }

  return new Intl.DateTimeFormat(
    locale === "km"
      ? "km-KH"
      : "en-GB",
    {
      year: "numeric",
      month: "short",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
    },
  ).format(date);
}
