

const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.js');
const require_CredoError = require('../../../../error/CredoError.js');
require('../../../../error/index.js');
require('../../../../utils/buffer.js');
const require_TypedArrayEncoder = require('../../../../utils/TypedArrayEncoder.js');
const require_JsonEncoder = require('../../../../utils/JsonEncoder.js');
const require_JsonTransformer = require('../../../../utils/JsonTransformer.js');
const require_MultiBaseEncoder = require('../../../../utils/MultiBaseEncoder.js');
const require_VarintEncoder = require('../../../../utils/VarintEncoder.js');
const require_MultiHashEncoder = require('../../../../utils/MultiHashEncoder.js');
require('../../../../utils/index.js');
const require_DidDocument = require('../../domain/DidDocument.js');
const require_parse = require('../../domain/parse.js');
require('../../domain/index.js');
let buffer_index_js = require("buffer/index.js");
buffer_index_js = require_rolldown_runtime.__toESM(buffer_index_js);

//#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) => require_MultiBaseEncoder.MultiBaseEncoder.encode(require_MultiHashEncoder.MultiHashEncoder.encode(require_TypedArrayEncoder.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 = require_parse.parseDid(did);
	const match = parsed.did.match(LONG_RE);
	if (!match) throw new require_CredoError.CredoError(`Invalid long form algo 4 did:peer: ${parsed.did}`);
	const [, hash, encodedDocument] = match;
	if (hash !== hashEncodedDocument(encodedDocument)) throw new require_CredoError.CredoError(`Hash is invalid for did: ${did}`);
	const { data } = require_MultiBaseEncoder.MultiBaseEncoder.decode(encodedDocument);
	const [multiCodecValue] = require_VarintEncoder.VarintEncoder.decode(data.subarray(0, 2));
	if (multiCodecValue !== JSON_MULTICODEC_VARINT) throw new require_CredoError.CredoError("Not a JSON multicodec data");
	const didDocumentJson = require_JsonEncoder.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 require_JsonTransformer.JsonTransformer.fromJSON(didDocumentJson, require_DidDocument.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_index_js.Buffer.concat([require_VarintEncoder.VarintEncoder.encode(JSON_MULTICODEC_VARINT), buffer_index_js.Buffer.from(JSON.stringify(didDocumentJson))]);
	const encodedDocument = require_MultiBaseEncoder.MultiBaseEncoder.encode(buffer, "base58btc");
	const shortFormDid = `did:peer:4${hashEncodedDocument(encodedDocument)}`;
	return {
		shortFormDid,
		longFormDid: `${shortFormDid}:${encodedDocument}`
	};
}

//#endregion
exports.didDocumentToNumAlgo4Did = didDocumentToNumAlgo4Did;
exports.didToNumAlgo4DidDocument = didToNumAlgo4DidDocument;
exports.getAlternativeDidsForNumAlgo4Did = getAlternativeDidsForNumAlgo4Did;
exports.isShortFormDidPeer4 = isShortFormDidPeer4;