

const require_JsonTransformer = require('../../../utils/JsonTransformer.js');
require('../../../utils/index.js');
const require_constants = require('../constants.js');
const require_W3cJsonLdVerifiableCredential = require('./models/W3cJsonLdVerifiableCredential.js');
const require_jsonld = require('./libraries/jsonld.js');
const require_jsonldUtil = require('./jsonldUtil.js');

//#region src/modules/vc/data-integrity/deriveProof.ts
/**
* Derives a proof from a document featuring a supported linked data proof
*
* NOTE - This is a temporary API extending JSON-LD signatures
*
* @param proofDocument A document featuring a linked data proof capable of proof derivation
* @param revealDocument A document of the form of a JSON-LD frame describing the terms to selectively derive from the proof document
* @param options Options for proof derivation
*/
const deriveProof = async (proofDocument, revealDocument, { suite, skipProofCompaction, documentLoader, nonce }) => {
	if (!suite) throw new TypeError("\"options.suite\" is required.");
	if (Array.isArray(proofDocument)) throw new TypeError("proofDocument should be an object not an array.");
	const { proofs, document } = await require_jsonldUtil.getProofs({
		document: proofDocument,
		proofType: suite.supportedDeriveProofType,
		documentLoader
	});
	if (proofs.length === 0) throw new Error("There were not any proofs provided that can be used to derive a proof with this suite.");
	let derivedProof = await suite.deriveProof({
		document,
		proof: proofs[0],
		revealDocument,
		documentLoader,
		nonce
	});
	if (proofs.length > 1) {
		derivedProof = {
			...derivedProof,
			proof: [derivedProof.proof]
		};
		proofs.splice(0, 1);
		for (const proof of proofs) {
			const additionalDerivedProofValue = await suite.deriveProof({
				document,
				proof,
				revealDocument,
				documentLoader
			});
			derivedProof.proof.push(additionalDerivedProofValue.proof);
		}
	}
	if (!skipProofCompaction) {
		const expandedProof = { [require_constants.SECURITY_PROOF_URL]: { "@graph": derivedProof.proof } };
		const { types, alias } = await require_jsonldUtil.getTypeInfo(derivedProof.document, { documentLoader });
		expandedProof["@type"] = types;
		const ctx = require_jsonld.default.getValues(derivedProof.document, "@context");
		const compactProof = await require_jsonld.default.compact(expandedProof, ctx, {
			documentLoader,
			compactToRelative: false
		});
		delete compactProof[alias];
		delete compactProof["@context"];
		/**
		* removes the @included tag when multiple proofs exist because the
		* @included tag messes up the canonicalized bytes leading to a bad
		* signature that won't verify.
		**/
		if (compactProof.proof?.["@included"]) compactProof.proof = compactProof.proof["@included"];
		const key = Object.keys(compactProof)[0];
		require_jsonld.default.addValue(derivedProof.document, key, compactProof[key]);
	} else {
		delete derivedProof.proof["@context"];
		require_jsonld.default.addValue(derivedProof.document, "proof", derivedProof.proof);
	}
	return require_JsonTransformer.JsonTransformer.fromJSON(derivedProof.document, require_W3cJsonLdVerifiableCredential.W3cJsonLdVerifiableCredential);
};

//#endregion
exports.deriveProof = deriveProof;