<?php

namespace CouponsPlus\App\Coupon\Tree\ItemSetsToDiscount;

use CouponsPlus\App\Coupon\Tree\Abilities\ItemSetToDiscountHandler;
use CouponsPlus\App\Coupon\Tree\Abilities\OptionalItemSetToDiscountHandler;

class ItemSetToDiscountHandlerComposite implements ItemSetToDiscountHandler
{
    /**
     * @var (ItemSetToDiscountHandler&OptionalItemSetToDiscountHandler)[]
     */
    protected array $itemSetToDiscountHandlers;

    /**
     * Can have multiple handlers handle the same item set to discount.
     * This will not pick the first available handler, it will pick any that can handle it.
     *
     */
    public function __construct(array $itemSetToDiscountHandlers)
    {
        $this->itemSetToDiscountHandlers = $itemSetToDiscountHandlers;
    }

    public function handle(ItemsSetToDiscount $itemSetToDiscount)
    {
        /**
         * Why can they not fill it?
         *
         * -can they be added to the cart? add them (execution ends here)
         * -cannot be added to the cart? react based on this. possible reactions:
         *  -show a notification
         *  -display a list of items that the user can choose from
         *  -etc
         */
        foreach($this->itemSetToDiscountHandlers as $itemSetToDiscountHandler) {
            if ($itemSetToDiscountHandler->canHandle($itemSetToDiscount)) {
                $itemSetToDiscountHandler->handle($itemSetToDiscount);
                // don break it because we may have multiple handlers that can handle the same item set to discount.
            }
        }
    }
}