"use strict";

const dotty = require("dotty");

const addCommonFields = require("../event-add-common-fields");

function noop() {
	return {};
}

module.exports = function mapMaybeRedacted(event, handler) {
	let { redacted, notRedacted } = (typeof handler === "function")
		? { redacted: noop, notRedacted: handler }
		: handler;

	let redactionData = dotty.get(event, ["unsigned", "redacted_because"]);

	if (redactionData != null) {
		return {
			... redacted(),
			isRedacted: true,
			redaction: addCommonFields(redactionData, {
				user: redactionData.sender,
				isVoluntary: (event.sender != null)
					? (redactionData.sender === event.sender)
					: undefined,
				reason: redactionData.content.reason
			})
		};
	} else {
		return {
			... notRedacted(),
			isRedacted: false,
			redaction: {},
		};
	}
};
