"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultComponents = exports.useComponents = void 0;
const tslib_1 = require("tslib");
const markdown_viewer_1 = require("@stoplight/markdown-viewer");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const lodash_1 = require("lodash");
const React = tslib_1.__importStar(require("react"));
const utils_1 = require("../components/Docs/HttpOperation/utils");
const HttpRequest_1 = require("../components/Docs/HttpRequest");
const SchemaViewer_1 = require("../components/SchemaViewer");
const Provider_1 = require("../containers/Provider");
const guards_1 = require("../utils/guards");
const useParsedValue_1 = require("./useParsedValue");
function useComponents() {
    const Components = React.useContext(Provider_1.ComponentsContext);
    return React.useMemo(() => {
        return Object.assign(Object.assign({}, exports.defaultComponents), Components);
    }, [Components]);
}
exports.useComponents = useComponents;
const CodeComponent = (props) => {
    const { node } = props;
    const { annotations } = node;
    const nodeType = lodash_1.get(annotations, 'type') || node.meta;
    if (['json_schema', 'http'].includes(nodeType)) {
        return React.createElement(WrapperComponent, Object.assign({}, props));
    }
    const DefaultCode = markdown_viewer_1.defaultComponentMapping.code;
    return React.createElement(DefaultCode, Object.assign({}, props));
};
const WrapperComponent = ({ node, parent }) => {
    const { annotations, value } = node;
    const parsedValue = useParsedValue_1.useParsedValue(value);
    const nodeType = lodash_1.get(annotations, 'type') || node.meta;
    if (nodeType === 'json_schema' && guards_1.isJSONSchema(parsedValue)) {
        return (React.createElement(SchemaViewer_1.SchemaViewer, { title: annotations === null || annotations === void 0 ? void 0 : annotations.title, schema: parsedValue, examples: utils_1.getExamplesFromSchema(parsedValue) }));
    }
    else if (nodeType === 'http' && lodash_1.isObject(parsedValue)) {
        return (React.createElement(HttpRequest_1.HttpRequest, { className: classnames_1.default('my-10', {
                [markdown_viewer_1.CLASSNAMES.block]: !parent || parent.type !== 'tab',
            }), data: guards_1.isHttpRequest(parsedValue) ? parsedValue : Object.assign({ method: 'get' }, parsedValue) }));
    }
    return null;
};
exports.defaultComponents = {
    code: CodeComponent,
    link: markdown_viewer_1.defaultComponentMapping.link,
};
//# sourceMappingURL=useComponents.js.map