import { c as _c } from "react/compiler-runtime";
import { memo, useCallback, useMemo, useState } from "react";
import useInterval from "./useInterval";
const nativeDurationFormat =
// @ts-expect-error: Remove this suppression when TS ships with
typeof Intl.DurationFormat === "function" // @ts-expect-error: Remove this suppression when TS ships with Intl.DurationFormat
? new Intl.DurationFormat(["de", "en"], {
  style: "narrow"
}) : undefined;
const absoluteDateFormatter = new Intl.DateTimeFormat(["de", "en"], {
  dateStyle: "long"
});
function formatTimeRemaining(timeLeftSeconds, trimLeadingZeros) {
  const days = timeLeftSeconds / (60 * 60 * 24) | 0;
  const hours = timeLeftSeconds % (60 * 60 * 24) / (60 * 60) | 0;
  const minutes = timeLeftSeconds % (60 * 60) / 60 | 0;
  const seconds = timeLeftSeconds % 60 | 0;

  // Docs:
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat
  const toFormat = {
    days,
    hours,
    minutes,
    seconds
  };
  if (trimLeadingZeros) {
    toFormat.days = days === 0 ? undefined : days;
    toFormat.hours = hours === 0 && toFormat.days === undefined ? undefined : hours;
    toFormat.minutes = minutes === 0 && toFormat.hours === undefined ? undefined : minutes;
  }
  if (!nativeDurationFormat) {
    if (trimLeadingZeros) {
      let res = "";
      if (toFormat.days !== undefined) {
        res += `${toFormat.days}d `;
      }
      if (toFormat.hours !== undefined && toFormat.days !== undefined) {
        res += `${toFormat.hours}h `;
      }
      if (toFormat.minutes !== undefined && toFormat.hours !== undefined) {
        res += `${toFormat.minutes}m `;
      }
      res += `${toFormat.seconds}s`; // seconds are always shown
      return res;
    }
    return `${days}d ${hours}h ${minutes}m ${seconds}s`;
  }
  return nativeDurationFormat.format(toFormat);
}
export default memo(function DateTimeCountdown(props) {
  const $ = _c(18);
  const now = Date.now() / 1000 | 0;
  const targetTimestamp = props.targetTimestamp;
  const [secondsRemaining, setSecondsRemaining] = useState(targetTimestamp - now);
  let t0;
  if ($[0] !== targetTimestamp) {
    t0 = () => {
      setSecondsRemaining(targetTimestamp - Date.now() / 1000 | 0);
    };
    $[0] = targetTimestamp;
    $[1] = t0;
  } else {
    t0 = $[1];
  }
  const cb = t0;
  useInterval(cb, 1000);
  if (secondsRemaining <= 0) {
    switch (props.onZero) {
      case "placeholder":
        {
          return props.placeholder;
        }
      case "hide":
        {
          return;
        }
      case "continue":
        {}
    }
  }
  let t1;
  const t2 = targetTimestamp * 1000;
  let t3;
  let t4;
  if ($[2] !== t2) {
    const targetDateTime = new Date(t2);
    t3 = targetDateTime.toISOString();
    t4 = absoluteDateFormatter.format(targetDateTime);
    $[2] = t2;
    $[3] = t3;
    $[4] = t4;
  } else {
    t3 = $[3];
    t4 = $[4];
  }
  let t5;
  if ($[5] !== t3 || $[6] !== t4) {
    t5 = [t3, t4];
    $[5] = t3;
    $[6] = t4;
    $[7] = t5;
  } else {
    t5 = $[7];
  }
  t1 = t5;
  const [isoString, userReadable] = t1;
  const t6 = `Countdown bis zum ${userReadable}`;
  const t7 = !!props.trimLeadingZeros;
  let t8;
  if ($[8] !== secondsRemaining || $[9] !== t7) {
    t8 = formatTimeRemaining(secondsRemaining, t7);
    $[8] = secondsRemaining;
    $[9] = t7;
    $[10] = t8;
  } else {
    t8 = $[10];
  }
  let t9;
  if ($[11] !== isoString || $[12] !== t6 || $[13] !== t8) {
    t9 = <time dateTime={isoString} title={t6}>{t8}</time>;
    $[11] = isoString;
    $[12] = t6;
    $[13] = t8;
    $[14] = t9;
  } else {
    t9 = $[14];
  }
  const formatted = t9;
  let t10;
  if ($[15] !== formatted || $[16] !== props.prefix) {
    t10 = props.prefix ? <>{props.prefix}{formatted}</> : formatted;
    $[15] = formatted;
    $[16] = props.prefix;
    $[17] = t10;
  } else {
    t10 = $[17];
  }
  return t10;
});
//# sourceMappingURL=DateTimeCountdown.jsx.map