import { LayoutVariant } from '@redocly/config';
import { isUndefined, addTrailingSlash } from '@redocly/theme/core/openapi';
import { WindowReferenceOptions } from '../../constants.js';
import { argValueToBoolean, argValueToExpandLevel, argValueToInt, argValueToNumber, getWindowReferenceOptions, normalizePath, } from '../../utils/index.js';
import { normalizeShowExtensions, normalizeScrollYOffset } from './helpers.js';
import { Languages } from '../code-samples/constants.js';
export function normalizeOptions(raw, defaultOptions) {
    raw = {
        ...defaultOptions,
        ...raw,
        markdocOptions: {
            ...raw?.markdocOptions,
            components: {
                ...defaultOptions?.markdocOptions?.components,
                ...raw?.markdocOptions?.components,
            },
            tags: {
                ...defaultOptions?.markdocOptions?.tags,
                ...raw?.markdocOptions?.tags,
            },
            nodes: {
                ...defaultOptions?.markdocOptions?.nodes,
                ...raw?.markdocOptions?.nodes,
                heading: (defaultOptions?.markdocOptions?.nodes?.heading ||
                    raw?.markdocOptions?.nodes.heading),
            },
        },
    };
    const ignoreNamedSchemas = Array.isArray(raw.ignoreNamedSchemas)
        ? raw.ignoreNamedSchemas
        : raw.ignoreNamedSchemas?.split(',').map((s) => s.trim());
    const normalizedOptions = {
        downloadUrls: raw.downloadUrls,
        schemaDefinitionsTagName: raw.schemaDefinitionsTagName,
        // TODO: validate URL and error here
        corsProxyUrl: raw.corsProxyUrl && addTrailingSlash(raw.corsProxyUrl),
        hideSidebar: argValueToBoolean(raw.hideSidebar, false),
        jsonSamplesExpandLevel: argValueToExpandLevel(raw.jsonSamplesExpandLevel, 2),
        generatedSamplesMaxDepth: argValueToInt(raw.generatedSamplesMaxDepth, 8),
        hideDownloadButtons: argValueToBoolean(raw.hideDownloadButtons, false),
        hideInfoMetadata: argValueToBoolean(raw.hideInfoMetadata, false),
        hideLoading: argValueToBoolean(raw.hideLoading, false),
        hideSchemaTitles: argValueToBoolean(raw.hideSchemaTitles, false),
        maxDisplayedEnumValues: argValueToNumber(raw.maxDisplayedEnumValues),
        mockServer: raw.mockServer?.url
            ? {
                url: raw.mockServer.url,
                position: raw.mockServer.position || 'first',
                description: raw.mockServer.description || 'Mock server',
            }
            : undefined,
        hideReplay: argValueToBoolean(raw.hideReplay, false),
        oAuth2RedirectURI: raw.oAuth2RedirectURI || null,
        onlyRequiredInSamples: argValueToBoolean(raw.onlyRequiredInSamples, false),
        onDeepLinkClick: raw.onDeepLinkClick || getWindowReferenceOptions(WindowReferenceOptions.ON_DEEP_LINK_CLICK),
        requestInterceptor: raw.requestInterceptor ||
            getWindowReferenceOptions(WindowReferenceOptions.REQUEST_INTERCEPTOR),
        routingBasePath: isUndefined(raw.disableRouter)
            ? ''
            : (raw.routingBasePath && normalizePath(raw.routingBasePath)) || '',
        schemasExpansionLevel: argValueToExpandLevel(raw.schemasExpansionLevel),
        feedback: raw.feedback || { hide: false },
        sortRequiredPropsFirst: argValueToBoolean(raw.sortRequiredPropsFirst, false),
        scrollYOffset: normalizeScrollYOffset(raw.scrollYOffset),
        showExtensions: normalizeShowExtensions(raw.showExtensions),
        sanitize: argValueToBoolean(raw.sanitize, false),
        skipBundle: argValueToBoolean(raw.skipBundle, false),
        ignoreNamedSchemas: new Set(ignoreNamedSchemas),
        markdocOptions: raw?.markdocOptions,
        codeSamples: raw.codeSamples || {
            languages: Object.values(Languages).map((lang) => ({ lang })),
        },
        layout: raw.layout || LayoutVariant.THREE_PANEL,
        events: raw.events || {},
        unstable_hooks: raw.unstable_hooks || getWindowReferenceOptions(WindowReferenceOptions.HOOKS) || {},
        unstable_skipSamples: argValueToBoolean(raw.unstable_skipSamples, false),
        unstable_externalCodeSamples: raw.unstable_externalCodeSamples || [],
        // New options
        hidePropertiesPrefix: argValueToBoolean(raw?.hidePropertiesPrefix, false),
    };
    return normalizedOptions;
}
//# sourceMappingURL=normalizeOptions.js.map