"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestEditor = exports.RequestEditorTab = void 0;
const tslib_1 = require("tslib");
const ui_kit_1 = require("@stoplight/ui-kit");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const mobx_react_lite_1 = require("mobx-react-lite");
const React = tslib_1.__importStar(require("react"));
const useRequestMaker_1 = require("../../../hooks/useRequestMaker");
const params_1 = require("../../../utils/params");
const TabTitle_1 = require("../TabTitle");
const Body_1 = require("./Body");
const CodeGenerator_1 = require("./CodeGenerator");
const Headers_1 = require("./Headers");
const Mocking_1 = require("./Mocking");
const Parameters_1 = require("./Parameters");
const panelClassName = 'bg-white dark:bg-transparent border-t';
var RequestEditorTab;
(function (RequestEditorTab) {
    RequestEditorTab["HEADERS"] = "headers";
    RequestEditorTab["BODY"] = "body";
    RequestEditorTab["QUERY"] = "query";
    RequestEditorTab["PATH"] = "path";
    RequestEditorTab["CODE"] = "code";
    RequestEditorTab["MOCKING"] = "mocking";
})(RequestEditorTab = exports.RequestEditorTab || (exports.RequestEditorTab = {}));
const defaultAvailableTabs = Object.values(RequestEditorTab);
exports.RequestEditor = mobx_react_lite_1.observer(({ tabs = defaultAvailableTabs, className }) => {
    const requestStore = useRequestMaker_1.useRequestMakerStore('request');
    const responseStore = useRequestMaker_1.useRequestMakerStore('response');
    const store = useRequestMaker_1.useRequestMakerStore();
    const shouldRenderQuery = tabs.includes(RequestEditorTab.QUERY);
    const shouldRenderHeaders = tabs.includes(RequestEditorTab.HEADERS);
    const shouldRenderBody = tabs.includes(RequestEditorTab.BODY);
    const shouldRenderPath = tabs.includes(RequestEditorTab.PATH);
    const shouldRenderCodeGen = tabs.includes(RequestEditorTab.CODE);
    const shouldRenderMocking = tabs.includes(RequestEditorTab.MOCKING) && store.operation;
    let defaultTab = RequestEditorTab.QUERY;
    if (shouldRenderQuery) {
        defaultTab = RequestEditorTab.QUERY;
    }
    else if (shouldRenderHeaders) {
        defaultTab = RequestEditorTab.HEADERS;
    }
    else if (shouldRenderBody) {
        defaultTab = RequestEditorTab.BODY;
    }
    else if (shouldRenderPath) {
        defaultTab = RequestEditorTab.PATH;
    }
    else if (shouldRenderCodeGen) {
        defaultTab = RequestEditorTab.CODE;
    }
    else if (shouldRenderMocking) {
        defaultTab = RequestEditorTab.MOCKING;
    }
    const [selectedTabId, setSelectedTabId] = React.useState(`request-${defaultTab}`);
    return (React.createElement("div", { className: classnames_1.default('RequestMaker__RequestEditor border-l border-r border-b bg-gray-1 dark:bg-transparent', className, {
            'rounded-b': responseStore.statusCode === 0,
        }) },
        React.createElement(ui_kit_1.Tabs, { id: "RequestMaker__RequestEditor-tabs", className: "RequestMaker__RequestEditor-tabs", selectedTabId: selectedTabId, onChange: (newTabId, prevTabId, e) => {
                e.preventDefault();
                e.stopPropagation();
                setSelectedTabId(String(newTabId));
            }, renderActiveTabPanelOnly: true },
            shouldRenderQuery && (React.createElement(ui_kit_1.Tab, { id: "request-query", title: React.createElement(TabTitle_1.TabTitle, { title: "Query", count: params_1.getEnabledParams(requestStore.queryParams).length }), panelClassName: panelClassName, panel: React.createElement(Parameters_1.RequestParameters, { type: "query" }) })),
            shouldRenderHeaders && (React.createElement(ui_kit_1.Tab, { id: "request-headers", title: React.createElement(TabTitle_1.TabTitle, { title: "Headers", count: params_1.getEnabledParams(requestStore.headerParams).length }), panelClassName: panelClassName, panel: React.createElement(Headers_1.RequestHeaders, null) })),
            shouldRenderBody && (React.createElement(ui_kit_1.Tab, { id: "request-body", title: React.createElement(TabTitle_1.TabTitle, { title: "Body", count: requestStore.bodyCount }), panelClassName: panelClassName, panel: React.createElement(Body_1.RequestBody, null) })),
            shouldRenderPath && (React.createElement(ui_kit_1.Tab, { id: "request-path", title: React.createElement(TabTitle_1.TabTitle, { title: "Path", count: params_1.getEnabledParams(requestStore.pathParams).length }), panelClassName: panelClassName, panel: React.createElement(Parameters_1.RequestParameters, { type: "path" }) })),
            shouldRenderCodeGen && (React.createElement(ui_kit_1.Tab, { id: "code-editor", title: React.createElement(TabTitle_1.TabTitle, { title: "Code Generation" }), panelClassName: panelClassName, panel: React.createElement(CodeGenerator_1.CodeGenerator, null) })),
            shouldRenderMocking && (React.createElement(ui_kit_1.Tab, { id: "mock-editor", title: React.createElement(TabTitle_1.TabTitle, { title: "Mocking" }), panelClassName: panelClassName, panel: React.createElement(Mocking_1.Mocking, null) })),
            React.createElement(ui_kit_1.Tabs.Expander, null))));
});
exports.RequestEditor.displayName = 'RequestMaker.RequestEditor';
//# sourceMappingURL=Editor.js.map