

const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
const require_MultiBaseEncoder = require('./MultiBaseEncoder.js');
const require_MultiHashEncoder = require('./MultiHashEncoder.js');
let borc = require("borc");
borc = require_rolldown_runtime.__toESM(borc);

//#region src/utils/HashlinkEncoder.ts
const hexTable = {
	urls: 15,
	contentType: 14
};
var HashlinkEncoder = class HashlinkEncoder {
	/**
	* Encodes a buffer, with optional metadata, into a hashlink
	*
	* @param buffer the buffer to encode into a hashlink
	* @param hashAlgorithm the name of the hashing algorithm 'sha-256'
	* @param baseEncoding the name of the base encoding algorithm 'base58btc'
	* @param metadata the optional metadata in the hashlink
	*
	* @returns hashlink hashlink with optional metadata
	*/
	static encode(buffer, hashAlgorithm, baseEncoding = "base58btc", metadata) {
		const checksum = HashlinkEncoder.encodeMultiHash(buffer, hashAlgorithm, baseEncoding);
		const mbMetadata = metadata && Object.keys(metadata).length > 0 ? HashlinkEncoder.encodeMetadata(metadata, baseEncoding) : null;
		return mbMetadata ? `hl:${checksum}:${mbMetadata}` : `hl:${checksum}`;
	}
	/**
	* Decodes a hashlink into HashlinkData object
	*
	* @param hashlink the hashlink that needs decoding
	*
	* @returns object the decoded hashlink
	*/
	static decode(hashlink) {
		if (HashlinkEncoder.isValid(hashlink)) {
			const [, checksum, encodedMetadata] = hashlink.split(":");
			return encodedMetadata ? {
				checksum,
				metadata: HashlinkEncoder.decodeMetadata(encodedMetadata)
			} : { checksum };
		}
		throw new Error(`Invalid hashlink: ${hashlink}`);
	}
	/**
	* Validates a hashlink
	*
	* @param hashlink the hashlink that needs validating
	*
	* @returns a boolean whether the hashlink is valid
	*
	* */
	static isValid(hashlink) {
		const hashlinkList = hashlink.split(":");
		if (!require_MultiBaseEncoder.MultiBaseEncoder.isValid(hashlinkList[1])) return false;
		const { data } = require_MultiBaseEncoder.MultiBaseEncoder.decode(hashlinkList[1]);
		return !!require_MultiHashEncoder.MultiHashEncoder.isValid(data);
	}
	static encodeMultiHash(data, hashName, baseEncoding = "base58btc") {
		const mh = require_MultiHashEncoder.MultiHashEncoder.encode(data, hashName);
		return require_MultiBaseEncoder.MultiBaseEncoder.encode(mh, baseEncoding);
	}
	static encodeMetadata(metadata, baseEncoding) {
		const metadataMap = /* @__PURE__ */ new Map();
		for (const key of Object.keys(metadata)) if (key === "urls" || key === "contentType") metadataMap.set(hexTable[key], metadata[key]);
		else throw new Error(`Invalid metadata: ${metadata}`);
		const cborData = borc.default.encode(metadataMap);
		return require_MultiBaseEncoder.MultiBaseEncoder.encode(cborData, baseEncoding);
	}
	static decodeMetadata(mb) {
		const obj = {
			urls: [],
			contentType: ""
		};
		const { data } = require_MultiBaseEncoder.MultiBaseEncoder.decode(mb);
		try {
			borc.default.decode(data).forEach((value, key) => {
				if (key === hexTable.urls) obj.urls = value;
				else if (key === hexTable.contentType) obj.contentType = value;
				else throw new Error(`Invalid metadata property: ${key}:${value}`);
			});
			return obj;
		} catch (error) {
			throw new Error(`Invalid metadata: ${mb}, ${error}`);
		}
	}
};

//#endregion
exports.HashlinkEncoder = HashlinkEncoder;