import { ChevronLeftIcon, useExplorerContext, useSchemaContext, } from '@graphiql/react';
import { isType } from 'graphql';
import React from 'react';
import FieldDoc from './DocExplorer/FieldDoc';
import SchemaDoc from './DocExplorer/SchemaDoc';
import SearchResults from './DocExplorer/SearchResults';
import TypeDoc from './DocExplorer/TypeDoc';
export function DocExplorer() {
    var _a = useSchemaContext({ nonNull: true }), fetchError = _a.fetchError, isFetching = _a.isFetching, schema = _a.schema, validationErrors = _a.validationErrors;
    var _b = useExplorerContext({
        nonNull: true,
    }), explorerNavStack = _b.explorerNavStack, pop = _b.pop;
    var navItem = explorerNavStack[explorerNavStack.length - 1];
    var content = null;
    if (fetchError) {
        content = React.createElement("div", { className: "graphiql-error" }, "Error fetching schema");
    }
    else if (validationErrors) {
        content = (React.createElement("div", { className: "graphiql-error" },
            "Schema is invalid: ",
            validationErrors[0].message));
    }
    else if (isFetching) {
        content = React.createElement("div", { className: "graphiql-spinner" });
    }
    else if (!schema) {
        content = React.createElement("div", null, "No GraphQL schema available");
    }
    else if (navItem.search) {
        content = React.createElement(SearchResults, null);
    }
    else if (explorerNavStack.length === 1) {
        content = React.createElement(SchemaDoc, null);
    }
    else if (isType(navItem.def)) {
        content = React.createElement(TypeDoc, null);
    }
    else if (navItem.def) {
        content = React.createElement(FieldDoc, null);
    }
    var prevName;
    if (explorerNavStack.length > 1) {
        prevName = explorerNavStack[explorerNavStack.length - 2].name;
    }
    return (React.createElement("section", { className: "graphiql-doc-explorer", "aria-label": "Documentation Explorer" },
        React.createElement("div", { className: "graphiql-doc-explorer-header" },
            React.createElement("div", { className: "graphiql-doc-explorer-header-content" },
                prevName && (React.createElement("a", { href: "#", className: "graphiql-doc-explorer-back", onClick: pop, "aria-label": "Go back to " + prevName },
                    React.createElement(ChevronLeftIcon, null),
                    prevName)),
                React.createElement("div", { className: "graphiql-doc-explorer-title" }, navItem.title || navItem.name))),
        React.createElement("div", { className: "graphiql-doc-explorer-content" }, content)));
}
//# sourceMappingURL=DocExplorer.js.map