import { jsx as _jsx } from "react/jsx-runtime";
import { cloneElement, isValidElement, useEffect, useMemo, useState } from 'react';
import { Loading } from '@redocly/theme/components/Loaders/Loading';
import { useDeepCompareMemoize, fixSpec } from './utils.js';
import { argValueToBoolean } from '../../utils/index.js';
import { RedoclyOpenAPIDocs } from './RedoclyOpenAPIDocs.js';
import { loadAndBundleDefinition } from '../../utils/loadAndBundleSpec.js';
export async function loadSingle(spec, specUrl, options) {
    let resolvedDefinition;
    if (options?.skipBundle) {
        if (!spec) {
            throw new Error('spec must be specified when using "skipBundleAndConvert"');
        }
        resolvedDefinition = spec;
    }
    else {
        resolvedDefinition = await loadAndBundleDefinition(spec || specUrl);
    }
    try {
        fixSpec(resolvedDefinition);
    }
    catch {
        // nope
    }
    return resolvedDefinition;
}
export function loadAndBundleSpecOrMd(url, title) {
    if (url.endsWith('.md')) {
        return loadAndBundleDefinition({
            openapi: '3.0.0',
            info: {
                title: title || '',
                version: '1.0',
                description: { $ref: url },
            },
            paths: {},
        });
    }
    else {
        return loadAndBundleDefinition(url);
    }
}
export const AppProvider = ({ options, definitionUrl, definition, activeSampleLanguage, children, }) => {
    const [, setError] = useState();
    const [loading, setLoading] = useState(true);
    const [loadedSpec, setLoadedSpec] = useState(null);
    useEffect(() => {
        async function load() {
            setLoading(true);
            try {
                setLoadedSpec(await loadSingle(definition, definitionUrl, options));
            }
            catch (e) {
                setError(() => {
                    throw e;
                });
            }
        }
        load();
    }, [definition, definitionUrl, options]); // do not reload based on options
    const store = useMemo(() => {
        if (!loadedSpec)
            return undefined;
        if (options?.unstable_hooks?.onInit) {
            options.unstable_hooks?.onInit?.({
                store: { definition: loadedSpec, options, definitionUrl, activeSampleLanguage },
            });
        }
        return { definition: loadedSpec, options, definitionUrl, activeSampleLanguage };
    }, 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    useDeepCompareMemoize([loadedSpec, options, definitionUrl]));
    useEffect(() => {
        if (!store)
            return;
        setLoading(false);
    }, [store]);
    if (!store) {
        return null;
    }
    return !loading ? (isValidElement(children) ? (cloneElement(children, { store })) : null) : argValueToBoolean(options?.hideLoading, false) ? null : (_jsx(Loading, { color: "--loading-spinner-color" }));
};
export function RedoclyOpenAPIDocsStandalone({ definition, definitionUrl, options = {}, activeItemId, activeSampleLanguage, activeDeepLink, }) {
    return (_jsx(AppProvider, { definition: definition, definitionUrl: definitionUrl, options: options, activeItemId: activeItemId, activeSampleLanguage: activeSampleLanguage, activeDeepLink: activeDeepLink, children: _jsx(RedoclyOpenAPIDocs, { withCommonStyles: true }) }));
}
//# sourceMappingURL=RedoclyOpenAPIDocsStandalone.js.map