"use strict";

const syncpipe = require("syncpipe");
const flatten = require("flatten");
const matchValue = require("match-value");

module.exports = function mapReceiptEvent(event, _context) {
	return syncpipe(event, [
		(_) => Object.entries(_.content),
		(_) => _.map(([ eventID, receiptSets ]) => syncpipe(receiptSets, [
			(_) => Object.entries(_),
			(_) => _.map(([ receiptType, receipts ]) => syncpipe(receipts, [
				(_) => Object.entries(_),
				(_) => _.map(([ user, receiptData ]) => {
					return {
						type: "receiptReceived",
						receiptType: matchValue(receiptType, {
							"m.read": "readReceipt"
						}),
						user: user,
						timestamp: receiptData.ts,
						eventID: eventID
					};
				})
			]))
		])),
		(_) => flatten(_)
	]);
};
