import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { memo, useRef } from 'react';
import { LayoutVariant } from '@redocly/config';
import { useSetAtom } from 'jotai';
import { ThreePanelLayout } from '@redocly/theme/layouts/ThreePanelLayout';
import { SidebarLogo } from '@redocly/theme/components/SidebarLogo/SidebarLogo';
import { GlobalStyle as ThemeCommonStyle, useMount } from '@redocly/theme/core/openapi';
import { StoreProvider } from './Providers.js';
import { Overlay } from '../Overlay.js';
import { RedocWrap } from './styled.js';
import { StickyResponsiveSidebar } from '../StickySidebar/index.js';
import { SideMenu } from '../SideMenu/index.js';
import { Content } from '../Content/index.js';
import { useContentItems, useUppercase2LowercaseRedirect, useTelemetry, usePerformanceMetrics, } from '../../hooks/index.js';
import { withRouter } from '../../hoc/withRouter.js';
import { withStore } from '../../hoc/withStore.js';
import { compose } from '../../utils/index.js';
import { useLicense } from '../../hooks/useLicense.js'; // we need a full import path for overriding license in portal
import { allOperationsAtom } from '../../jotai/replay.js';
const RedoclyOpenAPIDocsComponent = compose(withRouter, withStore, memo)(({ licenseKey, onLoaded, children, options, layout, collapsedSidebar, parser, withCommonStyles, }) => {
    const rootElementRef = useRef(null);
    const { isValid: hasValidLicense, licenseInfo } = useLicense(licenseKey);
    const setAllOperations = useSetAtom(allOperationsAtom);
    const telemetry = useTelemetry();
    // remove this when no more warnings are logged (https://github.com/Redocly/redocly/issues/6778)
    useUppercase2LowercaseRedirect();
    const { contentItems, flatItems } = useContentItems({ parser, options });
    usePerformanceMetrics({
        sendTelemetry: (data) => {
            const lowercaseData = Object.fromEntries(Object.entries(data).map(([key, value]) => [key.toLowerCase(), value]));
            telemetry.send({
                type: 'openapi_docs.performance_metrics',
                payload: {
                    cls: lowercaseData.cls,
                    lcp: lowercaseData.lcp,
                    fcp: lowercaseData.fcp,
                    ttfb: lowercaseData.ttfb,
                },
            });
        },
    });
    useMount(() => {
        onLoaded?.();
        telemetry.send({ type: 'openapi_docs.viewed', payload: { layout: layout } });
        if (!options.hideReplay) {
            const operations = flatItems.filter((item) => item.type === 'operation');
            setAllOperations(operations);
        }
    });
    const { hideSidebar, scrollYOffset } = options || {};
    const xLogo = parser.definition?.info?.['x-logo'];
    return (_jsxs(_Fragment, { children: [withCommonStyles && _jsx(ThemeCommonStyle, {}), _jsxs(RedocWrap, { className: "redoc-wrap", ref: rootElementRef, children: [!hasValidLicense && _jsx(Overlay, { licenseInfo: licenseInfo }), !hideSidebar && (_jsxs(StickyResponsiveSidebar, { scrollYOffset: scrollYOffset, className: "menu-content", collapsedSidebar: !collapsedSidebar, children: [_jsx(SidebarLogo, { imageUrl: xLogo?.url, href: xLogo?.href || parser.definition?.info?.contact?.url, altText: xLogo?.altText, backgroundColor: xLogo?.backgroundColor }), _jsx(SideMenu, { items: hasValidLicense ? contentItems : [] })] })), _jsxs(ThreePanelLayout, { className: "api-content", id: "api-content", layout: layout === LayoutVariant.THREE_PANEL ? LayoutVariant.THREE_PANEL : LayoutVariant.STACKED, collapsedSidebar: collapsedSidebar, children: [_jsx(Content, { items: hasValidLicense ? contentItems : [], routingBasePath: options.routingBasePath }), children] })] })] }));
});
export function RedoclyOpenAPIDocs({ onLoaded, children, store, withCommonStyles, }) {
    return store?.definition ? (_jsx(StoreProvider, { ...store, children: _jsx(RedoclyOpenAPIDocsComponent, { onLoaded: onLoaded, basePath: store.options?.routingBasePath, licenseKey: store.options?.licenseKey, disableRouter: store.options?.disableRouter, withCommonStyles: withCommonStyles, children: children }) })) : null;
}
//# sourceMappingURL=RedoclyOpenAPIDocs.js.map