<?php declare(strict_types=1);

namespace GraphQL\Utils;

use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\SerializationError;
use GraphQL\Language\AST\BooleanValueNode;
use GraphQL\Language\AST\DefinitionNode;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Language\AST\FloatValueNode;
use GraphQL\Language\AST\IntValueNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\ListValueNode;
use GraphQL\Language\AST\Location;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\NameNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\NodeList;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\NullValueNode;
use GraphQL\Language\AST\ObjectFieldNode;
use GraphQL\Language\AST\ObjectValueNode;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\ValueNode;
use GraphQL\Language\AST\VariableNode;
use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\IDType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\LeafType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\NullableType;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type;

/**
 * Various utilities dealing with AST.
 */
class AST
{
    /**
     * Convert representation of AST as an associative array to instance of GraphQL\Language\AST\Node.
     *
     * For example:
     *
     * ```php
     * AST::fromArray([
     *     'kind' => 'ListValue',
     *     'values' => [
     *         ['kind' => 'StringValue', 'value' => 'my str'],
     *         ['kind' => 'StringValue', 'value' => 'my other str']
     *     ],
     *     'loc' => ['start' => 21, 'end' => 25]
     * ]);
     * ```
     *
     * Will produce instance of `ListValueNode` where `values` prop is a lazily-evaluated `NodeList`
     * returning instances of `StringValueNode` on access.
     *
     * This is a reverse operation for AST::toArray($node)
     *
     * @param array<string, mixed> $node
     *
     * @api
     *
     * @throws \JsonException
     * @throws InvariantViolation
     */
    public static function fromArray(array $node): Node
    {
        $kind = $node['kind'] ?? null;
        if ($kind === null) {
            $safeNode = Utils::printSafeJson($node);
            throw new InvariantViolation("Node is missing kind: {$safeNode}");
        }

        $class = NodeKind::CLASS_MAP[$kind] ?? null;
        if ($class === null) {
            $safeNode = Utils::printSafeJson($node);
            throw new InvariantViolation("Node has unexpected kind: {$safeNode}");
        }

        $instance = new $class([]);

        if (isset($node['loc']['start'], $node['loc']['end'])) {
            $instance->loc = Location::create($node['loc']['start'], $node['loc']['end']);
        }

        foreach ($node as $key => $value) {
            if ($key === 'loc' || $key === 'kind') {
                continue;
            }

            if (is_array($value)) {
                $value = isset($value[0]) || $value === []
                    ? new NodeList($value)
                    : self::fromArray($value);
            }

            $instance->{$key} = $value;
        }

        return $instance;
    }

    /**
     * Convert AST node to serializable array.
     *
     * @return array<string, mixed>
     *
     * @api
     */
    public static function toArray(Node $node): array
    {
        return $node->toArray();
    }

