

const require_CredoError = require('../../../error/CredoError.js');
require('../../../error/index.js');
const require_constants = require('../constants.js');
const require_W3cJsonLdVerifiableCredential = require('./models/W3cJsonLdVerifiableCredential.js');
const require_jsonld = require('./libraries/jsonld.js');

//#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 require_W3cJsonLdVerifiableCredential.W3cJsonLdVerifiableCredential))) throw new require_CredoError.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 require_jsonld.default.compact(document, require_constants.SECURITY_CONTEXT_URL, {
		documentLoader,
		compactToRelative: false
	});
	proofs = require_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": require_constants.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 = require_jsonld.default.getValues(document, "@context");
	const compacted = await require_jsonld.default.compact({ "@type": "_:b0" }, context, { documentLoader });
	delete compacted["@context"];
	const alias = Object.keys(compacted)[0];
	const toExpand = { "@context": context };
	toExpand["@type"] = require_jsonld.default.getValues(document, "@type").concat(require_jsonld.default.getValues(document, alias));
	const expanded = (await require_jsonld.default.expand(toExpand, { documentLoader }))[0] || {};
	return {
		types: require_jsonld.default.getValues(expanded, "@type"),
		alias
	};
};

//#endregion
exports._includesContext = _includesContext;
exports.assertOnlyW3cJsonLdVerifiableCredentials = assertOnlyW3cJsonLdVerifiableCredentials;
exports.getProofs = getProofs;
exports.getTypeInfo = getTypeInfo;