# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_enums.ipynb.

# %% auto 0
__all__ = ['ByteSize', 'BytesUnit']

# %% ../nbs/02_enums.ipynb 4
import os, math
from typing import Union, List, Optional

# %% ../nbs/02_enums.ipynb 5
from litum import LitIntEnum, LitStrEnum, set_dtype, set_fallback

# %% ../nbs/02_enums.ipynb 7
from bytetype.units import (
    BYTE, KILOBYTE, MEGABYTE, GIGABYTE, TERABYTE,
    PETABYTE, EXABYTE, ZETTABYTE, YOTTABYTE
)

# %% ../nbs/02_enums.ipynb 9
from bytetype.utils import (
    bytes_idx, bytes_to_size
)

# %% ../nbs/02_enums.ipynb 12
@set_dtype(int)
@set_fallback('KB')
class ByteSize(LitIntEnum):
    """An enumeration of byte size.
    
    This class defines an int enumeration representing various byte size.    
    """
    B  = BYTE
    KB = KILOBYTE
    MB = MEGABYTE
    GB = GIGABYTE
    TB = TERABYTE
    PB = PETABYTE
    EB = EXABYTE
    ZB = ZETTABYTE
    YB = YOTTABYTE

# %% ../nbs/02_enums.ipynb 14
@set_dtype(str)
@set_fallback('B')
class BytesUnit(LitStrEnum):
    """An enumeration of byte units.
    
    This class defines a string enumeration representing various byte units.
    It provides methods for calculating size and determining appropriate unit from a byte count.
    """
    B = 'B'
    KB = 'KB'
    MB = 'MB'
    GB = 'GB'
    TB = 'TB'
    PB = 'PB'
    EB = 'EB'
    ZB = 'ZB'
    YB = 'YB'

    @staticmethod
    def calc_idx(nbytes: float) -> int:
        """Calculate the index of the byte unit for a byte count.
        
        Parameters
        ----------
        nbytes : float
            The byte count.

        Returns
        -------
        int
            The index of the byte unit.
        """
        return bytes_idx(nbytes)
    
    @staticmethod
    def calc_size(nbytes: float, decimals: Optional[int] = 2) -> float:
        """Calculate the size in appropriate byte unit for a byte count.
        
        Parameters
        ----------
        nbytes : float
            The byte count.
        decimals : int, optional
            The number of decimal places to round to (default is 2).

        Returns
        -------
        float
            The size in the appropriate byte unit.
        """
        return bytes_to_size(nbytes, decimals)

    @classmethod
    def unit(cls, nbytes: float) -> str:
        """Determine the appropriate byte unit for a byte count.
        
        Parameters
        ----------
        nbytes : float
            The byte count.

        Returns
        -------
        str
            The appropriate byte unit.
        """
        idx = cls.calc_idx(nbytes)
        key = cls.keys()[idx]    
        return key
    
    @classmethod
    def keys(cls) -> List[str]:
        """Get the keys of the enumeration.

        Returns
        -------
        List[str]
            The keys of the enumeration.
        """
        return list(cls._member_map_.keys())
    
    @classmethod
    def from_bytes(cls, nbytes: float) -> 'BytesUnit':
        """Create a BytesUnit instance from a byte count.
        
        Parameters
        ----------
        nbytes : float
            The byte count.

        Returns
        -------
        BytesUnit
            The BytesUnit instance representing the appropriate byte unit.
        """
        unit = cls.unit(nbytes)
        return cls(unit)
