import typing as t

from miska import strings
from miska import multitons


class MISSING(metaclass=multitons.MultitonMeta):
    def __init__(self, label: str | None = None, /) -> None:
        if isinstance(label, str) and not label:
            raise ValueError("label must be non-empty string")
        self.label = label

    def __repr__(self) -> str:
        return strings.to_repr(self, args=repr(self.label))

    def __str__(self) -> str:
        cls_name = strings.clsqualname(self)
        if self.label:
            return f"{cls_name}[{self.label}]"
        return cls_name

    def __bool__(self) -> bool:
        return False


def is_missing(obj: t.Any, label: str | None = None) -> bool:
    if label:
        return isinstance(obj, MISSING) and obj.label == label

    if isinstance(obj, type):
        return issubclass(obj, MISSING)

    return isinstance(obj, MISSING)
