import { Component, Prop } from '@stencil/core';
export class ScFormatBytes {
  constructor() {
    /** The number to format in bytes. */
    this.value = 0;
    /** The unit to display. */
    this.unit = 'byte';
    /** Determines how to display the result, e.g. "100 bytes", "100 b", or "100b". */
    this.display = 'short';
  }
  render() {
    if (isNaN(this.value)) {
      return '';
    }
    const bitPrefixes = ['', 'kilo', 'mega', 'giga', 'tera']; // petabit isn't a supported unit
    const bytePrefixes = ['', 'kilo', 'mega', 'giga', 'tera', 'peta'];
    const prefix = this.unit === 'bit' ? bitPrefixes : bytePrefixes;
    const index = Math.max(0, Math.min(Math.floor(Math.log10(this.value) / 3), prefix.length - 1));
    const unit = prefix[index] + this.unit;
    const valueToFormat = parseFloat((this.value / Math.pow(1000, index)).toPrecision(3));
    return new Intl.NumberFormat(this.locale, { style: 'unit', unit, unitDisplay: this.display }).format(valueToFormat);
  }
  static get is() { return "sc-format-bytes"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-format-bytes.css"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-format-bytes.css"]
  }; }
  static get properties() { return {
    "locale": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The locale to use when formatting the number."
      },
      "attribute": "locale",
      "reflect": false
    },
    "value": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The number to format in bytes."
      },
      "attribute": "value",
      "reflect": false,
      "defaultValue": "0"
    },
    "unit": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'byte' | 'bit'",
        "resolved": "\"bit\" | \"byte\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The unit to display."
      },
      "attribute": "unit",
      "reflect": false,
      "defaultValue": "'byte'"
    },
    "display": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'long' | 'short' | 'narrow'",
        "resolved": "\"long\" | \"narrow\" | \"short\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Determines how to display the result, e.g. \"100 bytes\", \"100 b\", or \"100b\"."
      },
      "attribute": "display",
      "reflect": false,
      "defaultValue": "'short'"
    }
  }; }
}
