import React from 'react';
import moment from 'moment';

/**
 * A note on UTC, and the `utc` prop being used for both input intepretation and output format:
 *
 * When using moment.js, it's effectively impossible to force reading something in as UTC and
 * then also have formatting happen in the user's local time.
 *
 * This is because `.utc()` both changes how a timestamp is interpreted and the moment object's
 * formatting rules. Once `.utc()` has been called, the only option is call `.local()`, but that
 * converts to the user's _current_ local offset from UTC. That isn't guaranteed to be the same
 * offset in future or past dates (due to DST) so we risk showing the wrong time.
 *
 * The majority use case for <Timestamp/> is to display dates _in UTC_ anyway, but if it is ever necessary
 * to display a timestamp in user-local time, just pass utc=false and ensure that input timestamps are in
 * ISO 8601 with explicit offsets.
 */

/** Simple component for rendering timestamps in a consistent manner */
import { jsxs as _jsxs } from "react/jsx-runtime";
export default function Timestamp({
  timestamp,
  outputFormat = 'YYYY-MM-DD HH:mm:ss',
  utc = true,
  showTimezoneLabel = utc,
  testId = 'timestamp'
}) {
  const momentInstance = utc ? moment.utc(timestamp) : moment(timestamp);

  // in utc mode with just use a literal, otherwise we just show the offset (e.g. +02:00)
  const timezoneLabel = utc ? 'UTC' : `UTC ${momentInstance.format('Z')}`;
  return /*#__PURE__*/_jsxs("span", {
    "test-id": testId,
    children: [momentInstance.format(outputFormat), showTimezoneLabel && ` (${timezoneLabel})`]
  });
}