text: /**
   * @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
   */
  'use strict';
  
  /* globals self */
  
  // Not named `NBSP` because that creates a duplicate identifier (util.js).
  const NBSP2 = '\xa0';
  
  class I18n {
    /**
     * @param {LH.Locale} locale
     * @param {LH.I18NRendererStrings=} strings
     */
    constructor(locale, strings) {
      // When testing, use a locale with more exciting numeric formatting.
      if (locale === 'en-XA') locale = 'de';
  
      this._numberDateLocale = locale;
      this._numberFormatter = new Intl.NumberFormat(locale);
      this._strings = /** @type {LH.I18NRendererStrings} */ (strings || {});
    }
  
    get strings() {
      return this._strings;
    }
  
    /**
     * Format number.
     * @param {number} number
     * @param {number=} granularity Number of decimal places to include. Defaults to 0.1.
     * @return {string}
     */
    formatNumber(number, granularity = 0.1) {
      const coarseValue = Math.round(number / granularity) * granularity;
      return this._numberFormatter.format(coarseValue);
    }
  
    /**
     * @param {number} size
     * @param {number=} granularity Controls how coarse the displayed value is, defaults to 0.1
     * @return {string}
     */
    formatBytesToKiB(size, granularity = 0.1) {
      const formatter = this._byteFormatterForGranularity(granularity);
      const kbs = formatter.format(Math.round(size / 1024 / granularity) * granularity);
      return `${kbs}${NBSP2}KiB`;
    }
  
    /**
     * @param {number} size
     * @param {number=} granularity Controls how coarse the displayed value is, defaults to 1
     * @return {string}
     */
    formatBytes(size, granularity = 1) {
      const formatter = this._byteFormatterForGranularity(granularity);
      const kbs = formatter.format(Math.round(size / granularity) * granularity);
      return `${kbs}${NBSP2}bytes`;
    }
  
    /**
     * Format bytes with a constant number of fractional digits, i.e for a granularity of 0.1, 10 becomes '10.0'
     * @param {number} granularity Controls how coarse the displayed value is
     * @return {Intl.NumberFormat}
     */
    _byteFormatterForGranularity(granularity) {
      // assume any granularity above 1 will not contain fractional parts, i.e. will never be 1.5
      let numberOfFractionDigits = 0;
      if (granularity < 1) {
        numberOfFractionDigits = -Math.floor(Math.log10(granularity));
      }
  
      return new Intl.NumberFormat(this._numberDateLocale, {
        ...this._numberFormatter.resolvedOptions(),
        maximumFractionDigits: numberOfFractionDigits,
        minimumFractionDigits: numberOfFractionDigits,
      });
    }
  
    /**
     * @param {number} ms
     * @param {number=} granularity Controls how coarse the displayed value is, defaults to 10
     * @return {string}
     */
    formatMilliseconds(ms, granularity = 10) {
      const coarseTime = Math.round(ms / granularity) * granularity;
      return coarseTime === 0
        ? `${this._numberFormatter.format(0)}${NBSP2}ms`
        : `${this._numberFormatter.format(coarseTime)}${NBSP2}ms`;
    }
  
    /**
     * @param {number} ms
     * @param {number=} granularity Controls how coarse the displayed value is, defaults to 0.1
     * @return {string}
     */
    formatSeconds(ms, granularity = 0.1) {
      const coarseTime = Math.round(ms / 1000 / granularity) * granularity;
      return `${this._numberFormatter.format(coarseTime)}${NBSP2}s`;
    }
  
    /**
     * Format time.
     * @param {string} date
     * @return {string}
     */
    formatDateTime(date) {
      /** @type {Intl.DateTimeFormatOptions} */
      const options = {
        month: 'short', day: 'numeric', year: 'numeric',
        hour: 'numeric', minute: 'numeric', timeZoneName: 'short',
      };
  
      // Force UTC if runtime timezone could not be detected.
      // See https://github.com/GoogleChrome/lighthouse/issues/1056
      // and https://github.com/GoogleChrome/lighthouse/pull/9822
      let formatter;
      try {
        formatter = new Intl.DateTimeFormat(this._numberDateLocale, options);
      } catch (err) {
        options.timeZone = 'UTC';
        formatter = new Intl.DateTimeFormat(this._numberDateLocale, options);
      }
  
      return formatter.format(new Date(date));
    }
  
    /**
     * Converts a time in milliseconds into a duration string, i.e. `1d 2h 13m 52s`
     * @param {number} timeInMilliseconds
     * @return {string}
     */
    formatDuration(timeInMilliseconds) {
      let timeInSeconds = timeInMilliseconds / 1000;
      if (Math.round(timeInSeconds) === 0) {
        return 'None';
      }
  
      /** @type {Array<string>} */
      const parts = [];
      /** @type {Record<string, number>} */
      const unitLabels = {
        d: 60 * 60 * 24,
        h: 60 * 60,
        m: 60,
        s: 1,
      };
  
      Object.keys(unitLabels).forEach(label => {
        const unit = unitLabels[label];
        const numberOfUnits = Math.floor(timeInSeconds / unit);
        if (numberOfUnits > 0) {
          timeInSeconds -= numberOfUnits * unit;
          parts.push(`${numberOfUnits}\xa0${label}`);
        }
      });
  
      return parts.join(' ');
    }
  }
  
  if (typeof module !== 'undefined' && module.exports) {
    module.exports = I18n;
  } else {
    self.I18n = I18n;
  }
  