<?php

namespace AutoAdmin\Bundle\Content\Entity\Duplicators;

use AutoAdmin\Bundle\Content\Entity\Interfaces\MenuItemInterface;

class MenuItemDuplicator extends AbstractDuplicator
{
    /**
     * @return string[]
     */
    public function support(): array
    {
        return [
            MenuItemInterface::class
        ];
    }

    /**
     * @param MenuItemInterface $entity
     * @param MenuItemInterface $duplicate
     *
     * @return MenuItemInterface
     */
    public function duplicate($entity, $duplicate, $parent = null)
    {
        if ($parent instanceof MenuItemInterface) {
            $duplicate->setMenu($parent);
        }
        foreach ($entity->getChildren() as $child) {
            $duplicate->addChild($this->duplicatorService->duplicate($child, $duplicate));
        }
        $duplicate->setDisplay($entity->isDisplay());
        $duplicate->setType($entity->getType());
        $duplicate->setUri($entity->getUri());
        $duplicate->setWrapperAttributes($entity->getWrapperAttributes());
        $duplicate->setLinkAttributes($entity->getLinkAttributes());
        $duplicate->setChildrenAttributes($entity->getChildrenAttributes());
        if ($entity->getContentReference() !== null) {
            $duplicate->setContentReference($this->duplicatorService->duplicate($entity->getContentReference()));
        }

        return $duplicate;
    }
}
