function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _process$env$TZ_LOG;
function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import { utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';
import * as React from 'react';
import { getPresetDateTimeOptions, intlDateTimeFormat } from '../../_utils/dateTime';
import { useI18nContext } from '../I18n';
var enableLogs = ((_process$env$TZ_LOG = process.env.TZ_LOG) === null || _process$env$TZ_LOG === void 0 ? void 0 : _process$env$TZ_LOG.toLowerCase()) === 'true';
function computerTimeZone() {
  var _window, _window$Intl, _window$Intl$DateTime, _window$Intl$DateTime2, _window$Intl$DateTime3;
  return (_window = window) === null || _window === void 0 ? void 0 : (_window$Intl = _window.Intl) === null || _window$Intl === void 0 ? void 0 : (_window$Intl$DateTime = _window$Intl.DateTimeFormat) === null || _window$Intl$DateTime === void 0 ? void 0 : (_window$Intl$DateTime2 = (_window$Intl$DateTime3 = _window$Intl$DateTime.call(_window$Intl)).resolvedOptions) === null || _window$Intl$DateTime2 === void 0 ? void 0 : _window$Intl$DateTime2.call(_window$Intl$DateTime3).timeZone;
}
function consoleMessageFormat(_ref) {
  var _ref$log = _ref.log,
    log = _ref$log === void 0 ? '' : _ref$log,
    result = _ref.result,
    timeZone = _ref.timeZone,
    type = _ref.type,
    value = _ref.value;
  if (enableLogs) {
    console.log("core-react: ".concat(log, " useDateTime format ").concat(type, "; destintation time zone: ").concat(timeZone, ", argument ").concat(value, ", result ").concat(result));
  }
}
function consoleMessageCompareShift(_ref2) {
  var _ref2$log = _ref2.log,
    log = _ref2$log === void 0 ? '' : _ref2$log,
    result = _ref2.result,
    timeZone = _ref2.timeZone,
    value = _ref2.value;
  if (enableLogs) {
    console.log("core-react: ".concat(log, "; destination time zone ").concat(timeZone, ". Your time zone ").concat(computerTimeZone(), "."), '\nInput:  ', value, '\nOutput: ', result, '\nIn ISO: ', {
      input: value.toISOString(),
      output: result.toISOString()
    });
  }
}
function consoleMessageShiftNoShift(_ref3) {
  var _ref3$log = _ref3.log,
    log = _ref3$log === void 0 ? '' : _ref3$log,
    timeZone = _ref3.timeZone,
    value = _ref3.value;
  if (enableLogs) {
    console.log("core-react: ".concat(log, " utcToZonedTime; destination time zone ").concat(timeZone, ". No argument, return arugment.\n"), value);
  }
}
export var dateTimeFormats = {
  'abbr-weekday-abbr-date': 'abbr-weekday-abbr-date',
  // Fri, Jan 8, 2021
  'abbr-weekday-abbr-date-at-time': 'abbr-weekday-abbr-date-at-time',
  // Fri, Jan 8, 2021 at 8:00 AM PST
  date: 'date',
  // January 8, 2021
  'date-at-time': 'date-at-time',
  // January 8, 2021 at 8:00 AM PST
  'numeric-date': 'numeric-date',
  // 1/8/2021
  'numeric-date-at-time': 'numeric-date-at-time',
  // 8:00 AM PST at 1/8/2021
  time: 'time',
  // 8:00 AM PST
  'time-on-abbr-weekday-abbr-date': 'time-on-abbr-weekday-abbr-date',
  // 8:00 AM PST on Fri, Jan 8, 2021
  'time-on-weekday-date': 'time-on-weekday-date',
  // 8:00 AM PST on Friday, January 8, 2021
  'time-on-date': 'time-on-date',
  // 8:00 AM PST on January 8, 2021
  'time-on-numeric-date': 'time-on-numeric-date',
  // 8:00 AM PST on January 8, 2021
  'weekday-date': 'weekday-date',
  // Friday, January 8, 2021
  'weekday-date-at-time': 'weekday-date-at-time',
  // Friday, January 8, 2021 at 8:00 AM PST
  none: 'none' // browser default 1/8/2021
};

var DateTimeContext = /*#__PURE__*/React.createContext({});
export function DateTimeProvider(_ref4) {
  var children = _ref4.children,
    hour12 = _ref4.hour12,
    timeZone = _ref4.timeZone;
  return /*#__PURE__*/React.createElement(DateTimeContext.Provider, {
    value: {
      hour12: hour12,
      timeZone: timeZone
    }
  }, children);
}
var emptyCache = {};
export function useDateTime() {
  var I18n = useI18nContext();
  var _React$useContext = React.useContext(DateTimeContext),
    hour12 = _React$useContext.hour12,
    timeZone = _React$useContext.timeZone;
  var _React$useState = React.useState(emptyCache),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    cache = _React$useState2[0],
    setCache = _React$useState2[1];
  var getDateFormatter = React.useCallback(function () {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    var key = JSON.stringify(args);
    if (cache[key]) {
      return cache[key];
    }
    var newFormat = intlDateTimeFormat.apply(void 0, args);
    setCache(function (state) {
      return _objectSpread(_objectSpread({}, state), {}, _defineProperty({}, key, newFormat));
    });
    return newFormat;
  }, [cache, setCache]);
  var format = React.useCallback(function (value, type, options, timeOptions, UNSAFE_log) {
    if (type.startsWith('time-on-')) {
      var time = getDateFormatter(I18n.locale, _objectSpread(_objectSpread({}, getPresetDateTimeOptions('time')), {}, {
        hour12: hour12,
        timeZone: timeZone
      }, timeOptions)).format(value);
      var date = getDateFormatter(I18n.locale, _objectSpread(_objectSpread({}, getPresetDateTimeOptions(type.replace('time-on-', ''))), {}, {
        hour12: hour12,
        timeZone: timeZone
      }, options)).format(value);
      var _result = I18n.t('timeOnDate', {
        time: time,
        date: date,
        scope: 'core.dateTimeFormat'
      });
      if (UNSAFE_log) {
        consoleMessageFormat({
          log: UNSAFE_log,
          result: _result,
          timeZone: timeZone,
          type: type,
          value: value
        });
      }
      return _result;
    }
    if (type.endsWith('-at-time')) {
      var _time = getDateFormatter(I18n.locale, _objectSpread(_objectSpread({}, getPresetDateTimeOptions('time')), {}, {
        hour12: hour12,
        timeZone: timeZone
      }, timeOptions)).format(value);
      var _date = getDateFormatter(I18n.locale, _objectSpread(_objectSpread({}, getPresetDateTimeOptions(type.replace('-at-time', ''))), {}, {
        hour12: hour12,
        timeZone: timeZone
      }, options)).format(value);
      var _result2 = I18n.t('dateAtTime', {
        time: _time,
        date: _date,
        scope: 'core.dateTimeFormat'
      });
      if (UNSAFE_log) {
        consoleMessageFormat({
          log: UNSAFE_log,
          result: _result2,
          timeZone: timeZone,
          type: type,
          value: value
        });
      }
      return _result2;
    }
    var result = getDateFormatter(I18n.locale, _objectSpread(_objectSpread({}, getPresetDateTimeOptions(type)), {}, {
      hour12: hour12,
      timeZone: timeZone
    }, options)).format(value);
    if (UNSAFE_log) {
      consoleMessageFormat({
        log: UNSAFE_log,
        timeZone: timeZone,
        type: type,
        result: result,
        value: value
      });
    }
    return result;
  }, [getDateFormatter, I18n.locale, hour12, timeZone]);
  function shiftUtcToZonedTime(value, log) {
    if (value && timeZone) {
      var result = utcToZonedTime(value, timeZone);
      if (log) {
        consoleMessageCompareShift({
          log: "".concat(log, " utcToZonedTime"),
          timeZone: timeZone,
          result: result,
          value: value
        });
      }
      return result;
    }
    if (log) {
      consoleMessageShiftNoShift({
        log: "".concat(log, " utcToZonedTime"),
        timeZone: timeZone,
        value: value
      });
    }
    return value;
  }
  function shiftZonedTimeToUtc(value, log) {
    if (value && timeZone) {
      var result = zonedTimeToUtc(value, timeZone);
      if (log) {
        consoleMessageCompareShift({
          log: "".concat(log, " zonedTimeToUtc"),
          timeZone: timeZone,
          result: result,
          value: value
        });
      }
      return result;
    }
    if (log) {
      consoleMessageShiftNoShift({
        log: "".concat(log, " zonedTimeToUtc"),
        timeZone: timeZone,
        value: value
      });
    }
    return value;
  }
  var newDate = function newDate() {
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }
    return timeZone ? utcToZonedTime(_construct(Date, _toConsumableArray(args)), timeZone) : _construct(Date, _toConsumableArray(args));
  };
  return {
    format: format,
    hour12: hour12,
    newDate: newDate,
    shiftUtcToZonedTime: shiftUtcToZonedTime,
    shiftZonedTimeToUtc: shiftZonedTimeToUtc,
    timeZone: timeZone
  };
}
//# sourceMappingURL=DateTime.js.map