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 { useEditorStore } from '../../EditorStoreProvider.js';
import { Dialog, Modal, ModalBody, ModalFooter, ModalFooterButton, ModalHeader, ModalTitle, PanelContent, PanelHeader, PanelHeaderActions, EyeIcon, ModalHeaderActions, CustomSelectorInput, } from '@finos/legend-art';
import { IngestDefinitionEditorState } from '../../../../stores/editor/editor-state/element-editor-state/ingest/IngestDefinitionEditorState.js';
import { CodeEditor } from '@finos/legend-lego/code-editor';
import React, { useEffect, useState } from 'react';
import { CODE_EDITOR_LANGUAGE } from '@finos/legend-code-editor';
import { flowResult } from 'mobx';
import { useApplicationNavigationContext } from '@finos/legend-application';
import { LEGEND_STUDIO_APPLICATION_NAVIGATION_CONTEXT_KEY } from '../../../../__lib__/LegendStudioApplicationNavigationContext.js';
import { LINEAGE_VIEW_MODE, LineageViewerContent, } from '@finos/legend-query-builder';
export const IngestLineageModal = observer((props) => {
    const { ingestDefinitionEditorState } = props;
    const matviewFunctions = ingestDefinitionEditorState.ingest.TEMPORARY_MATVIEW_FUNCTION_DATA_SETS;
    const matviewNames = ingestDefinitionEditorState.ingest.TEMPORARY_MATVIEW_FUNCTION_DATA_SETS?.map((dataset) => dataset.name) ?? [];
    const [selectedMatview, setSelectedMatview] = useState(undefined);
    useEffect(() => {
        if (!selectedMatview && matviewFunctions && matviewFunctions.length > 0) {
            setSelectedMatview(matviewFunctions[0]);
        }
    }, [matviewFunctions, selectedMatview]);
    useEffect(() => {
        ingestDefinitionEditorState.lineageState.setSelectedTab(LINEAGE_VIEW_MODE.DATABASE_LINEAGE);
    }, [ingestDefinitionEditorState.lineageState]);
    if (!matviewFunctions || matviewFunctions.length === 0) {
        return null;
    }
    const isDarkMode = !ingestDefinitionEditorState.lineageState.applicationStore.layoutService
        .TEMPORARY__isLightColorThemeEnabled;
    const closeLineageViewer = () => {
        ingestDefinitionEditorState.lineageState.setLineageData(undefined);
        ingestDefinitionEditorState.lineageState.setSelectedTab(LINEAGE_VIEW_MODE.DATABASE_LINEAGE);
        ingestDefinitionEditorState.lineageState.clearPropertySelections();
    };
    const datasetOptions = matviewNames
        .map((name, index) => {
        const dataset = matviewFunctions[index];
        if (dataset) {
            return {
                label: name,
                value: dataset,
            };
        }
        return null;
    })
        .filter((option) => option !== null);
    const handleDatasetChange = (option) => {
        if (option?.value) {
            setSelectedMatview(option.value);
            flowResult(ingestDefinitionEditorState.generateLineage(option.value)).catch(ingestDefinitionEditorState.editorStore.applicationStore
                .alertUnhandledError);
        }
    };
    return (_jsx(_Fragment, { children: _jsx(Dialog, { open: Boolean(ingestDefinitionEditorState.lineageState.lineageData), onClose: closeLineageViewer, children: _jsxs(Modal, { className: "editor-modal", darkMode: isDarkMode, children: [_jsxs(ModalHeader, { children: [_jsx(ModalTitle, { title: "LineageViewer" }), _jsx(ModalHeaderActions, { children: _jsx(CustomSelectorInput, { options: datasetOptions, onChange: handleDatasetChange, value: selectedMatview
                                        ? {
                                            label: selectedMatview.name,
                                            value: selectedMatview,
                                        }
                                        : null, darkMode: isDarkMode }) })] }), _jsx(ModalBody, { children: _jsx("div", { className: "lineage-viewer", style: { height: '100%' }, children: _jsx(LineageViewerContent, { lineageState: ingestDefinitionEditorState.lineageState }) }) }), _jsx(ModalFooter, { className: "editor-modal__footer", children: _jsx(ModalFooterButton, { onClick: closeLineageViewer, text: "Close", type: "secondary" }) })] }) }) }));
});
export const IngestDefinitionEditor = observer(() => {
    const editorStore = useEditorStore();
    const ingestDefinitionEditorState = editorStore.tabManagerState.getCurrentEditorState(IngestDefinitionEditorState);
    const ingestDef = ingestDefinitionEditorState.ingest;
    const isValidForLineage = ingestDefinitionEditorState.validForLineageViewer;
    useEffect(() => {
        ingestDefinitionEditorState.generateElementGrammar();
    }, [ingestDefinitionEditorState]);
    useApplicationNavigationContext(LEGEND_STUDIO_APPLICATION_NAVIGATION_CONTEXT_KEY.INGEST_DEFINITION_EDITOR);
    const viewLineage = () => {
        const firstMatViewQuery = ingestDefinitionEditorState.ingest
            .TEMPORARY_MATVIEW_FUNCTION_DATA_SETS?.[0];
        if (firstMatViewQuery) {
            flowResult(ingestDefinitionEditorState.generateLineage(firstMatViewQuery)).catch(editorStore.applicationStore.alertUnhandledError);
        }
        else {
            editorStore.applicationStore.notificationService.notifyError('No MatView datasets available for lineage generation');
        }
    };
    return (_jsxs("div", { className: "data-product-editor", children: [_jsx(PanelHeader, { title: "Ingest", titleContent: ingestDef.name, darkMode: true, isReadOnly: true }), _jsxs(PanelContent, { children: [_jsx(PanelHeader, { title: "deployment", darkMode: true, children: _jsx(PanelHeaderActions, { children: _jsx("div", { className: "panel__header__actions", children: _jsx("div", { className: "btn__dropdown-combo btn__dropdown-combo--primary", children: _jsxs("button", { className: "btn__dropdown-combo__label", onClick: viewLineage, tabIndex: -1, disabled: !isValidForLineage, children: [_jsx(EyeIcon, { className: "btn__dropdown-combo__label__icon" }), _jsx("div", { className: "btn__dropdown-combo__label__title", children: "Lineage" })] }) }) }) }) }), _jsx(PanelContent, { children: _jsx(CodeEditor, { inputValue: ingestDefinitionEditorState.textContent, isReadOnly: true, language: CODE_EDITOR_LANGUAGE.PURE }) }), _jsx(IngestLineageModal, { ingestDefinitionEditorState: ingestDefinitionEditorState })] })] }));
});
//# sourceMappingURL=IngestDefinitionEditor.js.map