<?php

namespace KupShop\GraphQLBundle\GraphiQL;

use Overblog\GraphiQLBundle\Config\GraphiQLControllerEndpoint;
use Overblog\GraphiQLBundle\Config\GraphQLEndpoint\GraphQLEndpointInvalidSchemaException;
use Symfony\Component\HttpFoundation\RequestStack;
use Webmozart\Assert\Assert;

final class EndpointResolver implements GraphiQLControllerEndpoint
{
    /**
     * @var RequestStack
     */
    private $requestStack;

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    public function getBySchema($name)
    {
        if ('default' === $name) {
            $request = $this->requestStack->getCurrentRequest();
            Assert::notNull($request);

            return $request->getBaseUrl().'/graphql';
        } elseif ($name === 'admin') {
            $request = $this->requestStack->getCurrentRequest();
            Assert::notNull($request);

            return $request->getBaseUrl().'/admin/graphql/';
        } elseif ($name === 'pos') {
            $request = $this->requestStack->getCurrentRequest();
            Assert::notNull($request);

            return $request->getBaseUrl().'/admin/pos/graphql/';
        }

        throw GraphQLEndpointInvalidSchemaException::forSchemaAndResolver($name, self::class);
    }

    public function getDefault()
    {
        return $this->getBySchema('default');
    }
}
