/*jslint node: true */
/* global -Promise */
"use strict";

var _ = require('./lodash');

module.exports = function (Domain) {
    Domain.prototype.send = function (to, from, body, options) {
        var _this = this;
        if (!to || to.length < 1) {
            return;
        }
        return _this.emit(to, body, new _this.Message({
            domain: _this
            , to: to
            , from: from
            , body: body
            , options: options
        }));
    };

    Domain.prototype.Message.prototype.send = function () {
        var domain = this.domain;
        return domain.send.apply(domain, arguments);
    };
};

/*jslint node: true */
/* global -Promise */
"use strict";

var _ = require('./lodash');

module.exports = function (Domain) {
    Domain.prototype.send = function (to, from, body, options) {
        var _this = this;
        var msg;
        if (_.isArray(to) || _.isString(to)) {
            msg = new _this.Message({
                domain: _this
                , to: to
                , from: from
                , body: body
                , options: options
            });
        } else {
            msg = new _this.Message(_.extend({}, to, {
                domain: _this
            }));
        }
        if (!msg.to || msg.to.length < 1) {
            return void 0;
        }
        return _this.emit(msg.to, msg.body, msg);
    };

    Domain.prototype.Message.prototype.send = function () {
        var domain = this.domain;
        return domain.send.apply(domain, arguments);
    };
};

Allow sending objects as messages.