import { jsx as _jsx, jsxs as _jsxs } 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 { useApplicationStore } from '@finos/legend-application';
import { ArrowCircleLeftIcon, PanelContent, PanelHeader, PanelHeaderActionItem, PanelHeaderActions, RefreshIcon, ResizablePanel, ResizablePanelGroup, ResizablePanelSplitter, ResizablePanelSplitterLine, } from '@finos/legend-art';
import { debounce } from '@finos/legend-shared';
import { flowResult } from 'mobx';
import { observer } from 'mobx-react-lite';
import { useEffect, useMemo } from 'react';
import { ModelUnitEditor } from '../external-format-editor/DSL_ExternalFormat_BindingElementEditor.js';
import { GenerationPropertyEditor } from './FileGenerationEditor.js';
import { FileSystemViewer } from './FileSystemViewer.js';
var HIDDEN_CONFIGURATION_PROPERTIES;
(function (HIDDEN_CONFIGURATION_PROPERTIES) {
    HIDDEN_CONFIGURATION_PROPERTIES["FORMAT"] = "format";
    HIDDEN_CONFIGURATION_PROPERTIES["TARGET_SCHEMA_SET"] = "targetSchemaSet";
})(HIDDEN_CONFIGURATION_PROPERTIES || (HIDDEN_CONFIGURATION_PROPERTIES = {}));
export const ExternalFormatGeneratioConfigEditor = observer((props) => {
    const { isReadOnly, xtState } = props;
    const xtGenerationState = xtState.xtGenerationState;
    const editorStore = xtState.editorStore;
    const applicationStore = editorStore.applicationStore;
    const configSpecification = xtState.xtGenerationState.configSpecification;
    const schemaGenerationProperties = xtState.description.schemaGenerationProperties;
    const debouncedRegenerate = useMemo(() => debounce(() => flowResult(xtState.xtGenerationState.generate()), 500), [xtState]);
    const update = (generationProperty, newValue) => {
        debouncedRegenerate.cancel();
        xtGenerationState.updateFileGenerationParameters(configSpecification, generationProperty, newValue);
        debouncedRegenerate()?.catch(applicationStore.alertUnhandledError);
    };
    const resetDefaultConfiguration = () => {
        debouncedRegenerate.cancel();
        xtGenerationState.resetGenerator();
        debouncedRegenerate()?.catch(applicationStore.alertUnhandledError);
    };
    const getConfigValue = (name) => xtState.xtGenerationState.configSpecification.configurationProperties.find((e) => e.name === name)?.value;
    return (_jsxs("div", { className: "panel file-generation-editor__configuration", children: [_jsx(PanelHeader, { title: `${xtState.description.name} configuration`, children: _jsx(PanelHeaderActions, { children: _jsx(PanelHeaderActionItem, { className: "file-generation-editor__configuration__reset-btn", disabled: isReadOnly ||
                            !configSpecification.configurationProperties.length, onClick: resetDefaultConfiguration, title: "Reset to default configuration", children: _jsx(RefreshIcon, {}) }) }) }), _jsx(PanelContent, { children: _jsxs("div", { className: "file-generation-editor__configuration__content", children: [_jsx(ModelUnitEditor, { isReadOnly: isReadOnly, modelUnit: configSpecification.modelUnit }), schemaGenerationProperties
                            .filter((property) => !Object.values(HIDDEN_CONFIGURATION_PROPERTIES).includes(property.name))
                            .map((abstractGenerationProperty) => (_jsx(GenerationPropertyEditor, { update: update, isReadOnly: isReadOnly, getConfigValue: getConfigValue, property: abstractGenerationProperty }, abstractGenerationProperty.name +
                            abstractGenerationProperty.type)))] }) })] }));
});
export const ElementXTGenerationEditor = observer((props) => {
    const { elementXTState, currentElementState } = props;
    const xtGenerationState = elementXTState.xtGenerationState;
    const applicationStore = useApplicationStore();
    const leaveElementGenerationView = () => currentElementState.setGenerationModeState(undefined);
    useEffect(() => {
        flowResult(elementXTState.regenerate()).catch(applicationStore.alertUnhandledError);
    }, [applicationStore, currentElementState, elementXTState]);
    return (_jsxs("div", { className: "panel element-generation-editor", children: [_jsx("div", { className: "panel__header element-generation-editor__header", children: _jsx("div", { className: "panel__header__title", children: _jsxs("button", { className: "panel__header__action element-generation-editor__leave-btn", tabIndex: -1, onClick: leaveElementGenerationView, title: "Leave element generation view mode", children: [_jsx(ArrowCircleLeftIcon, {}), " exit generation view"] }) }) }), _jsx("div", { className: "panel__content element-generation-editor__content", children: _jsx("div", { className: "file-generation-editor", children: _jsxs(ResizablePanelGroup, { orientation: "vertical", children: [_jsx(ResizablePanel, { size: 300, minSize: 300, className: "file-generation-editor__split-pane", children: _jsx(ExternalFormatGeneratioConfigEditor, { isReadOnly: currentElementState.isReadOnly, elementEditorState: currentElementState, xtState: elementXTState }) }), _jsx(ResizablePanelSplitter, { children: _jsx(ResizablePanelSplitterLine, { color: "var(--color-dark-grey-200)" }) }), _jsx(ResizablePanel, { children: _jsx(FileSystemViewer, { generatedFileState: xtGenerationState }) })] }) }) })] }));
});
//# sourceMappingURL=ElementXTGenerationEditor.js.map