"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocExplorer = void 0;
var react_1 = require("@graphiql/react");
var graphql_1 = require("graphql");
var react_2 = __importDefault(require("react"));
var FieldDoc_1 = __importDefault(require("./DocExplorer/FieldDoc"));
var SchemaDoc_1 = __importDefault(require("./DocExplorer/SchemaDoc"));
var SearchResults_1 = __importDefault(require("./DocExplorer/SearchResults"));
var TypeDoc_1 = __importDefault(require("./DocExplorer/TypeDoc"));
function DocExplorer(props) {
    var _a = (0, react_1.useSchemaContext)({ nonNull: true }), fetchError = _a.fetchError, isFetching = _a.isFetching, schemaFromContext = _a.schema, validationErrors = _a.validationErrors;
    var _b = (0, react_1.useExplorerContext)({
        nonNull: true,
    }), explorerNavStack = _b.explorerNavStack, pop = _b.pop;
    var navItem = explorerNavStack[explorerNavStack.length - 1];
    var schema = props.schema === undefined ? schemaFromContext : props.schema;
    var content = null;
    if (fetchError) {
        content = react_2.default.createElement("div", { className: "graphiql-error" }, "Error fetching schema");
    }
    else if (validationErrors) {
        content = (react_2.default.createElement("div", { className: "graphiql-error" },
            "Schema is invalid: ",
            validationErrors[0].message));
    }
    else if (isFetching) {
        content = react_2.default.createElement(react_1.Spinner, null);
    }
    else if (!schema) {
        content = react_2.default.createElement("div", null, "No GraphQL schema available");
    }
    else if (navItem.search) {
        content = react_2.default.createElement(SearchResults_1.default, null);
    }
    else if (explorerNavStack.length === 1) {
        content = react_2.default.createElement(SchemaDoc_1.default, null);
    }
    else if ((0, graphql_1.isType)(navItem.def)) {
        content = react_2.default.createElement(TypeDoc_1.default, null);
    }
    else if (navItem.def) {
        content = react_2.default.createElement(FieldDoc_1.default, null);
    }
    var prevName;
    if (explorerNavStack.length > 1) {
        prevName = explorerNavStack[explorerNavStack.length - 2].name;
    }
    return (react_2.default.createElement("section", { className: "graphiql-doc-explorer", "aria-label": "Documentation Explorer" },
        react_2.default.createElement("div", { className: "graphiql-doc-explorer-header" },
            react_2.default.createElement("div", { className: "graphiql-doc-explorer-header-content" },
                prevName && (react_2.default.createElement("a", { href: "#", className: "graphiql-doc-explorer-back", onClick: pop, "aria-label": "Go back to ".concat(prevName) },
                    react_2.default.createElement(react_1.ChevronLeftIcon, null),
                    prevName)),
                react_2.default.createElement("div", { className: "graphiql-doc-explorer-title" }, navItem.title || navItem.name))),
        react_2.default.createElement("div", { className: "graphiql-doc-explorer-content" }, content)));
}
exports.DocExplorer = DocExplorer;
//# sourceMappingURL=DocExplorer.js.map