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 { ServiceEditorState } from '../../../../stores/editor/editor-state/element-editor-state/service/ServiceEditorState.js';
import { clsx, PanelLoadingIndicator, CustomSelectorInput, CheckSquareIcon, SquareIcon, } from '@finos/legend-art';
import { prettyCONSTName } from '@finos/legend-shared';
import { LEGEND_STUDIO_TEST_ID } from '../../../../__lib__/LegendStudioTesting.js';
import { ServiceExecutionMode } from '@finos/legend-graph';
import { flowResult } from 'mobx';
import { useEditorStore } from '../../EditorStoreProvider.js';
import { useApplicationStore } from '@finos/legend-application';
import { MASTER_SNAPSHOT_ALIAS } from '@finos/legend-server-depot';
import { LATEST_PROJECT_REVISION } from '../../../../stores/editor/editor-state/element-editor-state/service/ServiceRegistrationState.js';
export const ServiceRegistrationEditor = observer(() => {
    const editorStore = useEditorStore();
    const applicationStore = useApplicationStore();
    const serviceState = editorStore.tabManagerState.getCurrentEditorState(ServiceEditorState);
    const registrationState = serviceState.registrationState;
    // env & execution server
    const envOptions = registrationState.options.map((info) => ({
        label: info.env.toUpperCase(),
        value: info.env,
    }));
    const selectedEnvOption = registrationState.serviceEnv
        ? {
            label: registrationState.serviceEnv.toUpperCase(),
            value: registrationState.serviceEnv,
        }
        : null;
    const onServerEnvChange = (val) => {
        registrationState.updateEnv(val?.value);
    };
    // execution mode
    const serviceTypesOptions = registrationState.executionModes.map((mode) => ({
        label: prettyCONSTName(mode),
        value: mode,
    }));
    const selectedServiceType = registrationState.serviceExecutionMode
        ? {
            label: prettyCONSTName(registrationState.serviceExecutionMode),
            value: registrationState.serviceExecutionMode,
        }
        : null;
    const onServiceTypeSelectionChange = (val) => {
        registrationState.updateType(val?.value);
    };
    // version
    const selectedVersion = registrationState.projectVersion
        ? {
            label: registrationState.projectVersion === MASTER_SNAPSHOT_ALIAS
                ? LATEST_PROJECT_REVISION
                : registrationState.projectVersion,
            value: registrationState.projectVersion,
        }
        : null;
    const onVersionSelectionChange = (val) => {
        registrationState.setProjectVersion(val?.value);
    };
    const versionPlaceholder = registrationState.versionOptions === undefined
        ? `Only valid for ${prettyCONSTName(ServiceExecutionMode.SEMI_INTERACTIVE)} and ${prettyCONSTName(ServiceExecutionMode.PROD)} service types`
        : !registrationState.versionOptions.length
            ? 'Project has no versions'
            : undefined;
    // activate
    const toggleActivatePostRegistration = () => {
        registrationState.setActivatePostRegistration(!registrationState.activatePostRegistration);
    };
    // store model for full interactive
    const toggleUseStoreModel = () => {
        registrationState.setUseStoreModelWithFullInteractive(!registrationState.TEMPORARY__useStoreModel);
    };
    const toggleUseGenerateLineage = () => {
        registrationState.setUseGenerateLineage(!registrationState.TEMPORARY__useGenerateLineage);
    };
    const toggleUseGenerateOpenApi = () => {
        registrationState.setUseGenerateOpenApi(!registrationState.TEMPORARY__useGenerateOpenApi);
    };
    // actions
    const registerService = () => {
        if (selectedEnvOption && selectedServiceType) {
            flowResult(registrationState.registerService()).catch(applicationStore.alertUnhandledError);
        }
    };
    const disableRegistration = !selectedEnvOption ||
        !selectedServiceType ||
        registrationState.registrationState.isInProgress;
    return (_jsxs("div", { "data-testid": LEGEND_STUDIO_TEST_ID.SERVICE_REGISTRATION_EDITOR, className: "service-registration-editor", children: [_jsxs("div", { className: "panel__header", children: [_jsx("div", { className: "panel__header__title", children: _jsx("div", { className: "panel__header__title__label", children: "Register Service" }) }), _jsx("div", { className: "panel__header__actions", children: _jsx("div", { className: "panel__header__action", children: _jsx("button", { className: "btn--dark model-loader__header__load-btn", onClick: registerService, disabled: disableRegistration, tabIndex: -1, title: "Register Service", children: "Register" }) }) })] }), _jsx(PanelLoadingIndicator, { isLoading: registrationState.registrationState.isInProgress }), _jsx("div", { className: "panel__content", children: _jsxs("div", { className: "panel__content__form", children: [registrationState.registrationState.message && (_jsx("div", { className: "service-registration-editor__progress-msg", children: `${registrationState.registrationState.message}...` })), _jsxs("div", { className: "panel__content__form__section", children: [_jsx("div", { className: "panel__content__form__section__header__label", children: "Activate Service" }), _jsxs("div", { className: "panel__content__form__section__toggler", onClick: toggleActivatePostRegistration, children: [_jsx("button", { className: clsx('panel__content__form__section__toggler__btn', {
                                                'panel__content__form__section__toggler__btn--toggled': registrationState.activatePostRegistration,
                                            }), tabIndex: -1, children: registrationState.activatePostRegistration ? (_jsx(CheckSquareIcon, {})) : (_jsx(SquareIcon, {})) }), _jsx("div", { className: "panel__content__form__section__toggler__prompt", children: "Activates service after registration" })] })] }), _jsxs("div", { className: "panel__content__form__section", children: [_jsx("div", { className: "panel__content__form__section__header__label", children: "Execution Server" }), _jsx("div", { className: "panel__content__form__section__header__prompt", children: "The execution server where your service will be registered" }), _jsx(CustomSelectorInput, { options: envOptions, onChange: onServerEnvChange, value: selectedEnvOption, darkMode: !applicationStore.layoutService
                                        .TEMPORARY__isLightColorThemeEnabled })] }), _jsxs("div", { className: "panel__content__form__section", children: [_jsx("div", { className: "panel__content__form__section__header__label", children: "Service Type" }), _jsx("div", { className: "panel__content__form__section__header__prompt", children: "The kind of service you want to register. Used to determine how the metadata will be fetched" }), _jsx(CustomSelectorInput, { options: serviceTypesOptions, onChange: onServiceTypeSelectionChange, value: selectedServiceType, darkMode: !applicationStore.layoutService
                                        .TEMPORARY__isLightColorThemeEnabled })] }), registrationState.serviceExecutionMode ===
                            ServiceExecutionMode.FULL_INTERACTIVE && (_jsxs("div", { className: "panel__content__form__section", children: [_jsx("div", { className: "panel__content__form__section__header__label", children: "Store Model" }), _jsxs("div", { className: "panel__content__form__section__toggler", onClick: toggleUseStoreModel, children: [_jsx("button", { className: clsx('panel__content__form__section__toggler__btn', {
                                                'panel__content__form__section__toggler__btn--toggled': registrationState.TEMPORARY__useStoreModel,
                                            }), tabIndex: -1, children: registrationState.TEMPORARY__useStoreModel ? (_jsx(CheckSquareIcon, {})) : (_jsx(SquareIcon, {})) }), _jsx("div", { className: "panel__content__form__section__toggler__prompt", children: "Use Store Model (slower)" })] })] })), _jsxs("div", { className: "panel__content__form__section", children: [_jsx("div", { className: "panel__content__form__section__header__label", children: "Generate Lineage" }), _jsxs("div", { className: "panel__content__form__section__toggler", onClick: toggleUseGenerateLineage, children: [_jsx("button", { className: clsx('panel__content__form__section__toggler__btn', {
                                                'panel__content__form__section__toggler__btn--toggled': registrationState.TEMPORARY__useGenerateLineage,
                                            }), tabIndex: -1, children: registrationState.TEMPORARY__useGenerateLineage ? (_jsx(CheckSquareIcon, {})) : (_jsx(SquareIcon, {})) }), _jsx("div", { className: "panel__content__form__section__toggler__prompt", children: "Generate Lineage (slower)" })] })] }), registrationState.serviceExecutionMode ===
                            ServiceExecutionMode.PROD && (_jsxs("div", { className: "panel__content__form__section", children: [_jsx("div", { className: "panel__content__form__section__header__label", children: "Generate Open Api" }), _jsxs("div", { className: "panel__content__form__section__toggler", onClick: toggleUseGenerateOpenApi, children: [_jsx("button", { className: clsx('panel__content__form__section__toggler__btn', {
                                                'panel__content__form__section__toggler__btn--toggled': registrationState.TEMPORARY__useGenerateOpenApi,
                                            }), tabIndex: -1, children: registrationState.TEMPORARY__useGenerateOpenApi ? (_jsx(CheckSquareIcon, {})) : (_jsx(SquareIcon, {})) }), _jsx("div", { className: "panel__content__form__section__toggler__prompt", children: "Generate Open Api" })] })] })), _jsxs("div", { className: "panel__content__form__section", children: [_jsx("div", { className: "panel__content__form__section__header__label", children: "Project Version" }), _jsx("div", { className: "panel__content__form__section__header__prompt", children: "The version of your project you want to use for registration. Only relevant for semi-interactive and production services." }), _jsx(CustomSelectorInput, { options: registrationState.versionOptions ?? [], onChange: onVersionSelectionChange, value: selectedVersion, darkMode: !applicationStore.layoutService
                                        .TEMPORARY__isLightColorThemeEnabled, disabled: registrationState.versionOptions === undefined, placeholder: versionPlaceholder, isLoading: editorStore.sdlcState.fetchPublishedProjectVersionsState
                                        .isInProgress })] })] }) })] }));
});
//# sourceMappingURL=ServiceRegistrationEditor.js.map