

import { CredoError } from "../../../../error/CredoError.mjs";
import "../../../../error/index.mjs";
import { Buffer } from "../../../../utils/buffer.mjs";
import { TypedArrayEncoder } from "../../../../utils/TypedArrayEncoder.mjs";
import { JsonEncoder } from "../../../../utils/JsonEncoder.mjs";
import { JsonTransformer } from "../../../../utils/JsonTransformer.mjs";
import { MultiBaseEncoder } from "../../../../utils/MultiBaseEncoder.mjs";
import { VarintEncoder } from "../../../../utils/VarintEncoder.mjs";
import { MultiHashEncoder } from "../../../../utils/MultiHashEncoder.mjs";
import "../../../../utils/index.mjs";
import { DidDocument } from "../../domain/DidDocument.mjs";
import { parseDid } from "../../domain/parse.mjs";
import "../../domain/index.mjs";

//#region src/modules/dids/methods/peer/peerDidNumAlgo4.ts
const LONG_RE = /^did:peer:4(z[1-9a-km-zA-HJ-NP-Z]{46}):(z[1-9a-km-zA-HJ-NP-Z]{6,})$/;
const SHORT_RE = /^did:peer:4(z[1-9a-km-zA-HJ-NP-Z]{46})$/;
const JSON_MULTICODEC_VARINT = 512;
const isShortFormDidPeer4 = (did) => SHORT_RE.test(did);
const hashEncodedDocument = (encodedDocument) => MultiBaseEncoder.encode(MultiHashEncoder.encode(TypedArrayEncoder.fromString(encodedDocument), "sha-256"), "base58btc");
function getAlternativeDidsForNumAlgo4Did(did) {
	const match = did.match(LONG_RE);
	if (!match) return;
	const [, hash] = match;
	return [`did:peer:4${hash}`];
}
function didToNumAlgo4DidDocument(did) {
	const parsed = parseDid(did);
	const match = parsed.did.match(LONG_RE);
	if (!match) throw new CredoError(`Invalid long form algo 4 did:peer: ${parsed.did}`);
	const [, hash, encodedDocument] = match;
	if (hash !== hashEncodedDocument(encodedDocument)) throw new CredoError(`Hash is invalid for did: ${did}`);
	const { data } = MultiBaseEncoder.decode(encodedDocument);
	const [multiCodecValue] = VarintEncoder.decode(data.subarray(0, 2));
	if (multiCodecValue !== JSON_MULTICODEC_VARINT) throw new CredoError("Not a JSON multicodec data");
	const didDocumentJson = JsonEncoder.fromBuffer(data.subarray(2));
	didDocumentJson.id = parsed.did;
	didDocumentJson.alsoKnownAs = [parsed.did.slice(0, did.lastIndexOf(":"))];
	const addControllerIfNotPresent = (item) => {
		if (Array.isArray(item)) item.forEach(addControllerIfNotPresent);
		if (item && typeof item === "object" && item.controller === void 0) item.controller = parsed.did;
	};
	addControllerIfNotPresent(didDocumentJson.verificationMethod);
	addControllerIfNotPresent(didDocumentJson.authentication);
	addControllerIfNotPresent(didDocumentJson.assertionMethod);
	addControllerIfNotPresent(didDocumentJson.keyAgreement);
	addControllerIfNotPresent(didDocumentJson.capabilityDelegation);
	addControllerIfNotPresent(didDocumentJson.capabilityInvocation);
	return JsonTransformer.fromJSON(didDocumentJson, DidDocument);
}
function didDocumentToNumAlgo4Did(didDocument) {
	const didDocumentJson = didDocument.toJSON();
	const deleteControllerIfPresent = (item) => {
		if (Array.isArray(item)) {
			for (const method of item) if (method.controller === "#id" || method.controller === didDocument.id) method.controller = void 0;
		}
	};
	didDocumentJson.id = void 0;
	didDocumentJson.alsoKnownAs = void 0;
	deleteControllerIfPresent(didDocumentJson.verificationMethod);
	deleteControllerIfPresent(didDocumentJson.authentication);
	deleteControllerIfPresent(didDocumentJson.assertionMethod);
	deleteControllerIfPresent(didDocumentJson.keyAgreement);
	deleteControllerIfPresent(didDocumentJson.capabilityDelegation);
	deleteControllerIfPresent(didDocumentJson.capabilityInvocation);
	const buffer = Buffer.concat([VarintEncoder.encode(JSON_MULTICODEC_VARINT), Buffer.from(JSON.stringify(didDocumentJson))]);
	const encodedDocument = MultiBaseEncoder.encode(buffer, "base58btc");
	const shortFormDid = `did:peer:4${hashEncodedDocument(encodedDocument)}`;
	return {
		shortFormDid,
		longFormDid: `${shortFormDid}:${encodedDocument}`
	};
}

//#endregion
export { didDocumentToNumAlgo4Did, didToNumAlgo4DidDocument, getAlternativeDidsForNumAlgo4Did, isShortFormDidPeer4 };
//# sourceMappingURL=peerDidNumAlgo4.mjs.map