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 { isValidFullPath } from '@finos/legend-graph';
import { flowResult } from 'mobx';
import { observer } from 'mobx-react-lite';
import { useMemo } from 'react';
import { debounce } from '@finos/legend-shared';
import { InputWithInlineValidation, Panel, PanelContent, PanelHeader, PanelHeaderActionItem, PanelHeaderActions, PanelLoadingIndicator, RefreshIcon, ResizablePanel, ResizablePanelGroup, ResizablePanelSplitter, ResizablePanelSplitterLine, } from '@finos/legend-art';
import { GenerationPropertyEditor } from '../element-generation-editor/FileGenerationEditor.js';
import { CODE_EDITOR_LANGUAGE } from '@finos/legend-code-editor';
import { CodeEditor } from '@finos/legend-lego/code-editor';
import { useApplicationNavigationContext } from '@finos/legend-application';
import { LEGEND_STUDIO_APPLICATION_NAVIGATION_CONTEXT_KEY } from '../../../../__lib__/LegendStudioApplicationNavigationContext.js';
var HIDDEN_CONFIGURATION_PROPERTIES;
(function (HIDDEN_CONFIGURATION_PROPERTIES) {
    HIDDEN_CONFIGURATION_PROPERTIES["FORMAT"] = "format";
})(HIDDEN_CONFIGURATION_PROPERTIES || (HIDDEN_CONFIGURATION_PROPERTIES = {}));
export const SchemaSetModelGenerationEditor = observer((props) => {
    const { modelGenerationState, isReadOnly } = props;
    const applicationStore = modelGenerationState.editorStore.applicationStore;
    const format = modelGenerationState.schemaSet.format;
    const description = modelGenerationState.description;
    const properties = description?.modelGenerationProperties ?? [];
    const debouncedRegenerate = useMemo(() => debounce(() => flowResult(modelGenerationState.generate()), 500), [modelGenerationState]);
    const update = (generationProperty, newValue) => {
        debouncedRegenerate.cancel();
        modelGenerationState.updateGenerationParameters(generationProperty, newValue);
        debouncedRegenerate()?.catch(applicationStore.alertUnhandledError);
    };
    const regenerate = () => {
        modelGenerationState.generate();
    };
    const resetProperties = () => {
        modelGenerationState.setConfigurationProperty([]);
    };
    const getConfigValue = (name) => modelGenerationState.getConfigValue(name);
    const targetBindingPathValidationMessage = Boolean(modelGenerationState.targetBinding) &&
        !isValidFullPath(modelGenerationState.targetBinding)
        ? 'Invalid target binding path'
        : undefined;
    const changeTargetBindingPath = (event) => {
        modelGenerationState.setTargetBindingPath(event.target.value);
        if (!modelGenerationState.targetBinding ||
            (modelGenerationState.targetBinding &&
                isValidFullPath(modelGenerationState.targetBinding))) {
            modelGenerationState.handleTargetBindingPathChange();
            debouncedRegenerate()?.catch(applicationStore.alertUnhandledError);
        }
    };
    const importGeneratedElements = () => {
        modelGenerationState.importGeneratedModelsIntoGraph();
    };
    useApplicationNavigationContext(LEGEND_STUDIO_APPLICATION_NAVIGATION_CONTEXT_KEY.SCHEMA_SET_MODEL_GENERATION);
    return (_jsx(PanelContent, { className: "file-generation-editor__content", children: _jsxs(ResizablePanelGroup, { orientation: "vertical", children: [_jsx(ResizablePanel, { size: 250, minSize: 50, children: _jsxs(Panel, { className: "file-generation-editor__configuration", children: [_jsx(PanelHeader, { title: `${format} configuration`, children: _jsx(PanelHeaderActions, { children: _jsx(PanelHeaderActionItem, { className: "file-generation-editor__configuration__reset-btn", disabled: isReadOnly || !properties.length, onClick: resetProperties, title: "Reset to default configuration", children: _jsx(RefreshIcon, {}) }) }) }), _jsx(PanelContent, { children: _jsxs("div", { className: "file-generation-editor__configuration__content", children: [_jsxs("div", { className: "panel__content__form__section", children: [_jsx("div", { className: "panel__content__form__section__header__label", children: 'Target Binding Path' }), _jsx("div", { className: "panel__content__form__section__header__prompt", children: 'If path is provided, a binding will be generated alongside the models. The binding will ensure the schema and models are compatible through compile time validations.' }), _jsx(InputWithInlineValidation, { className: "query-builder__variables__variable__name__input input-group__input", spellCheck: false, value: modelGenerationState.targetBinding, onChange: changeTargetBindingPath, placeholder: "Target binding path", error: targetBindingPathValidationMessage })] }), modelGenerationState.modelGenerationProperties
                                            .filter((property) => !Object.values(HIDDEN_CONFIGURATION_PROPERTIES).includes(property.name))
                                            .map((abstractGenerationProperty) => (_jsx(GenerationPropertyEditor, { update: update, isReadOnly: isReadOnly, getConfigValue: getConfigValue, property: abstractGenerationProperty }, abstractGenerationProperty.name)))] }) })] }) }), _jsx(ResizablePanelSplitter, { children: _jsx(ResizablePanelSplitterLine, { color: "var(--color-dark-grey-200)" }) }), _jsx(ResizablePanel, { children: _jsxs(Panel, { className: "generation-result-viewer__file", children: [_jsx(PanelHeader, { title: "result", children: _jsxs(PanelHeaderActions, { children: [_jsx(PanelHeaderActionItem, { className: "generation-result-viewer__generate-btn", onClick: regenerate, disabled: modelGenerationState.generatingModelsState.isInProgress ||
                                                modelGenerationState.importGeneratedElementsState
                                                    .isInProgress, title: "Regenerate", children: _jsxs("div", { className: "generation-result-viewer__generate-btn__label", children: [_jsx(RefreshIcon, { className: "generation-result-viewer__generate-btn__label__icon" }), _jsx("div", { className: "generation-result-viewer__generate-btn__label__title", children: "Generate" })] }) }), !modelGenerationState.isolatedGraph && (_jsx("button", { className: "btn--dark model-loader__header__load-btn", onClick: importGeneratedElements, disabled: modelGenerationState.generationValue === '', tabIndex: -1, title: "Import generated elements", children: "Import" }))] }) }), _jsxs(PanelContent, { children: [_jsx(PanelLoadingIndicator, { isLoading: modelGenerationState.generatingModelsState.isInProgress ||
                                            modelGenerationState.importGeneratedElementsState
                                                .isInProgress }), _jsx(CodeEditor, { inputValue: modelGenerationState.generationValue, isReadOnly: true, language: CODE_EDITOR_LANGUAGE.PURE })] })] }) })] }) }));
});
//# sourceMappingURL=DSL_ExternalFormat_SchemaSetModelGenerationEditor.js.map