import { DateTime } from 'luxon';
import { br as useState, b9 as useI18n } from './server.mjs';

function useTime() {
  const settings = useState("settings");
  const { locale } = useI18n();
  function dt(input) {
    let date;
    if (typeof input === "number") {
      date = DateTime.fromSeconds(input);
    } else if (typeof input === "string") {
      date = DateTime.fromISO(input);
    } else {
      date = DateTime.local();
    }
    return date.setZone(settings.value.instance_timezone).setLocale(locale.value || settings.value.instance_locale);
  }
  function currentYear() {
    return dt().year;
  }
  function currentMonth() {
    return dt().month;
  }
  function startMonth(year, month) {
    const date = year && month ? dt().set({ year, month }) : dt();
    return date.startOf("month").toUnixInteger();
  }
  function nowUnix() {
    return dt().toUnixInteger();
  }
  function time(datetime) {
    return dt(datetime).toString();
  }
  function unixFormat(timestamp, format2 = "EEEE d MMMM HH:mm") {
    return dt(timestamp).toFormat(format2);
  }
  function format(date, formatStr = "EEEE d MMMM HH:mm") {
    return dt(date).toFormat(formatStr);
  }
  function from(timestamp) {
    return dt(timestamp).toRelative();
  }
  function when(event) {
    const cy = currentYear();
    const addTimezone = event?.ap_id || event?.place?.name === "online";
    const start = dt(event?.start_datetime ?? 0);
    let timeStr = start.toLocaleString({
      weekday: "long",
      ...cy !== start.year && { year: "numeric" },
      month: "short",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
      ...addTimezone && { timeZoneName: "short" }
    });
    if (event?.end_datetime) {
      const end = dt(event.end_datetime);
      const isMultiDate = !end.hasSame(start, "day");
      if (isMultiDate) {
        timeStr += ` \u2192 ${end.toLocaleString({
          weekday: "long",
          ...cy !== end.year && { year: "numeric" },
          month: "short",
          day: "2-digit"
        })}`;
      } else if (end.diff(start, "minutes").minutes > 0) {
        timeStr += `-${end.toLocaleString({ hour: "2-digit", minute: "2-digit" })}`;
      }
    }
    return timeStr;
  }
  return {
    currentYear,
    currentMonth,
    startMonth,
    nowUnix,
    time,
    unixFormat,
    format,
    from,
    when
  };
}

export { useTime as u };
