var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { Argument, Button, DefaultValue, ExplorerSection, FieldLink, MarkdownContent, TypeLink, useExplorerContext, useSchemaContext, } from '@graphiql/react';
import { isAbstractType, isEnumType, isInputObjectType, isInterfaceType, isNamedType, isObjectType, } from 'graphql';
import React, { useState } from 'react';
export default function TypeDoc() {
    var schema = useSchemaContext({ nonNull: true }).schema;
    var explorerNavStack = useExplorerContext({ nonNull: true }).explorerNavStack;
    var navItem = explorerNavStack[explorerNavStack.length - 1];
    var type = navItem.def;
    if (!schema || !isNamedType(type)) {
        return null;
    }
    return (React.createElement(React.Fragment, null,
        type.description ? (React.createElement(MarkdownContent, { type: "description" }, type.description)) : null,
        React.createElement(ImplementsInterfaces, { type: type }),
        React.createElement(Fields, { type: type }),
        React.createElement(EnumValues, { type: type }),
        React.createElement(PossibleTypes, { type: type })));
}
function ImplementsInterfaces(_a) {
    var type = _a.type;
    if (!isObjectType(type)) {
        return null;
    }
    var interfaces = type.getInterfaces();
    return interfaces.length > 0 ? (React.createElement(ExplorerSection, { title: "Implements" }, type.getInterfaces().map(function (implementedInterface) { return (React.createElement("div", { key: implementedInterface.name },
        React.createElement(TypeLink, { type: implementedInterface }))); }))) : null;
}
function Fields(_a) {
    var e_1, _b;
    var type = _a.type;
    var _c = __read(useState(false), 2), showDeprecated = _c[0], setShowDeprecated = _c[1];
    if (!isObjectType(type) &&
        !isInterfaceType(type) &&
        !isInputObjectType(type)) {
        return null;
    }
    var fieldMap = type.getFields();
    var fields = [];
    var deprecatedFields = [];
    try {
        for (var _d = __values(Object.keys(fieldMap).map(function (name) { return fieldMap[name]; })), _e = _d.next(); !_e.done; _e = _d.next()) {
            var field = _e.value;
            if (field.deprecationReason) {
                deprecatedFields.push(field);
            }
            else {
                fields.push(field);
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return (React.createElement(React.Fragment, null,
        fields.length > 0 ? (React.createElement(ExplorerSection, { title: "Fields" }, fields.map(function (field) { return (React.createElement(Field, { key: field.name, field: field })); }))) : null,
        deprecatedFields.length > 0 ? (showDeprecated || fields.length === 0 ? (React.createElement(ExplorerSection, { title: "Deprecated Fields" }, deprecatedFields.map(function (field) { return (React.createElement(Field, { key: field.name, field: field })); }))) : (React.createElement(Button, { onClick: function () {
                setShowDeprecated(true);
            } }, "Show deprecated fields"))) : null));
}
function Field(_a) {
    var field = _a.field;
    var args = 'args' in field ? field.args.filter(function (arg) { return !arg.deprecationReason; }) : [];
    return (React.createElement("div", { className: "graphiql-doc-explorer-item" },
        React.createElement("div", null,
            React.createElement(FieldLink, { field: field }),
            args.length > 0 ? (React.createElement(React.Fragment, null,
                "(",
                React.createElement("span", null, args.map(function (arg) {
                    return args.length === 1 ? (React.createElement(Argument, { key: arg.name, arg: arg, inline: true })) : (React.createElement("div", { key: arg.name, className: "graphiql-doc-explorer-argument-multiple" },
                        React.createElement(Argument, { arg: arg, inline: true })));
                })),
                ")")) : null,
            ': ',
            React.createElement(TypeLink, { type: field.type }),
            React.createElement(DefaultValue, { field: field })),
        field.description ? (React.createElement(MarkdownContent, { type: "description", onlyShowFirstChild: true }, field.description)) : null,
        field.deprecationReason && (React.createElement("div", { className: "graphiql-doc-explorer-deprecation" },
            React.createElement("div", { className: "graphiql-doc-explorer-deprecation-label" }, "Deprecated"),
            React.createElement(MarkdownContent, { type: "deprecation", onlyShowFirstChild: true }, field.deprecationReason)))));
}
function EnumValues(_a) {
    var e_2, _b;
    var type = _a.type;
    var _c = __read(useState(false), 2), showDeprecated = _c[0], setShowDeprecated = _c[1];
    if (!isEnumType(type)) {
        return null;
    }
    var values = [];
    var deprecatedValues = [];
    try {
        for (var _d = __values(type.getValues()), _e = _d.next(); !_e.done; _e = _d.next()) {
            var value = _e.value;
            if (value.deprecationReason) {
                deprecatedValues.push(value);
            }
            else {
                values.push(value);
            }
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
        }
        finally { if (e_2) throw e_2.error; }
    }
    return (React.createElement(React.Fragment, null,
        values.length > 0 ? (React.createElement(ExplorerSection, { title: "Enum Values" }, values.map(function (value) { return (React.createElement(EnumValue, { key: value.name, value: value })); }))) : null,
        deprecatedValues.length > 0 ? (showDeprecated || values.length === 0 ? (React.createElement(ExplorerSection, { title: "Deprecated Enum Values" }, deprecatedValues.map(function (value) { return (React.createElement(EnumValue, { key: value.name, value: value })); }))) : (React.createElement(Button, { onClick: function () {
                setShowDeprecated(true);
            } }, "Show deprecated fields"))) : null));
}
function EnumValue(_a) {
    var value = _a.value;
    return (React.createElement("div", { className: "graphiql-doc-explorer-item" },
        React.createElement("div", { className: "graphiql-doc-explorer-enum-value" }, value.name),
        value.description ? (React.createElement(MarkdownContent, { type: "description" }, value.description)) : null,
        value.deprecationReason ? (React.createElement(MarkdownContent, { type: "deprecation" }, value.deprecationReason)) : null));
}
function PossibleTypes(_a) {
    var type = _a.type;
    var schema = useSchemaContext({ nonNull: true }).schema;
    if (!schema || !isAbstractType(type)) {
        return null;
    }
    return (React.createElement(ExplorerSection, { title: isInterfaceType(type) ? 'Implementations' : 'Possible types' }, schema.getPossibleTypes(type).map(function (possibleType) { return (React.createElement("div", { key: possibleType.name },
        React.createElement(TypeLink, { type: possibleType }))); })));
}
//# sourceMappingURL=TypeDoc.js.map