<?php

namespace CouponsPlus\App\Coupon\Tree;

use CouponsPlus\App\Coupon\Tree\Abilities;
use CouponsPlus\Original\Collections\Collection;

class GroveData implements Abilities\Grove
{
    private $rootNodes;

    public function __construct(
        Abilities\NodeData ...$rootNodes
    ) {
        $this->rootNodes = new Collection($rootNodes);
    }
    
    public function getTrees(): Collection
    {
        return $this->rootNodes;
    }

    public function replaceTree(NodeData $tree, Collection $trees): void
    {
        // collection dont support splice so lets use native arrays (ewwuh)
        $rootNodesArray = $this->rootNodes->asArray();

        array_splice(
            $rootNodesArray,
            $this->rootNodes->findKey(function ($trees) use ($tree) {
                return $trees === $tree;
            }),
            1,
            $trees->asArray()
        );

        $this->rootNodes = new Collection($rootNodesArray);
    }
}