{"version":3,"file":"documentLoader.mjs","names":["jsonld"],"sources":["../../../../../src/modules/vc/data-integrity/libraries/documentLoader.ts"],"sourcesContent":["import type { AgentContext } from '../../../../agent/context/AgentContext'\nimport { CredoError } from '../../../../error/CredoError'\nimport { isDid } from '../../../../utils'\nimport { DidResolverService } from '../../../dids'\nimport { DEFAULT_CONTEXTS } from './contexts'\nimport type { DocumentLoader } from './jsonld'\nimport jsonld from './jsonld'\nimport { getNativeDocumentLoader } from './nativeDocumentLoader'\n\nexport type DocumentLoaderWithContext = (agentContext: AgentContext) => DocumentLoader\n\nexport function defaultDocumentLoader(agentContext: AgentContext): DocumentLoader {\n  const didResolver = agentContext.dependencyManager.resolve(DidResolverService)\n\n  async function loader(url: string) {\n    // Check if in the default contexts shipped with Credo\n    if (url in DEFAULT_CONTEXTS) {\n      return {\n        contextUrl: null,\n        documentUrl: url,\n        document: DEFAULT_CONTEXTS[url as keyof typeof DEFAULT_CONTEXTS],\n      }\n    }\n\n    const withoutFragment = url.split('#')[0]\n    if (withoutFragment in DEFAULT_CONTEXTS) {\n      return {\n        contextUrl: null,\n        documentUrl: url,\n        document: DEFAULT_CONTEXTS[url as keyof typeof DEFAULT_CONTEXTS],\n      }\n    }\n\n    if (isDid(url)) {\n      const result = await didResolver.resolve(agentContext, url)\n\n      if (result.didResolutionMetadata.error || !result.didDocument) {\n        throw new CredoError(`Unable to resolve DID: ${url}`)\n      }\n\n      const framed = await jsonld.frame(\n        result.didDocument.toJSON(),\n        {\n          '@context': result.didDocument.context,\n          '@embed': '@never',\n          id: url,\n        },\n        // @ts-expect-error\n        { documentLoader: this }\n      )\n\n      return {\n        contextUrl: null,\n        documentUrl: url,\n        document: framed,\n      }\n    }\n\n    // fetches the documentLoader from documentLoader.ts or documentLoader.native.ts depending on the platform at bundle time\n    const platformLoader = await getNativeDocumentLoader()\n    const nativeLoader = platformLoader.apply(jsonld, [])\n\n    return await nativeLoader(url)\n  }\n\n  return loader.bind(loader)\n}\n"],"mappings":";;;;;;;;;;;;AAWA,SAAgB,sBAAsB,cAA4C;CAChF,MAAM,cAAc,aAAa,kBAAkB,QAAQ,mBAAmB;CAE9E,eAAe,OAAO,KAAa;AAEjC,MAAI,OAAO,iBACT,QAAO;GACL,YAAY;GACZ,aAAa;GACb,UAAU,iBAAiB;GAC5B;AAIH,MADwB,IAAI,MAAM,IAAI,CAAC,MAChB,iBACrB,QAAO;GACL,YAAY;GACZ,aAAa;GACb,UAAU,iBAAiB;GAC5B;AAGH,MAAI,MAAM,IAAI,EAAE;GACd,MAAM,SAAS,MAAM,YAAY,QAAQ,cAAc,IAAI;AAE3D,OAAI,OAAO,sBAAsB,SAAS,CAAC,OAAO,YAChD,OAAM,IAAI,WAAW,0BAA0B,MAAM;AAcvD,UAAO;IACL,YAAY;IACZ,aAAa;IACb,UAda,MAAMA,eAAO,MAC1B,OAAO,YAAY,QAAQ,EAC3B;KACE,YAAY,OAAO,YAAY;KAC/B,UAAU;KACV,IAAI;KACL,EAED,EAAE,gBAAgB,MAAM,CACzB;IAMA;;AAOH,SAAO,OAHgB,MAAM,yBAAyB,EAClB,MAAMA,gBAAQ,EAAE,CAAC,CAE3B,IAAI;;AAGhC,QAAO,OAAO,KAAK,OAAO"}