<?php
/** @noinspection OneTimeUseVariablesInspection */

namespace CouponsPlus\App\Coupon\Tree\ItemSetsToDiscount;

use CouponsPlus\Original\Collections\Collection;
use CouponsPlus\App\Coupon\Tree\Abilities\ItemsInCartFinderStrategy;
use CouponsPlus\App\Coupon\Tree\ItemSetsToDiscount\Abilities\ProductsInCartFinderStrategyOptionalFactory;

class ProductsInCartFinderStrategyAbstractFactory
{

    protected Collection $factories;

    public function __construct(array $productsInCartFinderStrategyFactories)
    {
        $this->factories = new Collection($productsInCartFinderStrategyFactories);
    }

    public function create(string $mode, $data): ItemsInCartFinderStrategy
    {
        /** @var ProductsInCartFinderStrategyOptionalFactory $factory */
        $factory = $this->factories->find(
            function (ProductsInCartFinderStrategyOptionalFactory $productsInCartFinderStrategyOptionalFactory) use ($mode): bool {
                return $productsInCartFinderStrategyOptionalFactory->canCreate($mode);
            }
        );

        return $factory->create($data);
    }
}