    /**
     * Produces a GraphQL Value AST given a PHP value.
     *
     * Optionally, a GraphQL type may be provided, which will be used to
     * disambiguate between value primitives.
     *
     * | PHP Value     | GraphQL Value        |
     * | ------------- | -------------------- |
     * | Object        | Input Object         |
     * | Assoc Array   | Input Object         |
     * | Array         | List                 |
     * | Boolean       | Boolean              |
     * | String        | String / Enum Value  |
     * | Int           | Int                  |
     * | Float         | Int / Float          |
     * | Mixed         | Enum Value           |
     * | null          | NullValue            |
     *
     * @param mixed $value
     * @param InputType&Type $type
     *
     * @throws \JsonException
     * @throws InvariantViolation
     * @throws SerializationError
     *
     * @return (ValueNode&Node)|null
     *
     * @api
     */
    public static function astFromValue($value, InputType $type): ?ValueNode
    {
        if ($type instanceof NonNull) {
            $wrappedType = $type->getWrappedType();
            assert($wrappedType instanceof InputType);

            $astValue = self::astFromValue($value, $wrappedType);

            return $astValue instanceof NullValueNode
                ? null
                : $astValue;
        }

        if ($value === null) {
            return new NullValueNode([]);
        }

        if ($type instanceof ListOfType) {
            $itemType = $type->getWrappedType();
            assert($itemType instanceof InputType, 'proven by schema validation');

            if (is_iterable($value)) {
                $valuesNodes = [];
                foreach ($value as $item) {
                    $itemNode = self::astFromValue($item, $itemType);
                    if ($itemNode !== null) {
                        $valuesNodes[] = $itemNode;
                    }
                }

                return new ListValueNode(['values' => new NodeList($valuesNodes)]);
            }

            return self::astFromValue($value, $itemType);
        }

        if ($type instanceof InputObjectType) {
            $isArray = is_array($value);
            $isArrayLike = $isArray || $value instanceof \ArrayAccess;
            if (! $isArrayLike && ! is_object($value)) {
                return null;
            }

            $fields = $type->getFields();
            $fieldNodes = [];
            foreach ($fields as $fieldName => $field) {
                $fieldValue = $isArrayLike
                    ? $value[$fieldName] ?? null
                    : $value->{$fieldName} ?? null;

                if ($fieldValue !== null) {
                    $fieldExists = true;
                } elseif ($isArray) {
                    $fieldExists = array_key_exists($fieldName, $value);
                } elseif ($isArrayLike) {
                    $fieldExists = $value->offsetExists($fieldName);
                } else {
                    $fieldExists = property_exists($value, $fieldName);
                }

                if (! $fieldExists) {
                    continue;
                }

                $fieldNode = self::astFromValue($fieldValue, $field->getType());

                if ($fieldNode === null) {
                    continue;
                }

                $fieldNodes[] = new ObjectFieldNode([
                    'name' => new NameNode(['value' => $fieldName]),
                    'value' => $fieldNode,
                ]);
            }

            return new ObjectValueNode(['fields' => new NodeList($fieldNodes)]);
        }

        assert($type instanceof LeafType, 'other options were exhausted');

        $serialized = $type->serialize($value);

        if (is_bool($serialized)) {
            return new BooleanValueNode(['value' => $serialized]);
        }

        if (is_int($serialized)) {
            return new IntValueNode(['value' => (string) $serialized]);
        }

        if (is_float($serialized)) {
            /** @phpstan-ignore equal.notAllowed (int cast with == used for performance reasons) */
            if ((int) $serialized == $serialized) {
                return new IntValueNode(['value' => (string) $serialized]);
            }

            return new FloatValueNode(['value' => (string) $serialized]);
        }

        if (is_string($serialized)) {
            if ($type instanceof EnumType) {
                return new EnumValueNode(['value' => $serialized]);
            }

            $asInt = (int) $serialized;
            if ($type instanceof IDType && (string) $asInt === $serialized) {
                return new IntValueNode(['value' => $serialized]);
            }

            return new StringValueNode(['value' => $serialized]);
        }

        $notConvertible = Utils::printSafe($serialized);
        throw new InvariantViolation("Cannot convert value to AST: {$notConvertible}");
    }

