import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { observer } from 'mobx-react-lite';
import { ExecutionNodeViewer, generateExecutionNodeLabel, } from './ExecutionPlanViewer.js';
import { EXECUTION_PLAN_VIEW_MODE, ExecutionPlanViewTreeNodeData, ExecutionNodeTreeNodeData, } from '../../stores/execution-plan/ExecutionPlanState.js';
import { PanelListItem, PanelDivider, Button, PanelContent, ChevronDownIcon, ChevronRightIcon, } from '@finos/legend-art';
import { ResultTypeViewer } from './ResultTypeViewer.js';
import { useState } from 'react';
export const SequenceExecutionNodeViewerHelper = observer((props) => {
    const { node, executionPlanState, viewJson } = props;
    const resultType = node.resultType;
    const [executionNode, setExecutionNode] = useState(undefined);
    const nodeExpandIcon = viewJson ? (_jsx("div", {})) : executionNode ? (_jsx(ChevronDownIcon, {})) : (_jsx(ChevronRightIcon, {}));
    const openExecutionNode = (child) => {
        const newNode = executionPlanState.treeData?.nodes.get(child._UUID);
        if (viewJson === false) {
            if (executionNode) {
                setExecutionNode(undefined);
            }
            else {
                setExecutionNode(child);
            }
        }
        else {
            if (newNode instanceof ExecutionPlanViewTreeNodeData) {
                executionPlanState.transformMetadataToProtocolJson(newNode.executionPlan);
            }
            else if (newNode instanceof ExecutionNodeTreeNodeData) {
                executionPlanState.transformMetadataToProtocolJson(newNode.executionNode);
            }
            if (newNode) {
                executionPlanState.setSelectedNode(newNode);
            }
        }
    };
    return (_jsxs(_Fragment, { children: [_jsx("div", { className: "query-builder__sequence__container", children: _jsxs("div", { children: [_jsx(PanelListItem, { className: "query-builder__sequence__container__item__label", children: "Execution Nodes" }), _jsx(PanelDivider, {}), node.executionNodes.map((child, index) => (_jsx("div", { className: "query-builder__sequence__container__item", children: _jsxs("button", { className: "query-builder__sequence__container__item__btn", onClick: () => openExecutionNode(child), tabIndex: -1, title: `Go to ${generateExecutionNodeLabel(child)}`, children: [!viewJson && nodeExpandIcon, `${index + 1}: ${generateExecutionNodeLabel(child)}`] }) }, child._UUID)))] }) }), _jsx(PanelDivider, {}), _jsx(ResultTypeViewer, { resultType: resultType }), viewJson === false && executionNode && (_jsx(ExecutionNodeViewer, { executionNode: executionNode, executionPlanState: executionPlanState, viewJson: false }))] }));
});
export const SequenceExecutionNodeViewer = observer((props) => {
    const { node, executionPlanState, viewJson } = props;
    const applicationStore = executionPlanState.applicationStore;
    if (viewJson === false) {
        return (_jsx(SequenceExecutionNodeViewerHelper, { node: node, executionPlanState: executionPlanState, viewJson: viewJson }));
    }
    return (_jsxs(PanelContent, { darkMode: !applicationStore.layoutService.TEMPORARY__isLightColorThemeEnabled, children: [_jsx(SequenceExecutionNodeViewerHelper, { node: node, executionPlanState: executionPlanState, viewJson: viewJson }), _jsx("div", { className: "query-builder__execution__container", children: _jsx(Button, { className: "btn--dark execution-node-viewer__unsupported-view__to-text-mode__btn", onClick: () => executionPlanState.setViewMode(EXECUTION_PLAN_VIEW_MODE.JSON), text: "View JSON" }) }), _jsx(PanelDivider, {})] }));
});
//# sourceMappingURL=SequenceExecutionNodeViewer.js.map