<?php

namespace CouponsPlus\App\Coupon\Tree\Finders;

use CouponsPlus\App\Conditions\ItemsSet;
use CouponsPlus\App\Coupon\Tree\Abilities\NodeData;
use CouponsPlus\App\Coupon\Tree\Abilities\NodeFinder;
use CouponsPlus\App\Coupon\Tree\Factories\EmptyOffersNodeDataFactory;
use Error;

class EmptyNodeOnErrorNodeFinder extends NodeFinderComposite implements NodeFinder
{
    public function find(ItemsSet $itemsSet, NodeDatasWithBranch $nodesToSearch): NodeData
    {
        try {
            return $this->nodeFinder->find($itemsSet, $nodesToSearch);
        } catch (Error $exception) {
            (object) $emptyOffersNodeFactory = new EmptyOffersNodeDataFactory;
            
            return $emptyOffersNodeFactory->create();
        }
    } 
}