<?php

namespace domain\Product\Accessory;

use domain\_\AbstractBuilder;
use domain\Producer\Producer\Producer;
use domain\Product\ProductAccessory\ProductAccessoryRepository;

class AccessoryBuilder extends AbstractBuilder
{
    protected ProductAccessoryRepository $productAccessoryRepository;

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

    public function instanciateAccessory(Producer $producer): Accessory
    {
        $accessory = new Accessory();
        $accessory->setProducer($producer);

        return $accessory;
    }

    public function initSelectedProductsIds(Accessory $accessory): void
    {
        $selectedProductsIdsArray = [];
        $productAccessoriesArray = $this->productAccessoryRepository->findProductAccessoriesByAccessory($accessory);
        foreach($productAccessoriesArray as $productAccessory) {
            $selectedProductsIdsArray[] = $productAccessory->getProduct()->id;
        }
        $accessory->setSelectedProductsIds($selectedProductsIdsArray);
    }
}