

import { CredoError } from "../../../error/CredoError.mjs";
import "../../../error/index.mjs";
import { SECURITY_CONTEXT_URL } from "../constants.mjs";
import { W3cJsonLdVerifiableCredential } from "./models/W3cJsonLdVerifiableCredential.mjs";
import jsonld_default from "./libraries/jsonld.mjs";

//#region src/modules/vc/data-integrity/jsonldUtil.ts
const _includesContext = (options) => {
	const context = options.document["@context"];
	return context === options.contextUrl || Array.isArray(context) && context.includes(options.contextUrl);
};
function assertOnlyW3cJsonLdVerifiableCredentials(credentials) {
	if (credentials.some((c) => !(c instanceof W3cJsonLdVerifiableCredential))) throw new CredoError("JSON-LD VPs can only contain JSON-LD VCs");
}
/**
* The property identifying the linked data proof
* Note - this will not work for legacy systems that
* relying on `signature`
*/
const PROOF_PROPERTY = "proof";
/**
* Gets a supported linked data proof from a JSON-LD Document
* Note - unless instructed not to the document will be compacted
* against the security v2 context @see https://w3id.org/security/v2
*
* @param options Options for extracting the proof from the document
*
* @returns {GetProofsResult} An object containing the matched proofs and the JSON-LD document
*/
const getProofs = async (options) => {
	const { proofType, skipProofCompaction, documentLoader } = options;
	let { document } = options;
	let proofs;
	if (!skipProofCompaction) document = await jsonld_default.compact(document, SECURITY_CONTEXT_URL, {
		documentLoader,
		compactToRelative: false
	});
	proofs = jsonld_default.getValues(document, PROOF_PROPERTY);
	delete document[PROOF_PROPERTY];
	if (typeof proofType === "string") proofs = proofs.filter((_) => _.type === proofType);
	if (Array.isArray(proofType)) proofs = proofs.filter((_) => proofType.includes(_.type));
	proofs = proofs.map((matchedProof) => ({
		"@context": SECURITY_CONTEXT_URL,
		...matchedProof
	}));
	return {
		proofs,
		document
	};
};
/**
* Gets the JSON-LD type information for a document
* @param document {any} JSON-LD document to extract the type information from
* @param options {GetTypeInfoOptions} Options for extracting the JSON-LD document
*
* @returns {object} Type info for the JSON-LD document
*/
const getTypeInfo = async (document, options) => {
	const { documentLoader } = options;
	const context = jsonld_default.getValues(document, "@context");
	const compacted = await jsonld_default.compact({ "@type": "_:b0" }, context, { documentLoader });
	delete compacted["@context"];
	const alias = Object.keys(compacted)[0];
	const toExpand = { "@context": context };
	toExpand["@type"] = jsonld_default.getValues(document, "@type").concat(jsonld_default.getValues(document, alias));
	const expanded = (await jsonld_default.expand(toExpand, { documentLoader }))[0] || {};
	return {
		types: jsonld_default.getValues(expanded, "@type"),
		alias
	};
};

//#endregion
export { _includesContext, assertOnlyW3cJsonLdVerifiableCredentials, getProofs, getTypeInfo };
//# sourceMappingURL=jsonldUtil.mjs.map