"use strict";

const unreachable = require("@joepie91/unreachable");

const normalizeEncryptionAlgorithmName = require("../normalize-encryption-algorithm-name");

module.exports = function mapKeyRequestEvent(event, _context) {
	if (event.content.action === "request") {
		return {
			type: "keyRequested",
			user: event.sender,
			deviceID: event.content.requesting_device_id,
			requestID: event.content.request_id,
			algorithm: normalizeEncryptionAlgorithmName(event.content.body.algorithm),
			senderKey: event.content.body.sender_key, // TODO: Better name for senderKey? also elsewhere
			roomID: event.content.body.room_id,
			sessionID: event.content.body.session_id
		};
	} else if (event.content.action === "request_cancellation") {
		return {
			type: "keyRequestCancelled",
			user: event.sender,
			deviceID: event.content.requesting_device_id,
			requestID: event.content.request_id,
		};
	} else {
		unreachable(`Unrecognized action '${event.content.action}' for room_key_request`);
	}
};
