{"version":3,"file":"use-format-byte.cjs","names":["useI18n","useNumberFormat"],"sources":["../../../../src/components/format/use-format-byte.ts"],"sourcesContent":["\"use client\"\n\nimport type { Locale } from \"../../providers/i18n-provider\"\nimport type { AnyString } from \"../../utils\"\nimport { useCallback } from \"react\"\nimport { useI18n } from \"../../providers/i18n-provider\"\nimport { useNumberFormat } from \"./use-format-number\"\n\nconst bitPrefixes = [\"\", \"kilo\", \"mega\", \"giga\", \"tera\"]\nconst bytePrefixes = [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\"]\n\nexport interface FormatBytesOptions {\n  /**\n   * The locale string to use for formatting.\n   *\n   * @default 'en-US'\n   */\n  locale?: AnyString | Locale\n  /**\n   * The unit granularity to display\n   */\n  unit?: \"bit\" | \"byte\"\n  /**\n   * The unit display\n   */\n  unitDisplay?: \"long\" | \"narrow\" | \"short\"\n}\n\n/**\n * `useFormatByte` is a custom hook that returns the formatted byte.\n *\n * @see https://yamada-ui.com/docs/hooks/use-format-byte\n */\nexport const useFormatByte = (bytes: number, options?: FormatBytesOptions) => {\n  const byteFormat = useByteFormat(options)\n\n  return byteFormat(bytes)\n}\n\nexport type UseFormatByteReturn = ReturnType<typeof useFormatByte>\n\nexport const useByteFormat = ({\n  locale,\n  unit: defaultUnit = \"byte\",\n  unitDisplay: defaultUnitDisplay = \"short\",\n}: FormatBytesOptions = {}) => {\n  const { locale: defaultLocale } = useI18n()\n  const numberFormat = useNumberFormat({ locale: locale ?? defaultLocale })\n\n  const byteFormat = useCallback(\n    (\n      bytes: number,\n      {\n        unit = defaultUnit,\n        unitDisplay = defaultUnitDisplay,\n      }: Omit<FormatBytesOptions, \"locale\"> = {},\n    ) => {\n      const sanitizedBytes = Number.isNaN(bytes) ? 0 : bytes\n      const prefix = unit === \"bit\" ? bitPrefixes : bytePrefixes\n      const index = Math.max(\n        0,\n        Math.min(Math.floor(Math.log10(sanitizedBytes) / 3), prefix.length - 1),\n      )\n      const value =\n        sanitizedBytes === 0\n          ? 0\n          : Number.parseFloat(\n              (sanitizedBytes / 10 ** (3 * index)).toPrecision(3),\n            )\n\n      return numberFormat(value, {\n        style: \"unit\",\n        unit: prefix[index] + unit,\n        unitDisplay,\n      })\n    },\n    [defaultUnit, defaultUnitDisplay, numberFormat],\n  )\n\n  return byteFormat\n}\n\nexport type UseByteFormatReturn = ReturnType<typeof useByteFormat>\n"],"mappings":";;;;;;;;;;AAQA,MAAM,cAAc;CAAC;CAAI;CAAQ;CAAQ;CAAQ;CAAO;AACxD,MAAM,eAAe;CAAC;CAAI;CAAQ;CAAQ;CAAQ;CAAQ;CAAO;;;;;;AAwBjE,MAAa,iBAAiB,OAAe,YAAiC;AAG5E,QAFmB,cAAc,QAAQ,CAEvB,MAAM;;AAK1B,MAAa,iBAAiB,EAC5B,QACA,MAAM,cAAc,QACpB,aAAa,qBAAqB,YACZ,EAAE,KAAK;CAC7B,MAAM,EAAE,QAAQ,kBAAkBA,+BAAS;CAC3C,MAAM,eAAeC,0CAAgB,EAAE,QAAQ,UAAU,eAAe,CAAC;AAgCzE,gCA5BI,OACA,EACE,OAAO,aACP,cAAc,uBACwB,EAAE,KACvC;EACH,MAAM,iBAAiB,OAAO,MAAM,MAAM,GAAG,IAAI;EACjD,MAAM,SAAS,SAAS,QAAQ,cAAc;EAC9C,MAAM,QAAQ,KAAK,IACjB,GACA,KAAK,IAAI,KAAK,MAAM,KAAK,MAAM,eAAe,GAAG,EAAE,EAAE,OAAO,SAAS,EAAE,CACxE;AAQD,SAAO,aANL,mBAAmB,IACf,IACA,OAAO,YACJ,iBAAiB,OAAO,IAAI,QAAQ,YAAY,EAAE,CACpD,EAEoB;GACzB,OAAO;GACP,MAAM,OAAO,SAAS;GACtB;GACD,CAAC;IAEJ;EAAC;EAAa;EAAoB;EAAa,CAChD"}