"use strict";

const withoutKeys = require("../without-keys");

module.exports = function mapEncryptedWrapper(wrapperObject, permitPassthrough = false) {
	return {
		// TODO: Add a `decrypt` method?
		encryptedPayloads: Object.entries(wrapperObject).map(([ keyID, data ]) => {
			if (data.passthrough === true) {
				if (permitPassthrough) {
					return {
						keyID: keyID,
						isKeyPassthrough: true,
						encryptionConfiguration: {}
					};
				} else {
					throw new Error(`Encountered a 'passthrough' encryption object where it is not allowed`);
				}
			} else {
				return {
					keyID: keyID,
					isKeyPassthrough: false,
					ciphertext: data.ciphertext,
					encryptionConfiguration: withoutKeys(data, [ "ciphertext" ])
				};
			}
		})
	};
};
