(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./channels/WebSocket", "./channels/Http", "@dojo/shim/Promise"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WebSocket_1 = require("./channels/WebSocket");
    var Http_1 = require("./channels/Http");
    var Promise_1 = require("@dojo/shim/Promise");
    var Channel = (function () {
        function Channel(options) {
            this.options = options;
        }
        Channel.prototype.sendMessage = function (name, data) {
            var _this = this;
            return this._initialize().then(function () {
                return _this._channel.sendMessage(name, data);
            });
        };
        Channel.prototype._initialize = function () {
            var _this = this;
            if (!this._initialized) {
                this._initialized = new Promise_1.default(function (resolve) {
                    if (_this.options.port) {
                        try {
                            _this._channel = new WebSocket_1.default(_this.options);
                            _this._channel.sendMessage('remoteStatus', 'ping').then(function () {
                                resolve();
                            }, function (_error) {
                                _this._channel = new Http_1.default(_this.options);
                                resolve();
                            });
                        }
                        catch (error) {
                            _this._channel = new Http_1.default(_this.options);
                            resolve();
                        }
                    }
                    else {
                        _this._channel = new Http_1.default(_this.options);
                        resolve();
                    }
                });
            }
            return this._initialized;
        };
        return Channel;
    }());
    exports.default = Channel;
});
//# sourceMappingURL=Channel.js.map