    /**
     * Produces a PHP value given a GraphQL Value AST.
     *
     * A GraphQL type must be provided, which will be used to interpret different
     * GraphQL Value literals.
     *
     * Returns `null` when the value could not be validly coerced according to
     * the provided type.
     *
     * | GraphQL Value        | PHP Value     |
     * | -------------------- | ------------- |
     * | Input Object         | Assoc Array   |
     * | List                 | Array         |
     * | Boolean              | Boolean       |
     * | String               | String        |
     * | Int / Float          | Int / Float   |
     * | Enum Value           | Mixed         |
     * | Null Value           | null          |
     *
     * @param (ValueNode&Node)|null $valueNode
     * @param array<string, mixed>|null $variables
     *
     * @throws \Exception
     *
     * @return mixed
     *
     * @api
     */
    public static function valueFromAST(?ValueNode $valueNode, Type $type, ?array $variables = null)
    {
        $undefined = Utils::undefined();

        if ($valueNode === null) {
            return $undefined;
        }

        if ($type instanceof NonNull) {
            if ($valueNode instanceof NullValueNode) {
                return $undefined;
            }

            return self::valueFromAST($valueNode, $type->getWrappedType(), $variables);
        }

        if ($valueNode instanceof NullValueNode) {
            return null;
        }

        if ($valueNode instanceof VariableNode) {
            $variableName = $valueNode->name->value;

            if ($variables === null || ! array_key_exists($variableName, $variables)) {
                return $undefined;
            }

            return $variables[$variableName];
        }

        if ($type instanceof ListOfType) {
            $itemType = $type->getWrappedType();

            if ($valueNode instanceof ListValueNode) {
                $coercedValues = [];
                $itemNodes = $valueNode->values;
                foreach ($itemNodes as $itemNode) {
                    if (self::isMissingVariable($itemNode, $variables)) {
                        if ($itemType instanceof NonNull) {
                            return $undefined;
                        }

                        $coercedValues[] = null;
                    } else {
                        $itemValue = self::valueFromAST($itemNode, $itemType, $variables);
                        if ($undefined === $itemValue) {
                            return $undefined;
                        }

                        $coercedValues[] = $itemValue;
                    }
                }

                return $coercedValues;
            }

            $coercedValue = self::valueFromAST($valueNode, $itemType, $variables);
            if ($undefined === $coercedValue) {
                return $undefined;
            }

            return [$coercedValue];
        }

        if ($type instanceof InputObjectType) {
            if (! $valueNode instanceof ObjectValueNode) {
                return $undefined;
            }

            $coercedObj = [];
            $fields = $type->getFields();

            $fieldNodes = [];
            foreach ($valueNode->fields as $field) {
                $fieldNodes[$field->name->value] = $field;
            }

            foreach ($fields as $field) {
                $fieldName = $field->name;
                $fieldNode = $fieldNodes[$fieldName] ?? null;

                if ($fieldNode === null || self::isMissingVariable($fieldNode->value, $variables)) {
                    if ($field->defaultValueExists()) {
                        $coercedObj[$fieldName] = $field->defaultValue;
                    } elseif ($field->getType() instanceof NonNull) {
                        return $undefined;
                    }

                    continue;
                }

                $fieldValue = self::valueFromAST(
                    $fieldNode->value,
                    $field->getType(),
                    $variables
                );

                if ($undefined === $fieldValue) {
                    return $undefined;
                }

                $coercedObj[$fieldName] = $fieldValue;
            }

            return $type->parseValue($coercedObj);
        }

        if ($type instanceof EnumType) {
            try {
                return $type->parseLiteral($valueNode, $variables);
            } catch (\Throwable $error) {
                return $undefined;
            }
        }

        assert($type instanceof ScalarType, 'only remaining option');

        try {
            return $type->parseLiteral($valueNode, $variables);
        } catch (\Throwable $error) {
            return $undefined;
        }
    }

    /**
     * Returns true if the provided valueNode is a variable which is not defined
     * in the set of variables.
     *
     * @param ValueNode&Node $valueNode
     * @param array<string, mixed>|null $variables
     */
    private static function isMissingVariable(ValueNode $valueNode, ?array $variables): bool
    {
        return $valueNode instanceof VariableNode
            && ($variables === null || ! array_key_exists($valueNode->name->value, $variables));
    }

