import { bundle, Config } from '@redocly/openapi-core';
import { IS_BROWSER } from '@redocly/theme/core/openapi';
import { convertSwagger2OpenAPI } from './convertSwagger2OpenAPI.js';
export async function loadAndBundleDefinition(specUrlOrObject) {
    const config = new Config({});
    const bundleOpts = {
        config,
        base: IS_BROWSER
            ? window.location.origin
            : typeof globalThis.process !== 'undefined'
                ? globalThis.process.cwd()
                : '',
    };
    if (IS_BROWSER) {
        config.resolve.http.customFetch = globalThis.fetch;
    }
    if (typeof specUrlOrObject === 'object' && specUrlOrObject !== null) {
        bundleOpts['doc'] = createParsedDocument(specUrlOrObject);
    }
    else {
        bundleOpts['ref'] = specUrlOrObject;
    }
    const { bundle: { parsed }, } = await bundle(bundleOpts);
    return parsed.swagger !== undefined ? convertSwagger2OpenAPI(parsed) : parsed;
}
function createParsedDocument(specUrlOrObject) {
    return {
        source: { absoluteRef: '' },
        parsed: specUrlOrObject,
    };
}
//# sourceMappingURL=loadAndBundleSpec.js.map