/* index.tsx generated by @compiled/babel-plugin v0.40.0 */
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import "./index.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { selectUnit } from '@formatjs/intl-utils';
import { FormattedMessage, useIntl } from 'react-intl';
import { fg } from '@atlaskit/platform-feature-flags';
import { messages } from '../../../../../../messages';
var styles = null;
var ABSOLUTE_DATE_FORMAT = {
  month: 'short',
  day: 'numeric',
  year: 'numeric'
};
var isValidTimeZone = function isValidTimeZone(timeZone) {
  try {
    new Intl.DateTimeFormat(undefined, {
      timeZone: timeZone
    });
    return true;
  } catch (_unused) {
    return false;
  }
};
var formatAbsoluteDateWithOptionalTimeZone = function formatAbsoluteDateWithOptionalTimeZone(_ref) {
  var date = _ref.date,
    formatDate = _ref.formatDate,
    timeZone = _ref.timeZone;
  if (!timeZone || !isValidTimeZone(timeZone)) {
    return formatDate(date, ABSOLUTE_DATE_FORMAT);
  }
  return formatDate(date, _objectSpread(_objectSpread({}, ABSOLUTE_DATE_FORMAT), {}, {
    timeZone: timeZone
  }));
};
var typeToDescriptorMap = {
  created: {
    relative: messages.created_on_relative,
    absolute: messages.created_on_absolute
  },
  modified: {
    relative: messages.modified_on_relative,
    absolute: messages.modified_on_absolute
  },
  sent: {
    relative: messages.sent_on_relative,
    absolute: messages.sent_on_absolute
  }
};
var fontOverrideStyleMap = {
  'font.body': "_11c8fhey",
  'font.body.large': "_11c81d4k",
  'font.body.small': "_11c8wadc"
};
/**
 * A base element that displays an ISO Timestamp in text.
 * @internal
 * @param {BaseDateTimeElementProps} BaseDateTimeElementProps - The props necessary for the DateTime element.
 * @see CreatedOn
 * @see ModifiedOn
 * @see SentOn
 */
var BaseDateTimeElement = function BaseDateTimeElement(_ref2) {
  var date = _ref2.date,
    name = _ref2.name,
    className = _ref2.className,
    type = _ref2.type,
    _ref2$testId = _ref2.testId,
    testId = _ref2$testId === void 0 ? 'smart-element-date-time' : _ref2$testId,
    text = _ref2.text,
    _ref2$hideDatePrefix = _ref2.hideDatePrefix,
    hideDatePrefix = _ref2$hideDatePrefix === void 0 ? false : _ref2$hideDatePrefix,
    color = _ref2.color,
    fontSize = _ref2.fontSize,
    timeZone = _ref2.timeZone;
  var _useIntl = useIntl(),
    formatRelativeTime = _useIntl.formatRelativeTime,
    formatDate = _useIntl.formatDate;
  if (!type || !date) {
    return null;
  }
  var isLongerThenWeek = Math.abs(date.getTime() - Date.now()) > 1000 * 60 * 60 * 24 * 7;
  var context;
  var typeVariant;
  if (isLongerThenWeek) {
    typeVariant = 'absolute';
    context = fg('dfo_issue_view_remote_data_srr_group') ? formatAbsoluteDateWithOptionalTimeZone({
      date: date,
      formatDate: formatDate,
      timeZone: timeZone
    }) : formatDate(date, {
      month: 'short',
      day: 'numeric',
      year: 'numeric'
    });
  } else {
    var _selectUnit = selectUnit(date, Date.now()),
      value = _selectUnit.value,
      unit = _selectUnit.unit;
    typeVariant = 'relative';
    context = formatRelativeTime(value, unit, {
      numeric: 'auto'
    });
  }
  var content;
  if (hideDatePrefix) {
    content = context;
  } else if (text) {
    content = "".concat(text, " ").concat(context);
  } else {
    content = /*#__PURE__*/React.createElement(FormattedMessage, _extends({}, typeToDescriptorMap[type][typeVariant], {
      values: {
        context: context
      }
    }));
  }
  return /*#__PURE__*/React.createElement("span", {
    style: {
      color: color
    },
    "data-separator": true,
    "data-smart-element": name,
    "data-smart-element-date-time": true,
    "data-testid": testId
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop
    ,
    className: ax(["_11c8wadc _1reo15vq _18m915vq _syazazsu _1e0ccj1k _1ul9idpf _1bto1l2s _1nmz1hna _1yyjkb7n _sudp1e54 _102k7vkz", fontSize !== undefined && fontOverrideStyleMap[fontSize], className])
  }, content);
};
export default BaseDateTimeElement;
export var toDateTimeProps = function toDateTimeProps(type, dateString) {
  return dateString ? {
    date: new Date(dateString),
    type: type
  } : undefined;
};