    /**
     * Produces a PHP value given a GraphQL Value AST.
     *
     * Unlike `valueFromAST()`, no type is provided. The resulting PHP value
     * will reflect the provided GraphQL value AST.
     *
     * | GraphQL Value        | PHP Value     |
     * | -------------------- | ------------- |
     * | Input Object         | Assoc Array   |
     * | List                 | Array         |
     * | Boolean              | Boolean       |
     * | String               | String        |
     * | Int / Float          | Int / Float   |
     * | Enum                 | Mixed         |
     * | Null                 | null          |
     *
     * @param array<string, mixed>|null $variables
     *
     * @throws \Exception
     *
     * @return mixed
     *
     * @api
     */
    public static function valueFromASTUntyped(Node $valueNode, ?array $variables = null)
    {
        switch (true) {
            case $valueNode instanceof NullValueNode:
                return null;

            case $valueNode instanceof IntValueNode:
                return (int) $valueNode->value;

            case $valueNode instanceof FloatValueNode:
                return (float) $valueNode->value;

            case $valueNode instanceof StringValueNode:
            case $valueNode instanceof EnumValueNode:
            case $valueNode instanceof BooleanValueNode:
                return $valueNode->value;

            case $valueNode instanceof ListValueNode:
                $values = [];
                foreach ($valueNode->values as $node) {
                    $values[] = self::valueFromASTUntyped($node, $variables);
                }

                return $values;

            case $valueNode instanceof ObjectValueNode:
                $values = [];
                foreach ($valueNode->fields as $field) {
                    $values[$field->name->value] = self::valueFromASTUntyped($field->value, $variables);
                }

                return $values;

            case $valueNode instanceof VariableNode:
                $variableName = $valueNode->name->value;

                return ($variables ?? []) !== [] && isset($variables[$variableName])
                    ? $variables[$variableName]
                    : null;
        }

        throw new Error("Unexpected value kind: {$valueNode->kind}");
    }

    /**
     * Returns type definition for given AST Type node.
     *
     * @param callable(string): ?Type $typeLoader
     * @param NamedTypeNode|ListTypeNode|NonNullTypeNode $inputTypeNode
     *
     * @throws \Exception
     *
     * @api
     */
    public static function typeFromAST(callable $typeLoader, Node $inputTypeNode): ?Type
    {
        if ($inputTypeNode instanceof ListTypeNode) {
            $innerType = self::typeFromAST($typeLoader, $inputTypeNode->type);

            return $innerType === null
                ? null
                : new ListOfType($innerType);
        }

        if ($inputTypeNode instanceof NonNullTypeNode) {
            $innerType = self::typeFromAST($typeLoader, $inputTypeNode->type);
            if ($innerType === null) {
                return null;
            }

            assert($innerType instanceof NullableType, 'proven by schema validation');

            return new NonNull($innerType);
        }

        return $typeLoader($inputTypeNode->name->value);
    }

    /**
     * Returns the operation within a document by name.
     *
     * If a name is not provided, an operation is only returned if the document has exactly one.
     *
     * @api
     */
    public static function getOperationAST(DocumentNode $document, ?string $operationName = null): ?OperationDefinitionNode
    {
        $operation = null;
        foreach ($document->definitions->getIterator() as $node) {
            if (! $node instanceof OperationDefinitionNode) {
                continue;
            }

            if ($operationName === null) {
                if ($operation !== null) {
                    return null;
                }

                $operation = $node;
            } elseif ($node->name instanceof NameNode && $node->name->value === $operationName) {
                return $node;
            }
        }

        return $operation;
    }

    /**
     * Provided a collection of ASTs, presumably each from different files,
     * concatenate the ASTs together into batched AST, useful for validating many
     * GraphQL source files which together represent one conceptual application.
     *
     * @param array<DocumentNode> $documents
     *
     * @api
     */
    public static function concatAST(array $documents): DocumentNode
    {
        /** @var array<int, Node&DefinitionNode> $definitions */
        $definitions = [];
        foreach ($documents as $document) {
            foreach ($document->definitions as $definition) {
                $definitions[] = $definition;
            }
        }

        return new DocumentNode(['definitions' => new NodeList($definitions)]);
    }
}
