from mw_helper import ensure_list
from production.box import ItemSorterBox
from production.treebuilder import BoxConfiguration

from ._BoxBuilder import BoxBuilder


class ItemSorterBoxBuilder(BoxBuilder):
    def __init__(self, product_repository, loggers):
        super(ItemSorterBoxBuilder, self).__init__(loggers)
        self.product_repository = product_repository
        self.loggers = loggers

    def build(self, config):
        # type: (BoxConfiguration) -> ItemSorterBox

        sort_order = ensure_list(self.get_extra(config, "SortOrder", []))
        item_property_sorters = ensure_list(self.get_extra(config, "ItemPropertySorters", []))

        return ItemSorterBox(
            config.name,
            config.sons,
            self.product_repository,
            sort_order,
            item_property_sorters,
            self.get_logger(config)
        )
