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 { forwardRef, useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import { LEGEND_STUDIO_TEST_ID } from '../../../__lib__/LegendStudioTesting.js';
import { useEditorStore } from '../EditorStoreProvider.js';
import { ContextMenu, PanelContent, PURE_ServiceIcon, clsx, PlayIcon, Dialog, Modal, TimesIcon, CheckCircleIcon, CircleNotchIcon, TimesCircleIcon, EmptyCircleIcon, MenuContentItem, MenuContent, ModalBody, ModalHeader, ModalFooter, ModalFooterButton, CheckSquareIcon, SquareIcon, } from '@finos/legend-art';
import { REGISTRATION_RESULT, getServiceRegistrationResult, } from '../../../stores/editor/sidebar-state/BulkServiceRegistrationState.js';
import { BulkServiceRegistrationEditor } from '../editor-group/service-editor/BulkServiceRegistrationEditor.js';
import { guaranteeNonNullable, noop } from '@finos/legend-shared';
import { ServiceRegistrationFail, ServiceRegistrationSuccess, } from '@finos/legend-graph';
import { generateServiceManagementUrl } from '../../../stores/editor/editor-state/element-editor-state/service/ServiceRegistrationState.js';
import { useApplicationStore } from '@finos/legend-application';
import { CODE_EDITOR_LANGUAGE } from '@finos/legend-code-editor';
import { CodeEditor } from '@finos/legend-lego/code-editor';
export const getRegistrationStatusIcon = (isSelected, registrationResult) => {
    if (registrationResult === REGISTRATION_RESULT.SUCCESS) {
        return (_jsx("div", { title: "Service registered successfully", className: "bulk-service-registration__item__link__content__status__indicator bulk-service-registration__item__link__content__status__indicator--succeeded", children: _jsx(CheckCircleIcon, {}) }));
    }
    else if (registrationResult === REGISTRATION_RESULT.IN_PROGRESS &&
        isSelected) {
        return (_jsx("div", { title: "Service registration in progress", className: "bulk-service-registration__item__link__content__status__indicator bulk-service-registration__item__link__content__status__indicator--in-progress", children: _jsx(CircleNotchIcon, {}) }));
    }
    else if (registrationResult === REGISTRATION_RESULT.FAILED) {
        return (_jsx("div", { title: "Service Registration Failed", className: "bulk-service-registration__item__link__content__status__indicator bulk-service-registration__item__link__content__status__indicator--failed", children: _jsx(TimesCircleIcon, {}) }));
    }
    else {
        return (_jsx("div", { title: "Service is not registered", className: "bulk-service-registration__item__link__content__status__indicator bulk-service-registration__item__link__content__status__indicator--unknown", children: _jsx(EmptyCircleIcon, {}) }));
    }
};
const ServiceContextMenu = observer(forwardRef(function ServiceContextMenu(props, ref) {
    const { serviceState, globalServiceRegistrationState } = props;
    const applicationStore = useApplicationStore();
    return (_jsxs(MenuContent, { children: [serviceState.registrationResult instanceof
                ServiceRegistrationSuccess && (_jsx(MenuContentItem, { onClick: () => {
                    const config = guaranteeNonNullable(globalServiceRegistrationState.serviceConfigState.options.find((info) => info.env ===
                        globalServiceRegistrationState.serviceConfigState
                            .serviceEnv));
                    applicationStore.navigationService.navigator.visitAddress(generateServiceManagementUrl(config.managementUrl, serviceState.service.pattern));
                }, children: "Launch Service" })), serviceState.registrationResult instanceof ServiceRegistrationFail && (_jsx(MenuContentItem, { onClick: () => globalServiceRegistrationState.setFailingView(serviceState.registrationResult), children: "View Error" }))] }));
}));
const ServiceFailViewer = observer((props) => {
    const { globalBulkServiceRegistrationState, failure } = props;
    const applicationStore = globalBulkServiceRegistrationState.editorStore.applicationStore;
    const closeLogViewer = () => globalBulkServiceRegistrationState.setFailingView(undefined);
    return (_jsx(Dialog, { open: Boolean(failure), onClose: closeLogViewer, classes: {
            root: 'editor-modal__root-container',
            container: 'editor-modal__container',
            paper: 'editor-modal__content',
        }, children: _jsxs(Modal, { darkMode: !applicationStore.layoutService.TEMPORARY__isLightColorThemeEnabled, className: "editor-modal", children: [_jsx(ModalHeader, { title: guaranteeNonNullable(failure?.service).path }), _jsx(ModalBody, { children: failure instanceof ServiceRegistrationFail && (_jsx(CodeEditor, { inputValue: failure.errorMessage, isReadOnly: true, language: CODE_EDITOR_LANGUAGE.TEXT })) }), _jsx(ModalFooter, { children: _jsx(ModalFooterButton, { text: "Close", onClick: closeLogViewer, type: "secondary" }) })] }) }));
});
export const RegisterService = observer((props) => {
    const editorStore = useEditorStore();
    const applicationStore = editorStore.applicationStore;
    const services = editorStore.graphManagerState.graph.ownServices;
    const toggleSelectAllServices = () => {
        editorStore.globalBulkServiceRegistrationState.toggleSelectAllServices(!editorStore.globalBulkServiceRegistrationState.selectAllServices);
        editorStore.globalBulkServiceRegistrationState.setSelectAll(editorStore.globalBulkServiceRegistrationState.selectAllServices);
    };
    const selectService = (serviceState) => {
        serviceState.toggleIsSelected();
        editorStore.globalBulkServiceRegistrationState.toggleSelectAllServices(editorStore.globalBulkServiceRegistrationState.bulkServiceRegistrationStates.filter((bulkServiceState) => bulkServiceState.isSelected).length ===
            editorStore.globalBulkServiceRegistrationState
                .bulkServiceRegistrationStates.length);
    };
    useEffect(() => {
        editorStore.globalBulkServiceRegistrationState.init();
    }, [editorStore.globalBulkServiceRegistrationState]);
    const serviceItems = () => (_jsx(_Fragment, { children: editorStore.globalBulkServiceRegistrationState.bulkServiceRegistrationStates.map((serviceState) => (_jsx(ContextMenu, { content: _jsx(ServiceContextMenu, { globalServiceRegistrationState: editorStore.globalBulkServiceRegistrationState, serviceState: serviceState }), children: _jsxs("div", { className: clsx('side-bar__panel__item'), children: [_jsxs("div", { className: clsx('bulk-service-registration__service__container bulk-service-registration__explorer__service__container'), children: [_jsx("div", { className: "bulk-service-registration__services-tree__node__icon__type", children: getRegistrationStatusIcon(serviceState.isSelected, getServiceRegistrationResult(editorStore.globalBulkServiceRegistrationState
                                    .isServiceRegistering.isInProgress, serviceState.registrationResult)) }), _jsx("div", { className: "bulk-service-registration__explorer__service__result__icon__type", children: _jsx(PURE_ServiceIcon, {}) }), _jsx("div", { className: "bulk-service-registration__item__link__content", children: _jsx("div", { className: "bulk-service-registration__item__link__content__id", children: serviceState.service.name }) })] }), _jsx("button", { type: "button", className: clsx('bulk-service-registration__section__toggler__btn', {
                            'bulk-service-registration__section__toggler__btn--toggled': serviceState.isSelected,
                        }), onClick: () => {
                            selectService(serviceState);
                        }, tabIndex: -1, children: serviceState.isSelected ? (_jsx(CheckSquareIcon, {})) : (_jsx(SquareIcon, {})) })] }) }, serviceState.service._UUID))) }));
    return (_jsxs("div", { "data-testid": LEGEND_STUDIO_TEST_ID.BULK_REGISTRATION, className: "panel bulk-service-registration", children: [_jsxs("div", { className: "panel__header side-bar__header", children: [_jsx("div", { className: "panel__header__title bulk-service-registration__header__title", children: _jsx("div", { className: "panel__header__title__content side-bar__header__title__content", children: "REGISTER SERVICES" }) }), _jsx("button", { className: "panel__header__action bulk-service-registration__service__header__action bulk-service-registration__play-btn", onClick: () => editorStore.globalBulkServiceRegistrationState.setShowRegConfig(true), tabIndex: -1, title: "Register All Services", children: _jsx(PlayIcon, {}) }), _jsx(Dialog, { onClose: noop, open: editorStore.globalBulkServiceRegistrationState
                            .showRegistrationConfig, children: _jsxs(Modal, { darkMode: !applicationStore.layoutService
                                .TEMPORARY__isLightColorThemeEnabled, className: clsx('editor-modal bulk-service-registration__service__editor'), children: [_jsxs("div", { className: "bulk-service-registration__header", children: [_jsx("div", { className: "bulk-service-registration__header__actions" }), _jsx("button", { className: "bulk-service-registration__header__action", tabIndex: -1, onClick: () => editorStore.globalBulkServiceRegistrationState.setShowRegConfig(false), children: _jsx(TimesIcon, {}) })] }), _jsx("div", { className: "bulk-service-registration__panel__content__form", children: _jsx(BulkServiceRegistrationEditor, {}) })] }) })] }), _jsxs("div", { className: "panel__header side-bar__header", children: [_jsx("div", { className: "panel__header__title bulk-service-registration__header__title", children: _jsx("div", { className: "panel__header__title__content side-bar__header__title__content", children: "Select All" }) }), _jsx("button", { type: "button", className: clsx('panel__header__action bulk-service-registration__section__toggler__btn', {
                            'panel__header__action bulk-service-registration__section__toggler__btn--toggled': editorStore.globalBulkServiceRegistrationState
                                .selectAllServices,
                        }), onClick: toggleSelectAllServices, tabIndex: -1, children: editorStore.globalBulkServiceRegistrationState
                            .selectAllServices ? (_jsx(CheckSquareIcon, {})) : (_jsx(SquareIcon, {})) })] }), _jsx("div", { className: "panel__content side-bar__content", children: _jsxs("div", { className: "panel side-bar__panel", children: [_jsxs("div", { className: "panel__header", children: [_jsx("div", { className: "panel__header__title", children: _jsx("div", { className: "panel__header__title__content", children: "SERVICES" }) }), _jsxs("div", { className: "bulk-service-registration__header__changes-count", "data-testid": LEGEND_STUDIO_TEST_ID.SIDEBAR_PANEL_HEADER__CHANGES_COUNT, children: [editorStore.globalBulkServiceRegistrationState.bulkServiceRegistrationStates.filter((serviceState) => serviceState.isSelected).length, ' ', "/ ", services.length] })] }), _jsx(PanelContent, { children: serviceItems() }), editorStore.globalBulkServiceRegistrationState.failingView && (_jsx(ServiceFailViewer, { globalBulkServiceRegistrationState: editorStore.globalBulkServiceRegistrationState, failure: editorStore.globalBulkServiceRegistrationState.failingView }))] }) })] }));
});
//# sourceMappingURL=RegisterService.js.map