(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
  (global = global || self, factory(global.IntlUnifiedNumberFormat = {}));
}(this, function (exports) { 'use strict';

  /**
   * @name toDate
   * @category Common Helpers
   * @summary Convert the given argument to an instance of Date.
   *
   * @description
   * Convert the given argument to an instance of Date.
   *
   * If the argument is an instance of Date, the function returns its clone.
   *
   * If the argument is a number, it is treated as a timestamp.
   *
   * If the argument is none of the above, the function returns Invalid Date.
   *
   * **Note**: *all* Date arguments passed to any *date-fns* function is processed by `toDate`.
   *
   * @param {Date|Number} argument - the value to convert
   * @returns {Date} the parsed date in the local time zone
   * @throws {TypeError} 1 argument required
   *
   * @example
   * // Clone the date:
   * const result = toDate(new Date(2014, 1, 11, 11, 30, 30))
   * //=> Tue Feb 11 2014 11:30:30
   *
   * @example
   * // Convert the timestamp to date:
   * const result = toDate(1392098430000)
   * //=> Tue Feb 11 2014 11:30:30
   */

  /**
   * @name endOfTomorrow
   * @category Day Helpers
   * @summary Return the end of tomorrow.
   * @pure false
   *
   * @description
   * Return the end of tomorrow.
   *
   * > ⚠️ Please note that this function is not present in the FP submodule as
   * > it uses `Date.now()` internally hence impure and can't be safely curried.
   *
   * ### v2.0.0 breaking changes:
   *
   * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
   *
   * @returns {Date} the end of tomorrow
   *
   * @example
   * // If today is 6 October 2014:
   * var result = endOfTomorrow()
   * //=> Tue Oct 7 2014 23:59:59.999
   */

  /**
   * @name endOfYesterday
   * @category Day Helpers
   * @summary Return the end of yesterday.
   * @pure false
   *
   * @description
   * Return the end of yesterday.
   *
   * > ⚠️ Please note that this function is not present in the FP submodule as
   * > it uses `Date.now()` internally hence impure and can't be safely curried.
   *
   * ### v2.0.0 breaking changes:
   *
   * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
   *
   * @returns {Date} the end of yesterday
   *
   * @example
   * // If today is 6 October 2014:
   * var result = endOfYesterday()
   * //=> Sun Oct 5 2014 23:59:59.999
   */

  /**
   * @name isDate
   * @category Common Helpers
   * @summary Is the given value a date?
   *
   * @description
   * Returns true if the given value is an instance of Date. The function works for dates transferred across iframes.
   *
   * ### v2.0.0 breaking changes:
   *
   * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
   *
   * @param {*} value - the value to check
   * @returns {boolean} true if the given value is a date
   * @throws {TypeError} 1 arguments required
   *
   * @example
   * // For a valid date:
   * var result = isDate(new Date())
   * //=> true
   *
   * @example
   * // For an invalid date:
   * var result = isDate(new Date(NaN))
   * //=> true
   *
   * @example
   * // For some value:
   * var result = isDate('2014-02-31')
   * //=> false
   *
   * @example
   * // For an object:
   * var result = isDate({})
   * //=> false
   */

  /**
   * @name startOfTomorrow
   * @category Day Helpers
   * @summary Return the start of tomorrow.
   * @pure false
   *
   * @description
   * Return the start of tomorrow.
   *
   * > ⚠️ Please note that this function is not present in the FP submodule as
   * > it uses `Date.now()` internally hence impure and can't be safely curried.
   *
   * ### v2.0.0 breaking changes:
   *
   * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
   *
   * @returns {Date} the start of tomorrow
   *
   * @example
   * // If today is 6 October 2014:
   * var result = startOfTomorrow()
   * //=> Tue Oct 7 2014 00:00:00
   */

  /**
   * @name startOfYesterday
   * @category Day Helpers
   * @summary Return the start of yesterday.
   * @pure false
   *
   * @description
   * Return the start of yesterday.
   *
   * > ⚠️ Please note that this function is not present in the FP submodule as
   * > it uses `Date.now()` internally hence impure and can't be safely curried.
   *
   * ### v2.0.0 breaking changes:
   *
   * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
   *
   * @returns {Date} the start of yesterday
   *
   * @example
   * // If today is 6 October 2014:
   * var result = startOfYesterday()
   * //=> Sun Oct 5 2014 00:00:00
   */

  /**
   *  Maximum allowed time.
   *  @constant
   *  @type {number}
   *  @default
   */

  // This file is generated automatically by `scripts/build/indices.js`. Please, don't change it.

  var __assign = (undefined && undefined.__assign) || function () {
      __assign = Object.assign || function(t) {
          for (var s, i = 1, n = arguments.length; i < n; i++) {
              s = arguments[i];
              for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                  t[p] = s[p];
          }
          return t;
      };
      return __assign.apply(this, arguments);
  };

  function resolveSupportedLocales(locales, localeData, aliases) {
      if (aliases === void 0) { aliases = {}; }
      var resolvedLocales = (Array.isArray(locales) ? locales : [locales])
          .filter(function (s) { return typeof s === 'string'; })
          .map(function (l) { return aliases[l] || l; });
      var i, len, localeParts, data;
      var supportedLocales = [];
      // Using the set of locales + the default locale, we look for the first one
      // which that has been registered. When data does not exist for a locale, we
      // traverse its ancestors to find something that's been registered within
      // its hierarchy of locales. Since we lack the proper `parentLocale` data
      // here, we must take a naive approach to traversal.
      for (i = 0, len = resolvedLocales.length; i < len; i += 1) {
          localeParts = resolvedLocales[i].toLowerCase().split('-');
          while (localeParts.length) {
              if (localeData) {
                  data = localeData[localeParts.join('-')];
                  if (data) {
                      // Return the normalized locale string; e.g., we return "en-US",
                      // instead of "en-us".
                      supportedLocales.push(data.locale);
                      break;
                  }
                  localeParts.pop();
              }
          }
      }
      return supportedLocales;
  }

  var __assign$1 = (undefined && undefined.__assign) || function () {
      __assign$1 = Object.assign || function(t) {
          for (var s, i = 1, n = arguments.length; i < n; i++) {
              s = arguments[i];
              for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                  t[p] = s[p];
          }
          return t;
      };
      return __assign$1.apply(this, arguments);
  };
  var __rest = (undefined && undefined.__rest) || function (s, e) {
      var t = {};
      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
          t[p] = s[p];
      if (s != null && typeof Object.getOwnPropertySymbols === "function")
          for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
              t[p[i]] = s[p[i]];
      return t;
  };
  function isUnitSupported(unit) {
      try {
          new Intl.NumberFormat(undefined, {
              style: 'unit',
              unit: unit,
          });
      }
      catch (e) {
          return false;
      }
      return true;
  }
  var NativeNumberFormat = Intl.NumberFormat;
  function findUnitData(locale, unit) {
      var data = UnifiedNumberFormat.__unitLocaleData__;
      var parentLocale = '';
      locale = locale.toLowerCase();
      if (!data[locale]) {
          parentLocale = locale.split('-')[0];
      }
      else {
          if (data[locale].units[unit]) {
              return data[locale].units[unit];
          }
          if (data[locale].parentLocale) {
              parentLocale = data[locale].parentLocale;
          }
          else {
              throw new RangeError("Cannot find data for " + locale);
          }
      }
      return findUnitData(parentLocale, unit);
  }
  function intersection(arr1, arr2) {
      return arr1.filter(function (s) { return ~arr2.indexOf(s); });
  }
  var DEFAULT_LOCALE = new NativeNumberFormat().resolvedOptions().locale;
  var UnifiedNumberFormat = /** @class */ (function () {
      function UnifiedNumberFormat(locales, _a) {
          if (_a === void 0) { _a = {}; }
          var style = _a.style, unit = _a.unit, unitDisplay = _a.unitDisplay, options = __rest(_a, ["style", "unit", "unitDisplay"]);
          this.unit = undefined;
          this.unitDisplay = undefined;
          if (style === 'unit') {
              if (!unit) {
                  throw new TypeError('Unit is required for `style: unit`');
              }
              this.unit = unit;
              this.unitDisplay = unitDisplay || 'short';
              var resolvedLocale = resolveSupportedLocales(intersection(NativeNumberFormat.supportedLocalesOf(locales), Intl.PluralRules.supportedLocalesOf(locales)).concat([
                  DEFAULT_LOCALE,
              ]), UnifiedNumberFormat.__unitLocaleData__)[0];
              this.patternData = findUnitData(resolvedLocale, this.unit);
          }
          this.nf = new NativeNumberFormat(locales, __assign$1({}, options, { style: style === 'unit' ? 'decimal' : style }));
          this.locale = this.nf.resolvedOptions().locale;
      }
      UnifiedNumberFormat.prototype.format = function (num) {
          var formattedNum = this.nf.format(num);
          if (this.patternData) {
              var pl = new Intl.PluralRules(this.locale).select(num);
              var pattern = this.patternData[this.unitDisplay][pl === 'one' ? 'one' : 'other'];
              return pattern.replace('{0}', formattedNum);
          }
          return formattedNum;
      };
      UnifiedNumberFormat.prototype.formatToParts = function (num) {
          return this.nf.formatToParts(num);
      };
      UnifiedNumberFormat.prototype.resolvedOptions = function () {
          var ro = this.nf.resolvedOptions();
          if (this.unit) {
              ro.style = 'unit';
              ro.unit = this.unit;
              ro.unitDisplay = this.unitDisplay;
          }
          return ro;
      };
      UnifiedNumberFormat.supportedLocalesOf = function () {
          var args = [];
          for (var _i = 0; _i < arguments.length; _i++) {
              args[_i] = arguments[_i];
          }
          return resolveSupportedLocales(args[0], UnifiedNumberFormat.__unitLocaleData__);
      };
      UnifiedNumberFormat.__addUnitLocaleData = function (data) {
          Object.keys(data).forEach(function (locale) {
              var datum = data[locale];
              if (!(datum && datum.locale)) {
                  throw new Error('Locale data provided to UnifiedNumberFormat is missing a ' +
                      '`locale` property value');
              }
              UnifiedNumberFormat.__unitLocaleData__[datum.locale.toLowerCase()] = datum;
          });
      };
      UnifiedNumberFormat.polyfilled = true;
      UnifiedNumberFormat.__unitLocaleData__ = {};
      return UnifiedNumberFormat;
  }());

  exports.UnifiedNumberFormat = UnifiedNumberFormat;
  exports.isUnitSupported = isUnitSupported;

  Object.defineProperty(exports, '__esModule', { value: true });

}));
//# sourceMappingURL=intl-unified-numberformat.js.map
