<?php

declare(strict_types=1);

namespace BeyondSEODeps\ProxyManager\Factory;

use OutOfBoundsException;
use BeyondSEODeps\ProxyManager\Configuration;
use BeyondSEODeps\ProxyManager\Factory\RemoteObject\AdapterInterface;
use BeyondSEODeps\ProxyManager\Proxy\RemoteObjectInterface;
use BeyondSEODeps\ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
use BeyondSEODeps\ProxyManager\ProxyGenerator\RemoteObjectGenerator;
use BeyondSEODeps\ProxyManager\Signature\Exception\InvalidSignatureException;
use BeyondSEODeps\ProxyManager\Signature\Exception\MissingSignatureException;

use function get_class;
use function is_object;

/**
 * Factory responsible of producing remote proxy objects
 */
class RemoteObjectFactory extends AbstractBaseFactory
{
    protected $adapter;
    private $generator;

    /**
     * {@inheritDoc}
     *
     * @param AdapterInterface $adapter
     * @param Configuration    $configuration
     */
    public function __construct(AdapterInterface $adapter, ?Configuration $configuration = null)
    {
        parent::__construct($configuration);

        $this->adapter   = $adapter;
        $this->generator = new RemoteObjectGenerator();
    }

    /**
     * @psalm-param RealObjectType|class-string<RealObjectType> $instanceOrClassName
     *
     * @psalm-return RealObjectType&RemoteObjectInterface
     *
     * @throws InvalidSignatureException
     * @throws MissingSignatureException
     * @throws OutOfBoundsException
     *
     * @psalm-template RealObjectType of object
     * @psalm-suppress MixedInferredReturnType We ignore type checks here, since `staticProxyConstructor` is not
     *                                         interfaced (by design)
     */
    public function createProxy($instanceOrClassName): RemoteObjectInterface
    {
        $proxyClassName = $this->generateProxy(
            is_object($instanceOrClassName) ? get_class($instanceOrClassName) : $instanceOrClassName
        );

        /**
         * We ignore type checks here, since `staticProxyConstructor` is not interfaced (by design)
         *
         * @psalm-suppress MixedMethodCall
         * @psalm-suppress MixedReturnStatement
         */
        return $proxyClassName::staticProxyConstructor($this->adapter);
    }

    protected function getGenerator(): ProxyGeneratorInterface
    {
        return $this->generator ?? $this->generator = new RemoteObjectGenerator();
    }
}
