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 { observer } from 'mobx-react-lite';
import { PanelFormBooleanField, Panel, PanelFormTextField, PanelForm, CloudDownloadIcon, PanelFormListItems, } from '@finos/legend-art';
import { ContentType, downloadFileUsingDataURI, getContentTypeFileExtension, isValidUrl, } from '@finos/legend-shared';
import { useEditorStore } from '../EditorStoreProvider.js';
import { LEGEND_STUDIO_SETTING_KEY } from '../../../__lib__/LegendStudioSetting.js';
import { flowResult } from 'mobx';
import { PARSER_SECTION_MARKER, PURE_PARSER, } from '@finos/legend-graph';
export const DevToolPanel = observer(() => {
    const editorStore = useEditorStore();
    // Engine
    const engineConfig = editorStore.graphManagerState.graphManager.TEMPORARY__getEngineConfig();
    const toggleEngineClientRequestPayloadCompression = () => engineConfig.setUseClientRequestPayloadCompression(!engineConfig.useClientRequestPayloadCompression);
    const toggleEngineClientRequestPayloadDebugging = () => engineConfig.setEnableDebuggingPayload(!engineConfig.enableDebuggingPayload);
    const toggleEngineClientDataURLEncoding = () => engineConfig.setUseBase64ForAdhocConnectionDataUrls(!engineConfig.useBase64ForAdhocConnectionDataUrls);
    const toggleSetUseDevClientProtocol = () => engineConfig.setUseDevClientProtocol(!engineConfig.useDevClientProtocol);
    // Graph Manager
    const toggleStrictMode = () => {
        editorStore.graphState.setEnableStrictMode(!editorStore.graphState.enableStrictMode);
        editorStore.applicationStore.settingService.persistValue(LEGEND_STUDIO_SETTING_KEY.EDITOR_STRICT_MODE, editorStore.graphState.enableStrictMode);
    };
    const toggleArtifactGeneration = () => {
        editorStore.graphState.graphGenerationState.setEnableArtifactGeneration(!editorStore.graphState.graphGenerationState.enableArtifactGeneration);
    };
    const downloadDependencyProjectGrammars = async () => {
        const grammars = await Promise.all(Array.from(editorStore.graphManagerState.graph.dependencyManager
            .projectDependencyModelsIndex).map((graph) => flowResult(editorStore.graphManagerState.graphManager.graphToPureCode(graph[1], {
            pretty: true,
        }))));
        return grammars;
    };
    const downloadProjectGrammar = async (withDependency) => {
        const graphGrammar = (await Promise.all([
            flowResult(editorStore.graphManagerState.graphManager.graphToPureCode(editorStore.graphManagerState.graph, { pretty: true })),
        ]));
        const dependencyGrammars = withDependency
            ? (await Promise.all([
                flowResult(downloadDependencyProjectGrammars()),
            ]))
            : [];
        const fullGrammar = [graphGrammar, ...dependencyGrammars].join(`\n${PARSER_SECTION_MARKER}${PURE_PARSER.PURE}\n`);
        const fileName = `grammar.${getContentTypeFileExtension(ContentType.TEXT_PLAIN)}`;
        downloadFileUsingDataURI(fileName, `${fullGrammar}`, ContentType.TEXT_PLAIN);
    };
    return (_jsx(Panel, { children: _jsxs(PanelForm, { children: [_jsx(PanelFormBooleanField, { name: "Engine client request payload compression", prompt: "Specifies if request payload should be compressed", value: engineConfig.useClientRequestPayloadCompression, isReadOnly: false, update: toggleEngineClientRequestPayloadCompression }), _jsx(PanelFormBooleanField, { name: "Engine client request payload debug", prompt: "Specifies if request payload should be downloaded for debugging purpose", value: engineConfig.enableDebuggingPayload, isReadOnly: false, update: toggleEngineClientRequestPayloadDebugging }), _jsx(PanelFormTextField, { name: "Engine client base URL", value: engineConfig.baseUrl ?? '', isReadOnly: false, update: (value) => engineConfig.setBaseUrl(value === '' ? undefined : value), errorMessage: !isValidUrl(engineConfig.baseUrl ?? '') ? 'Invalid URL' : '' }), _jsx(PanelFormBooleanField, { name: "Use Dev client protocol version", prompt: "Specifies if development client protocol (v_X_X_X) version should be used for execution", value: engineConfig.useDevClientProtocol, isReadOnly: false, update: toggleSetUseDevClientProtocol }), Boolean(editorStore.applicationStore.config.options
                    .TEMPORARY__serviceRegistrationConfig.length) && (_jsx(PanelFormTextField, { name: "Engine client service registration base URL", value: engineConfig.baseUrlForServiceRegistration ?? '', isReadOnly: false, update: (value) => engineConfig.setBaseUrlForServiceRegistration(value === '' ? undefined : value), errorMessage: Boolean(engineConfig.baseUrlForServiceRegistration) &&
                        !isValidUrl(engineConfig.baseUrlForServiceRegistration ?? '')
                        ? 'Invalid URL'
                        : '' })), _jsx(PanelFormBooleanField, { name: "Engine execution runner", prompt: "Use Base64 encoding for adhoc connection data URLs", value: engineConfig.useBase64ForAdhocConnectionDataUrls, isReadOnly: false, update: toggleEngineClientDataURLEncoding }), _jsx(PanelFormBooleanField, { name: "Graph builder strict mode", prompt: "Use strict-mode when building the graph (some warnings will be treated as errors)", value: editorStore.graphState.enableStrictMode, isReadOnly: false, update: toggleStrictMode }), _jsx(PanelFormBooleanField, { name: "Generate Artifact Generations", prompt: "Include generation of artifact extensions during generation action (F10)", value: editorStore.graphState.graphGenerationState.enableArtifactGeneration, isReadOnly: false, update: toggleArtifactGeneration }), _jsx(PanelFormListItems, { title: "Download Project Grammar", children: _jsxs("div", { className: "developer-tools__action-groups", children: [_jsxs("div", { className: "developer-tools__action-group", children: [_jsx("button", { className: "developer-tools__action-group__btn", onClick: () => {
                                            downloadProjectGrammar(false).catch(editorStore.applicationStore.alertUnhandledError);
                                        }, tabIndex: -1, title: "Download Project Grammar", children: _jsx(CloudDownloadIcon, {}) }), _jsx("div", { className: "developer-tools__action-group__prompt", children: "download grammar without dependency" })] }), _jsxs("div", { className: "developer-tools__action-group", children: [_jsx("button", { className: "developer-tools__action-group__btn", onClick: () => {
                                            downloadProjectGrammar(true).catch(editorStore.applicationStore.alertUnhandledError);
                                        }, tabIndex: -1, title: "Download Project Grammar with Dependency", children: _jsx(CloudDownloadIcon, {}) }), _jsx("div", { className: "developer-tools__action-group__prompt", children: "download grammar with dependency" })] })] }) })] }) }));
});
//# sourceMappingURL=DevToolPanel.js.map