function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
var _process$env$TZ_LOG;
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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