from .product import product
from .product import product_factory
from .exception import empty_field_exception

class product_builder:
    def __init__(self, target: product | None = None) -> None:
        self.__target = target

        if self.__target is None:
            self.__target = product()

    def modify(self, target: dict) -> object:
        factory = product_factory(self.__target)

        if "name" in target:
            factory.name = target["name"]

        if "description" in target:
            factory.description = target["description"]

        if "author" in target:
            factory.author = target["author"]

        if "stock_count" in target:
            stock_count = target["stock_count"]

            if type(stock_count) is str:
                stock_count = stock_count.strip()
            
            if type(stock_count) is str and len(stock_count) == 0:
                raise empty_field_exception("stock_count")
            
            factory.stock_count = int(stock_count)

        if "barcode" in target:
            factory.barcode = target["barcode"]

        self.__target = factory.result    

        return self

    @property
    def result(self) -> product:
        return self.__target
