<?php

namespace domain\Order\OrderStatus;

use domain\_\AbstractRepository;
use yii\base\ErrorException;

class OrderStatusRepository extends AbstractRepository
{
    protected OrderStatusDefinition $orderStatusDefinition;

    public function loadDependencies(): void
    {
        $this->orderStatusDefinition = $this->loadService(OrderStatusDefinition::class);
    }

    public function getDefaultOptionsSearch(): array
    {
        return [];
    }

    public function getOrderStatusByAlias(string $alias): OrderStatus
    {
        $orderStatusArray = $this->orderStatusDefinition->getOrderStatusList();

        if(isset($orderStatusArray[$alias])) {
            return OrderStatusBuilder::getInstance()->instanciateOrderStatus(
                $alias,
                $orderStatusArray[$alias]['label']
            );
        }
        else {
            throw new ErrorException('Statut de commande introuvable : '.$alias);
        }
    }
}