import { DidCommConnectionInvitationMessage } from "../connections/messages/DidCommConnectionInvitationMessage.mjs";
import { OutOfBandDidCommService } from "./domain/OutOfBandDidCommService.mjs";
import { DidCommInvitationType, DidCommOutOfBandInvitation } from "./messages/DidCommOutOfBandInvitation.mjs";
import { DidCommV1Service, DidDocumentBuilder, DidKey, didKeyToVerkey, verkeyToDidKey } from "@credo-ts/core";

//#region ../didcomm/src/modules/oob/converters.ts
function convertToNewInvitation(oldInvitation) {
	let service;
	if (oldInvitation.did) service = oldInvitation.did;
	else if (oldInvitation.serviceEndpoint && oldInvitation.recipientKeys && oldInvitation.recipientKeys.length > 0) service = new OutOfBandDidCommService({
		id: "#inline",
		recipientKeys: oldInvitation.recipientKeys?.map(verkeyToDidKey),
		routingKeys: oldInvitation.routingKeys?.map(verkeyToDidKey),
		serviceEndpoint: oldInvitation.serviceEndpoint
	});
	else throw new Error("Missing required serviceEndpoint, routingKeys and/or did fields in connection invitation");
	const outOfBandInvitation = new DidCommOutOfBandInvitation({
		id: oldInvitation.id,
		label: oldInvitation.label,
		imageUrl: oldInvitation.imageUrl,
		appendedAttachments: oldInvitation.appendedAttachments,
		accept: ["didcomm/aip1", "didcomm/aip2;env=rfc19"],
		services: [service],
		handshakeProtocols: ["https://didcomm.org/connections/1.0"]
	});
	outOfBandInvitation.invitationType = DidCommInvitationType.Connection;
	return outOfBandInvitation;
}
function convertToOldInvitation(newInvitation) {
	const [service] = newInvitation.getServices();
	let options;
	if (typeof service === "string") options = {
		id: newInvitation.id,
		label: newInvitation.label ?? "",
		did: service,
		imageUrl: newInvitation.imageUrl,
		appendedAttachments: newInvitation.appendedAttachments
	};
	else options = {
		id: newInvitation.id,
		label: newInvitation.label ?? "",
		recipientKeys: service.recipientKeys.map(didKeyToVerkey),
		routingKeys: service.routingKeys?.map(didKeyToVerkey),
		serviceEndpoint: service.serviceEndpoint,
		imageUrl: newInvitation.imageUrl,
		appendedAttachments: newInvitation.appendedAttachments
	};
	return new DidCommConnectionInvitationMessage(options);
}

//#endregion
export { convertToNewInvitation, convertToOldInvitation };
//# sourceMappingURL=converters.mjs.map