"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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.");
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("@graphiql/react");
var graphql_1 = require("graphql");
var react_2 = __importStar(require("react"));
function TypeDoc() {
    var schema = react_1.useSchemaContext({ nonNull: true }).schema;
    var explorerNavStack = react_1.useExplorerContext({ nonNull: true }).explorerNavStack;
    var navItem = explorerNavStack[explorerNavStack.length - 1];
    var type = navItem.def;
    if (!schema || !graphql_1.isNamedType(type)) {
        return null;
    }
    return (react_2.default.createElement(react_2.default.Fragment, null,
        type.description ? (react_2.default.createElement(react_1.MarkdownContent, { type: "description" }, type.description)) : null,
        react_2.default.createElement(ImplementsInterfaces, { type: type }),
        react_2.default.createElement(Fields, { type: type }),
        react_2.default.createElement(EnumValues, { type: type }),
        react_2.default.createElement(PossibleTypes, { type: type })));
}
exports.default = TypeDoc;
function ImplementsInterfaces(_a) {
    var type = _a.type;
    if (!graphql_1.isObjectType(type)) {
        return null;
    }
    var interfaces = type.getInterfaces();
    return interfaces.length > 0 ? (react_2.default.createElement(react_1.ExplorerSection, { title: "Implements" }, type.getInterfaces().map(function (implementedInterface) { return (react_2.default.createElement("div", { key: implementedInterface.name },
        react_2.default.createElement(react_1.TypeLink, { type: implementedInterface }))); }))) : null;
}
function Fields(_a) {
    var e_1, _b;
    var type = _a.type;
    var _c = __read(react_2.useState(false), 2), showDeprecated = _c[0], setShowDeprecated = _c[1];
    if (!graphql_1.isObjectType(type) &&
        !graphql_1.isInterfaceType(type) &&
        !graphql_1.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_2.default.createElement(react_2.default.Fragment, null,
        fields.length > 0 ? (react_2.default.createElement(react_1.ExplorerSection, { title: "Fields" }, fields.map(function (field) { return (react_2.default.createElement(Field, { key: field.name, field: field })); }))) : null,
        deprecatedFields.length > 0 ? (showDeprecated || fields.length === 0 ? (react_2.default.createElement(react_1.ExplorerSection, { title: "Deprecated Fields" }, deprecatedFields.map(function (field) { return (react_2.default.createElement(Field, { key: field.name, field: field })); }))) : (react_2.default.createElement(react_1.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_2.default.createElement("div", { className: "graphiql-doc-explorer-item" },
        react_2.default.createElement("div", null,
            react_2.default.createElement(react_1.FieldLink, { field: field }),
            args.length > 0 ? (react_2.default.createElement(react_2.default.Fragment, null,
                "(",
                react_2.default.createElement("span", null, args.map(function (arg) {
                    return args.length === 1 ? (react_2.default.createElement(react_1.Argument, { key: arg.name, arg: arg, inline: true })) : (react_2.default.createElement("div", { key: arg.name, className: "graphiql-doc-explorer-argument-multiple" },
                        react_2.default.createElement(react_1.Argument, { arg: arg, inline: true })));
                })),
                ")")) : null,
            ': ',
            react_2.default.createElement(react_1.TypeLink, { type: field.type }),
            react_2.default.createElement(react_1.DefaultValue, { field: field })),
        field.description ? (react_2.default.createElement(react_1.MarkdownContent, { type: "description", onlyShowFirstChild: true }, field.description)) : null,
        field.deprecationReason && (react_2.default.createElement("div", { className: "graphiql-doc-explorer-deprecation" },
            react_2.default.createElement("div", { className: "graphiql-doc-explorer-deprecation-label" }, "Deprecated"),
            react_2.default.createElement(react_1.MarkdownContent, { type: "deprecation", onlyShowFirstChild: true }, field.deprecationReason)))));
}
function EnumValues(_a) {
    var e_2, _b;
    var type = _a.type;
    var _c = __read(react_2.useState(false), 2), showDeprecated = _c[0], setShowDeprecated = _c[1];
    if (!graphql_1.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_2.default.createElement(react_2.default.Fragment, null,
        values.length > 0 ? (react_2.default.createElement(react_1.ExplorerSection, { title: "Enum Values" }, values.map(function (value) { return (react_2.default.createElement(EnumValue, { key: value.name, value: value })); }))) : null,
        deprecatedValues.length > 0 ? (showDeprecated || values.length === 0 ? (react_2.default.createElement(react_1.ExplorerSection, { title: "Deprecated Enum Values" }, deprecatedValues.map(function (value) { return (react_2.default.createElement(EnumValue, { key: value.name, value: value })); }))) : (react_2.default.createElement(react_1.Button, { onClick: function () {
                setShowDeprecated(true);
            } }, "Show deprecated fields"))) : null));
}
function EnumValue(_a) {
    var value = _a.value;
    return (react_2.default.createElement("div", { className: "graphiql-doc-explorer-item" },
        react_2.default.createElement("div", { className: "graphiql-doc-explorer-enum-value" }, value.name),
        value.description ? (react_2.default.createElement(react_1.MarkdownContent, { type: "description" }, value.description)) : null,
        value.deprecationReason ? (react_2.default.createElement(react_1.MarkdownContent, { type: "deprecation" }, value.deprecationReason)) : null));
}
function PossibleTypes(_a) {
    var type = _a.type;
    var schema = react_1.useSchemaContext({ nonNull: true }).schema;
    if (!schema || !graphql_1.isAbstractType(type)) {
        return null;
    }
    return (react_2.default.createElement(react_1.ExplorerSection, { title: graphql_1.isInterfaceType(type) ? 'Implementations' : 'Possible types' }, schema.getPossibleTypes(type).map(function (possibleType) { return (react_2.default.createElement("div", { key: possibleType.name },
        react_2.default.createElement(react_1.TypeLink, { type: possibleType }))); })));
}
//# sourceMappingURL=TypeDoc.js.map