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 { useEffect } from 'react';
import { WorkspaceSetup } from './workspace-setup/WorkspaceSetup.js';
import { Editor } from './editor/Editor.js';
import { ProjectReviewer } from './project-reviewer/ProjectReviewer.js';
import { ProjectViewer } from './project-view/ProjectViewer.js';
import { observer } from 'mobx-react-lite';
import { clsx, CubesLoadingIndicator, CubesLoadingIndicatorIcon, GhostIcon, MarkdownTextViewer, } from '@finos/legend-art';
import { LEGEND_STUDIO_ROUTE_PATTERN, LEGEND_STUDIO_SDLC_BYPASSED_ROUTE_PATTERN, } from '../__lib__/LegendStudioNavigation.js';
import { flowResult } from 'mobx';
import { LegendStudioFrameworkProvider, useLegendStudioApplicationStore, useLegendStudioBaseStore, } from './LegendStudioFrameworkProvider.js';
import { useApplicationStore } from '@finos/legend-application';
import { BrowserEnvironmentProvider, generateExtensionUrlPattern, Route, Routes, } from '@finos/legend-application/browser';
import { LEGEND_STUDIO_DOCUMENTATION_KEY } from '../__lib__/LegendStudioDocumentation.js';
import { LazyTextEditor } from './lazy-text-editor/LazyTextEditor.js';
import { PureCompatibilityTestManager } from './pct/PureCompatibilityTest.js';
import { ShowcaseViewer } from './showcase/ShowcaseViewer.js';
import { AuthProvider, withAuthenticationRequired, } from 'react-oidc-context';
const NotFoundPage = observer(() => {
    const applicationStore = useApplicationStore();
    const currentPath = applicationStore.navigationService.navigator.getCurrentLocation();
    const documentation = applicationStore.documentationService.getDocEntry(LEGEND_STUDIO_DOCUMENTATION_KEY.NOT_FOUND_HELP);
    return (_jsx("div", { className: "app__page", children: _jsxs("div", { className: clsx('not-found-screen', {
                'not-found-screen--no-documentation': !documentation?.markdownText,
            }), children: [_jsxs("div", { className: "not-found-screen__icon", children: [_jsx("div", { className: "not-found-screen__icon__ghost", children: _jsx(GhostIcon, {}) }), _jsx("div", { className: "not-found-screen__icon__shadow", children: _jsx("svg", { viewBox: "0 0 600 400", children: _jsx("g", { transform: "translate(300 200)", children: _jsx("ellipse", { className: "not-found-screen__icon__shadow__inner", rx: "320", ry: "80" }) }) }) })] }), _jsxs("div", { className: "not-found-screen__text-content", children: [_jsx("div", { className: "not-found-screen__text-content__title", children: "404. Not Found" }), _jsxs("div", { className: "not-found-screen__text-content__detail", children: ["The requested URL", _jsx("span", { className: "not-found-screen__text-content__detail__url", children: applicationStore.navigationService.navigator.generateAddress(currentPath) }), "was not found in the application"] })] }), documentation?.markdownText && (_jsx("div", { className: "not-found-screen__documentation", children: _jsx(MarkdownTextViewer, { value: documentation.markdownText, className: "markdown-content--page" }) }))] }) }));
});
export const LegendStudioWebApplicationRouter = observer(() => {
    const baseStore = useLegendStudioBaseStore();
    const applicationStore = useLegendStudioApplicationStore();
    const extraApplicationPageEntries = applicationStore.pluginManager
        .getApplicationPlugins()
        .flatMap((plugin) => plugin.getExtraApplicationPageEntries?.() ?? []);
    useEffect(() => {
        flowResult(baseStore.initialize()).catch(applicationStore.alertUnhandledError);
    }, [applicationStore, baseStore]);
    return (_jsx("div", { className: "app", children: baseStore.initState.hasCompleted && (_jsxs(_Fragment, { children: [baseStore.isSDLCAuthorized === undefined && (_jsx(_Fragment, { children: _jsxs(Routes, { children: [_jsx(Route, { path: LEGEND_STUDIO_SDLC_BYPASSED_ROUTE_PATTERN.VIEW_BY_GAV, element: _jsx(ProjectViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_SDLC_BYPASSED_ROUTE_PATTERN.VIEW_BY_GAV_ENTITY, element: _jsx(ProjectViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_SDLC_BYPASSED_ROUTE_PATTERN.SHOWCASE, element: _jsx(ShowcaseViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_SDLC_BYPASSED_ROUTE_PATTERN.PCT_REPORT, element: _jsx(PureCompatibilityTestManager, {}) }), _jsx(Route, { path: "*", element: _jsx(NotFoundPage, {}) })] }) })), baseStore.isSDLCAuthorized && (_jsx(_Fragment, { children: _jsxs(Routes, { children: [_jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.VIEW, element: _jsx(ProjectViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.VIEW_BY_ENTITY, element: _jsx(ProjectViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.VIEW_BY_REVISION, element: _jsx(ProjectViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.VIEW_BY_VERSION, element: _jsx(ProjectViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.VIEW_BY_REVISION_ENTITY, element: _jsx(ProjectViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.VIEW_BY_VERSION_ENTITY, element: _jsx(ProjectViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.REVIEW, element: _jsx(ProjectReviewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.EDIT_GROUP_WORKSPACE, element: _jsx(Editor, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.EDIT_PATCH_GROUP_WORKSPACE, element: _jsx(Editor, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.EDIT_GROUP_WORKSPACE_ENTITY, element: _jsx(Editor, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.EDIT_PATCH_GROUP_WORKSPACE_ENTITY, element: _jsx(Editor, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.EDIT_WORKSPACE, element: _jsx(Editor, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.EDIT_PATCH_WORKSPACE, element: _jsx(Editor, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.EDIT_WORKSPACE_ENTITY, element: _jsx(Editor, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.EDIT_PATCH_WORKSPACE_ENTITY, element: _jsx(Editor, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.TEXT_GROUP_WORKSPACE, element: _jsx(LazyTextEditor, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.TEXT_WORKSPACE, element: _jsx(LazyTextEditor, {}) }), _jsx(Route
                            // root path will lead to setup page (home page)
                            , { 
                                // root path will lead to setup page (home page)
                                path: "", element: _jsx(WorkspaceSetup, {}) }), _jsx(Route
                            // root path will lead to setup page (home page)
                            , { 
                                // root path will lead to setup page (home page)
                                path: "/", element: _jsx(WorkspaceSetup, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.SETUP_WORKSPACE, element: _jsx(WorkspaceSetup, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.SETUP_PATCH_WORKSPACE, element: _jsx(WorkspaceSetup, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.SETUP_GROUP_WORKSPACE, element: _jsx(WorkspaceSetup, {}) }), _jsx(Route, { path: LEGEND_STUDIO_ROUTE_PATTERN.SETUP_PATCH_GROUP_WORKSPACE, element: _jsx(WorkspaceSetup, {}) }), _jsx(Route, { path: LEGEND_STUDIO_SDLC_BYPASSED_ROUTE_PATTERN.SHOWCASE, element: _jsx(ShowcaseViewer, {}) }), _jsx(Route, { path: LEGEND_STUDIO_SDLC_BYPASSED_ROUTE_PATTERN.PCT_REPORT, element: _jsx(PureCompatibilityTestManager, {}) }), extraApplicationPageEntries.flatMap((entry) => entry.addressPatterns
                                .map(generateExtensionUrlPattern)
                                .map((path) => (_jsx(Route, { path: path, element: entry.renderer() }, entry.key)))), _jsx(Route, { path: "*", element: _jsx(NotFoundPage, {}) })] }) }))] })) }));
});
const LegendStudioWebProvider = ({ baseUrl }) => {
    return (_jsx(BrowserEnvironmentProvider, { baseUrl: baseUrl, children: _jsx(LegendStudioFrameworkProvider, { children: _jsx(LegendStudioWebApplicationRouter, {}) }) }));
};
const AuthenticatedLegendStudioWebProvider = withAuthenticationRequired(LegendStudioWebProvider, {
    OnRedirecting: () => (_jsx(CubesLoadingIndicator, { isLoading: true, children: _jsx(CubesLoadingIndicatorIcon, {}) })),
    signinRedirectArgs: {
        state: `${window.location.pathname}${window.location.search}`,
    },
});
export const LegendStudioWebApplication = observer((props) => {
    const { baseUrl } = props;
    const applicationStore = useLegendStudioApplicationStore();
    const oidcConfig = applicationStore.config.options.ingestDeploymentConfig?.deployment
        .oidcConfig;
    const enableOauthFlow = applicationStore.config.options.enableOauthFlow;
    if (oidcConfig) {
        const onSigninCallback = (_user) => {
            window.location.href = _user?.state ?? '/';
        };
        const mergedOIDCConfig = {
            ...oidcConfig.authProviderProps,
            redirect_uri: `${window.location.origin}${oidcConfig.redirectPath}`,
            silent_redirect_uri: `${window.location.origin}${oidcConfig.silentRedirectPath}`,
            onSigninCallback,
        };
        if (enableOauthFlow) {
            return (_jsx(AuthProvider, { ...mergedOIDCConfig, children: _jsx(AuthenticatedLegendStudioWebProvider, { baseUrl: baseUrl }) }));
        }
        return (_jsx(AuthProvider, { ...mergedOIDCConfig, children: _jsx(LegendStudioWebProvider, { baseUrl: baseUrl }) }));
    }
    return _jsx(LegendStudioWebProvider, { baseUrl: baseUrl });
});
//# sourceMappingURL=LegendStudioWebApplication.js.map