# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.

# %% auto 0
__all__ = ['nymstr']

# %% ../nbs/00_core.ipynb 5
import copy

from atyp import AnyQ, StrLikeQ, ClsStrQ, ClsBool, ClsFloat

from indoc import indoc
from fuzstr import fuzstr
from dynattr import DynamicAttributesMixin

# from aprep import aprep

# %% ../nbs/00_core.ipynb 6
from ispec.enums import (AttrPref,)
from ispec.utils import (funkws, kwsopt, )
from ispec.dun import (getnew, getsup, )
from ispec.agg import aggattrs
from ispec.set import setkws

# %% ../nbs/00_core.ipynb 7
from .snake import to_snake

# %% ../nbs/00_core.ipynb 9
@indoc
class nymstr(fuzstr, DynamicAttributesMixin):    
    '''nymstr

    Parameters
    ----------    
    itemattr : str, Default: 'el'
        The attribute which holds the item being named, by default 'el'.
    fullrepr : bool, Default: False
        Whether or not to show the full representation, by default False.
    '''
    # Whether to ignore case when matching
    icase:  ClsBool  = False
    # Whether to ignore spaces when matching
    ispace: ClsBool  = False
    # Whether to ignore underscores when matching
    iunder: ClsBool  = False
    # The cutoff for fuzzy matching
    cutoff: ClsFloat = 1

    itemattr: ClsStrQ = 'el'
    fullrepr: ClsBool = False

    def __new__(cls, name: StrLikeQ, el: AnyQ = None, **kwargs):        
        # NOTE: `aggattr` gets all kws, whereas `cls.prepall(cls)` only gets annotated kws
        kws, supnew= aggattrs(cls, -1), getnew(getsup(cls))
        kws = kwsopt(kws, **kwargs)        
        # Filter for super().__new__ parameters
        supkws = funkws(supnew, **kws) 
        # Create the original "str" (fuzstr)
        obj = super().__new__(cls, name, **supkws)

        # Extract the name and item from the kwargs  
        obj = setkws(obj, pref=AttrPref.KOC, keys=list(kws.keys()), **kws) 

        # Set dynamic attributes
        dattrs = ('itemattr', )
        kws.update({cls.itemattr: el})
        obj.setdynattrs(dattrs, **kws)
        return obj

    def __repr__(self):
        if self.getclsattr('fullrepr') or self.fullrepr:
            return f'{self.clsname}({str(self)}={self.item})'
        return str(self)

    @property
    def snake(self) -> str:
        return to_snake(self)

    def copy(self):
        return copy.copy(self)

    @property
    def item(self) -> str:
        '''The dynamic `itemattr` i.e. `getattr(self, self.itemattr, None)`'''
        return self.getdynattr('itemattr', None)        
    
    def isitem(self, other: AnyQ) -> bool:
        '''Check if `other` is the instance's item'''        
        if isinstance(other, type(self)) or hasattr(other, 'item'):
            return self.item == other.item
        return self.item == other

    def __eq__(self, other):
        if isinstance(other, (type(self))):
            return super().__eq__(other) and self.isitem(other)
        return super().__eq__(other)
