<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\DI\Definition;

use PpPhp\Core\Platform\DI\Contracts\ContainerInterface;
use PpPhp\Core\Platform\DI\Contracts\ResolverInterface;
use PpPhp\Core\Platform\DI\Metadata\Lifetime;

final class DecoratorDefinition extends AbstractDefinition
{
    public function __construct(
        string $id,
        private readonly string $decorates,
        private readonly string $decoratorClass,
        string $lifetime = Lifetime::TRANSIENT
    ) {
        parent::__construct($id, $lifetime);
    }

    public function type(): string
    {
        return 'decorator';
    }

    public function decorates(): string
    {
        return $this->decorates;
    }

    public function decoratorClass(): string
    {
        return $this->decoratorClass;
    }

    public function resolve(ContainerInterface $container, ResolverInterface $resolver): mixed
    {
        return $resolver->buildClass($this->decoratorClass, $container);
    }

    public function toArray(): array
    {
        return $this->baseArray() + [
            'decorates' => $this->decorates,
            'decoratorClass' => $this->decoratorClass,
        ];
    }
}
