

const require_CredoError = require('../../../../error/CredoError.js');
const require_did = require('../../../../utils/did.js');
require('../../../../utils/index.js');
const require_DidResolverService = require('../../../dids/services/DidResolverService.js');
require('../../../dids/index.js');
const require_jsonld = require('./jsonld.js');
const require_defaultContexts = require('./contexts/defaultContexts.js');
const require_nativeDocumentLoader = require('./nativeDocumentLoader.js');

//#region src/modules/vc/data-integrity/libraries/documentLoader.ts
function defaultDocumentLoader(agentContext) {
	const didResolver = agentContext.dependencyManager.resolve(require_DidResolverService.DidResolverService);
	async function loader(url) {
		if (url in require_defaultContexts.DEFAULT_CONTEXTS) return {
			contextUrl: null,
			documentUrl: url,
			document: require_defaultContexts.DEFAULT_CONTEXTS[url]
		};
		if (url.split("#")[0] in require_defaultContexts.DEFAULT_CONTEXTS) return {
			contextUrl: null,
			documentUrl: url,
			document: require_defaultContexts.DEFAULT_CONTEXTS[url]
		};
		if (require_did.isDid(url)) {
			const result = await didResolver.resolve(agentContext, url);
			if (result.didResolutionMetadata.error || !result.didDocument) throw new require_CredoError.CredoError(`Unable to resolve DID: ${url}`);
			return {
				contextUrl: null,
				documentUrl: url,
				document: await require_jsonld.default.frame(result.didDocument.toJSON(), {
					"@context": result.didDocument.context,
					"@embed": "@never",
					id: url
				}, { documentLoader: this })
			};
		}
		return await (await require_nativeDocumentLoader.getNativeDocumentLoader()).apply(require_jsonld.default, [])(url);
	}
	return loader.bind(loader);
}

//#endregion
exports.defaultDocumentLoader = defaultDocumentLoader;