const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.js');
const require_messageType = require('./messageType.js');
const require_DidCommMessage = require('../DidCommMessage.js');
const require_index = require('../../../../node_modules/.pnpm/query-string@7.1.3/node_modules/query-string/index.js');
const require_DidCommConnectionInvitationMessage = require('../modules/connections/messages/DidCommConnectionInvitationMessage.js');
require('../modules/connections/messages/index.js');
const require_OutOfBandDidCommService = require('../modules/oob/domain/OutOfBandDidCommService.js');
const require_DidCommOutOfBandInvitation = require('../modules/oob/messages/DidCommOutOfBandInvitation.js');
require('../modules/oob/messages/index.js');
const require_converters = require('../modules/oob/converters.js');
let __credo_ts_core = require("@credo-ts/core");
__credo_ts_core = require_rolldown_runtime.__toESM(__credo_ts_core);

//#region ../didcomm/src/util/parseInvitation.ts
var import_query_string = /* @__PURE__ */ require_rolldown_runtime.__toESM(require_index.require_query_string());
const fetchShortUrl = async (invitationUrl, dependencies) => {
	const abortController = new AbortController();
	const id = setTimeout(() => abortController.abort(), 15e3);
	let response;
	try {
		response = await dependencies.fetch(invitationUrl, {
			method: "GET",
			headers: {
				Accept: "application/json",
				"Content-Type": "application/json"
			}
		});
	} catch (error) {
		throw new __credo_ts_core.CredoError(`Get request failed on provided url: ${error.message}`, { cause: error });
	}
	clearTimeout(id);
	return response;
};
/**
* Parses a JSON containing an invitation message and returns an DidCommOutOfBandInvitation instance
*
* @param invitationJson JSON object containing message
* @returns DidCommOutOfBandInvitation
*/
const parseInvitationJson = (invitationJson) => {
	const messageType = invitationJson["@type"];
	if (!messageType) throw new __credo_ts_core.CredoError("Invitation is not a valid DIDComm message");
	const parsedMessageType = require_messageType.parseMessageType(messageType);
	if (require_messageType.supportsIncomingMessageType(parsedMessageType, require_DidCommOutOfBandInvitation.DidCommOutOfBandInvitation.type)) {
		const invitation = __credo_ts_core.JsonTransformer.fromJSON(invitationJson, require_DidCommOutOfBandInvitation.DidCommOutOfBandInvitation);
		__credo_ts_core.MessageValidator.validateSync(invitation);
		invitation.invitationType = require_DidCommOutOfBandInvitation.DidCommInvitationType.OutOfBand;
		return invitation;
	}
	if (require_messageType.supportsIncomingMessageType(parsedMessageType, require_DidCommConnectionInvitationMessage.DidCommConnectionInvitationMessage.type)) {
		const invitation = __credo_ts_core.JsonTransformer.fromJSON(invitationJson, require_DidCommConnectionInvitationMessage.DidCommConnectionInvitationMessage);
		__credo_ts_core.MessageValidator.validateSync(invitation);
		const outOfBandInvitation = require_converters.convertToNewInvitation(invitation);
		outOfBandInvitation.invitationType = require_DidCommOutOfBandInvitation.DidCommInvitationType.Connection;
		return outOfBandInvitation;
	}
	if (invitationJson["~service"]) return transformLegacyConnectionlessInvitationToOutOfBandInvitation(invitationJson);
	throw new __credo_ts_core.CredoError(`Invitation with '@type' ${parsedMessageType.messageTypeUri} not supported.`);
};
/**
* Parses URL containing encoded invitation and returns invitation message.
*
* @param invitationUrl URL containing encoded invitation
*
* @returns DidCommOutOfBandInvitation
*/
const parseInvitationUrl = (invitationUrl) => {
	const parsedUrl = (0, import_query_string.parseUrl)(invitationUrl).query;
	const encodedInvitation = parsedUrl.oob ?? parsedUrl.c_i ?? parsedUrl.d_m;
	if (typeof encodedInvitation === "string") return parseInvitationJson(__credo_ts_core.JsonEncoder.fromBase64(encodedInvitation));
	throw new __credo_ts_core.CredoError("InvitationUrl is invalid. It needs to contain one, and only one, of the following parameters: `oob`, `c_i` or `d_m`.");
};
const oobInvitationFromShortUrl = async (response) => {
	if (response) {
		if (response.headers.get("Content-Type")?.startsWith("application/json") && response.ok) return parseInvitationJson(await response.json());
		if (response.url) {
			let responseUrl;
			const location = response.headers.get("Location");
			if ((response.status === 302 || response.status === 301) && location) responseUrl = location;
			else responseUrl = response.url;
			return parseInvitationUrl(responseUrl);
		}
	}
	throw new __credo_ts_core.CredoError("HTTP request time out or did not receive valid response");
};
function transformLegacyConnectionlessInvitationToOutOfBandInvitation(messageJson) {
	const agentMessage = __credo_ts_core.JsonTransformer.fromJSON(messageJson, require_DidCommMessage.DidCommMessage);
	if (!agentMessage.service) throw new __credo_ts_core.CredoError("Invalid legacy connectionless invitation url. Missing ~service decorator.");
	const { "~service": service,...messageWithoutService } = messageJson;
	const invitation = new require_DidCommOutOfBandInvitation.DidCommOutOfBandInvitation({ services: [require_OutOfBandDidCommService.OutOfBandDidCommService.fromResolvedDidCommService(agentMessage.service.resolvedDidCommService)] });
	invitation.invitationType = require_DidCommOutOfBandInvitation.DidCommInvitationType.Connectionless;
	invitation.addRequest(__credo_ts_core.JsonTransformer.fromJSON(messageWithoutService, require_DidCommMessage.DidCommMessage));
	return invitation;
}
/**
* Parses URL containing encoded invitation and returns invitation message. Compatible with
* parsing short Urls
*
* @param invitationUrl URL containing encoded invitation
*
* @param dependencies Agent dependencies containing fetch
*
* @returns DidCommOutOfBandInvitation
*/
const parseInvitationShortUrl = async (invitationUrl, dependencies) => {
	const parsedUrl = (0, import_query_string.parseUrl)(invitationUrl).query;
	if (parsedUrl.oob || parsedUrl.c_i) return parseInvitationUrl(invitationUrl);
	if (parsedUrl.d_m) return transformLegacyConnectionlessInvitationToOutOfBandInvitation(__credo_ts_core.JsonEncoder.fromBase64(parsedUrl.d_m));
	try {
		const outOfBandInvitation = await oobInvitationFromShortUrl(await fetchShortUrl(invitationUrl, dependencies));
		outOfBandInvitation.invitationType = require_DidCommOutOfBandInvitation.DidCommInvitationType.OutOfBand;
		return outOfBandInvitation;
	} catch (_error) {
		throw new __credo_ts_core.CredoError("InvitationUrl is invalid. It needs to contain one, and only one, of the following parameters: `oob`, `c_i` or `d_m`, or be valid shortened URL");
	}
};

//#endregion
exports.oobInvitationFromShortUrl = oobInvitationFromShortUrl;
exports.parseInvitationShortUrl = parseInvitationShortUrl;
exports.parseInvitationUrl = parseInvitationUrl;