"use strict";

/**
 * Options that can be applied to a message before sending it.
 * @typedef {(object)} MessageOptions
 * @property {boolean} [tts=false] Whether or not the message should be sent as text-to-speech.
*/

exports.__esModule = true;

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var _UtilCache = require("../Util/Cache");

var _UtilCache2 = _interopRequireDefault(_UtilCache);

var _User = require("./User");

var _User2 = _interopRequireDefault(_User);

var _UtilArgumentRegulariser = require("../Util/ArgumentRegulariser");

var _UtilEquality = require("../Util/Equality");

var _UtilEquality2 = _interopRequireDefault(_UtilEquality);

var Message = (function (_Equality) {
	_inherits(Message, _Equality);

	function Message(data, channel, client) {
		var _this = this;

		_classCallCheck(this, Message);

		_Equality.call(this);
		this.channel = channel;
		this.server = channel.server;
		this.client = client;
		this.nonce = data.nonce;
		this.attachments = data.attachments;
		this.tts = data.tts;
		this.embeds = data.embeds;
		this.timestamp = Date.parse(data.timestamp);
		this.everyoneMentioned = data.mention_everyone !== undefined ? data.mention_everyone : data.everyoneMentioned;
		this.pinned = data.pinned;
		this.id = data.id;

		if (data.edited_timestamp) {
			this.editedTimestamp = Date.parse(data.edited_timestamp);
		}

		if (data.author instanceof _User2["default"]) {
			this.author = data.author;
		} else if (data.author) {
			this.author = client.internal.users.add(new _User2["default"](data.author, client));
		}

		this.content = data.content;

		var mentionData = client.internal.resolver.resolveMentions(data.content, channel);
		this.cleanContent = mentionData[1];
		this.mentions = [];

		mentionData[0].forEach(function (mention) {
			// this is .add and not .get because it allows the bot to cache
			// users from messages from logs who may have left the server and were
			// not previously cached.
			if (mention instanceof _User2["default"]) {
				_this.mentions.push(mention);
			} else {
				_this.mentions.push(client.internal.users.add(new _User2["default"](mention, client)));
			}
		});
	}

	Message.prototype.isMentioned = function isMentioned(user) {
		user = this.client.internal.resolver.resolveUser(user);
		if (!user) {
			return false;
		}
		for (var _iterator = this.mentions, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
			var _ref;

			if (_isArray) {
				if (_i >= _iterator.length) break;
				_ref = _iterator[_i++];
			} else {
				_i = _iterator.next();
				if (_i.done) break;
				_ref = _i.value;
			}

			var mention = _ref;

			if (mention.id == user.id) {
				return true;
			}
		}
		return false;
	};

	Message.prototype.toString = function toString() {
		return this.content;
	};

	Message.prototype["delete"] = function _delete() {
		return this.client.deleteMessage.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
	};

	Message.prototype.update = function update() {
		return this.client.updateMessage.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
	};

	Message.prototype.edit = function edit() {
		return this.client.updateMessage.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
	};

	Message.prototype.reply = function reply() {
		return this.client.reply.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
	};

	Message.prototype.replyTTS = function replyTTS() {
		return this.client.replyTTS.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
	};

	Message.prototype.pin = function pin() {
		return this.client.pinMessage.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
	};

	Message.prototype.unpin = function unpin() {
		return this.client.unpinMessage.apply(this.client, req(this, arguments));
	};

	_createClass(Message, [{
		key: "sender",
		get: function get() {
			return this.author;
		}
	}]);

	return Message;
})(_UtilEquality2["default"]);

exports["default"] = Message;
module.exports = exports["default"];
