"use strict";

const sendMessageEvent = require("../send-message-event");

const { validateArguments } = require("@validatem/core");
const required = require("@validatem/required");
const isNonEmptyString = require("@validatem/is-non-empty-string");
const isRoomID = require("../is-room-id");
const isSession = require("@modular-matrix/is-session");

module.exports = function sendMessage(_session, _options) {
	let [ session, options ] = validateArguments(arguments, {
		session: [ required, isSession ],
		options: [ required, {
			roomID: [ required, isRoomID ],
			text: [ required, isNonEmptyString ],
			html: [ isNonEmptyString ]
		}]
	});

	return sendMessageEvent(session, {
		roomID: options.roomID,
		type: "m.room.message",
		content: {
			msgtype: "m.text",
			body: options.text,
			format: (options.html != null)
				? "org.matrix.custom.html"
				: undefined,
			formatted_body: options.html
		}
	});
};
