use crate::parser::grammar::description;
use crate::parser::grammar::directive;
use crate::parser::grammar::field;
use crate::parser::grammar::name;
use crate::parser::grammar::object;
use crate::parser::grammar::value::Constness;
use crate::Parser;
use crate::SyntaxKind;
use crate::TokenKind;
use crate::T;

/// See: https://spec.graphql.org/October2021/#InterfaceTypeDefinition
///
/// *InterfaceTypeDefinition*:
///     Description? **interface** Name ImplementsInterface? Directives[Const]? FieldsDefinition?
pub(crate) fn interface_type_definition(p: &mut Parser) {
    let _g = p.start_node(SyntaxKind::INTERFACE_TYPE_DEFINITION);

    if let Some(TokenKind::StringValue) = p.peek() {
        description::description(p);
    }

    if let Some("interface") = p.peek_data() {
        p.bump(SyntaxKind::interface_KW);
    }

    match p.peek() {
        Some(TokenKind::Name) => name::name(p),
        _ => p.err("expected a Name"),
    }

    if let Some("implements") = p.peek_data() {
        object::implements_interfaces(p);
    }

    if let Some(T![@]) = p.peek() {
        directive::directives(p, Constness::Const);
    }

    if let Some(T!['{']) = p.peek() {
        field::fields_definition(p);
    }
}

/// See: https://spec.graphql.org/October2021/#InterfaceTypeExtension
///
/// *InterfaceTypeExtension*:
///     **extend** **interface** Name ImplementsInterface? Directives[Const]? FieldsDefinition
///     **extend** **interface** Name ImplementsInterface? Directives[Const]
///     **extend** **interface** Name ImplementsInterface
pub(crate) fn interface_type_extension(p: &mut Parser) {
    let _g = p.start_node(SyntaxKind::INTERFACE_TYPE_EXTENSION);
    p.bump(SyntaxKind::extend_KW);
    p.bump(SyntaxKind::interface_KW);

    let mut meets_requirements = false;

    match p.peek() {
        Some(TokenKind::Name) => name::name(p),
        _ => p.err("expected a Name"),
    }

    if let Some("implements") = p.peek_data() {
        meets_requirements = true;
        object::implements_interfaces(p);
    }

    if let Some(T![@]) = p.peek() {
        meets_requirements = true;
        directive::directives(p, Constness::Const);
    }

    if let Some(T!['{']) = p.peek() {
        meets_requirements = true;
        field::fields_definition(p);
    }

    if !meets_requirements {
        p.err("exptected an Implements Interfaces, Directives, or a Fields Definition");
    }
}
