Object.defineProperty(exports, "__esModule", { value: true });
exports.Mitti = void 0;
var HyperdeckLib = require("hyperdeck-js-lib");
var Mitti = /** @class */ (function () {
    function Mitti(ip) {
        this.events = {};
        this.connected = false;
        // console.log(`Connecting to hyperdeck at ${ip}`);
        var _this = this;
        this.hyperdeck = new HyperdeckLib.Hyperdeck(ip);
        this.hyperdeck
            .onConnected()
            .then(function () {
            _this.hyperdeck.getNotifier().on('asynchronousEvent', function (response) {
                //console.log('Got an asynchronous event with code ' + response.code + '.');
            });
            _this.hyperdeck.getNotifier().on('connectionLost', function () {
                _this.connected = false;
                console.error('Connection lost.');
                if (_this.events.connectionlost != undefined)
                    _this.events.connectionlost();
            });
            if (_this.events.connected != undefined)
                _this.events.connected();
        })
            .catch(function (error) {
            console.log(error);
            _this.connected = false;
            if (_this.events.connecterror != undefined)
                _this.events.connecterror('Failed to connect to hyperdeck.');
        });
    }
    Mitti.prototype.on = function (event, callback) {
        if (this.events[event] != undefined)
            throw new Error("Event '".concat(event, "' already created"));
        else {
            this.events[event] = callback;
            if (event == 'connected' && this.connected == true)
                callback();
        }
    };
    Mitti.prototype.raw = function (command) {
        var _this_1 = this;
        return new Promise(function (resolve, reject) {
            //console.log('Sending command: ' + command);
            _this_1.hyperdeck
                .makeRequest(command)
                .then(function (response) {
                //console.log('Got response with code ' + response.code + '.');
                resolve(response);
            })
                .catch(function (errResponse) {
                if (!errResponse) {
                    reject(new Error('The request failed because the hyperdeck connection was lost.'));
                }
                else {
                    reject(new Error('The hyperdeck returned an error with status code ' + errResponse.code + '.'));
                }
            });
        });
    };
    Mitti.prototype.getTransportInfo = function () {
        var _this_1 = this;
        return new Promise(function (resolve, reject) {
            //console.log('Getting transport info');
            _this_1.hyperdeck
                .makeRequest('transport info')
                .then(function (response) {
                //console.log('Got response with code ' + response.code + '.');
                if (response.params != undefined) {
                    resolve(response.params);
                }
                else {
                    reject(new Error('Response contained no params'));
                }
            })
                .catch(function (errResponse) {
                if (!errResponse) {
                    reject(new Error('The request failed because the hyperdeck connection was lost.'));
                }
                else {
                    reject(new Error('The hyperdeck returned an error with status code ' + errResponse.code + '.'));
                }
            });
        });
    };
    Mitti.prototype.getClipCount = function () {
        var _this_1 = this;
        return new Promise(function (resolve, reject) {
            //console.log('Getting clips');
            _this_1.hyperdeck
                .makeRequest('clips count')
                .then(function (response) {
                //console.log('Got response with code ' + response.code + '.');
                if (response.params != undefined) {
                    if (response.params['clip count'] != undefined) {
                        resolve(response.params['clip count']);
                    }
                    else {
                        reject(new Error('Response contained no clip count data'));
                    }
                }
                else {
                    reject(new Error('Response contained no params data'));
                }
            })
                .catch(function (errResponse) {
                if (!errResponse) {
                    reject(new Error('The request failed because the hyperdeck connection was lost.'));
                }
                else {
                    reject(new Error('The hyperdeck returned an error with status code ' + errResponse.code + '.'));
                }
            });
        });
    };
    Mitti.prototype.getClips = function () {
        var _this_1 = this;
        return new Promise(function (resolve, reject) {
            //console.log('Getting clips');
            _this_1.hyperdeck
                .clipsGet()
                .then(function (response) {
                //console.log('Got response with code ' + response.code + '.');
                if (response.params != undefined) {
                    if (response.params['clip count'] != undefined)
                        delete response.params['clip count'];
                    resolve(response.params);
                }
                else {
                    reject(new Error('Response contained no clip data'));
                }
            })
                .catch(function (errResponse) {
                if (!errResponse) {
                    reject(new Error('The request failed because the hyperdeck connection was lost.'));
                }
                else {
                    reject(new Error('The hyperdeck returned an error with status code ' + errResponse.code + '.'));
                }
            });
        });
    };
    Mitti.prototype.selectClip = function (index) {
        var _this_1 = this;
        return new Promise(function (resolve, reject) {
            //console.log('Selecting clip');
            _this_1.hyperdeck
                .makeRequest("goto: clip id: ".concat(index))
                .then(function (response) {
                //console.log('Got response with code ' + response.code + '.');
                resolve(true);
            })
                .catch(function (errResponse) {
                if (!errResponse) {
                    reject(new Error('The request failed because the hyperdeck connection was lost.'));
                }
                else {
                    reject(new Error('The hyperdeck returned an error with status code ' + errResponse.code + '.'));
                }
            });
        });
    };
    Mitti.prototype.play = function () {
        var _this_1 = this;
        return new Promise(function (resolve, reject) {
            //console.log('Playing');
            _this_1.hyperdeck
                .play()
                .then(function (response) {
                //console.log('Got response with code ' + response.code + '.');
                resolve(true);
            })
                .catch(function (errResponse) {
                if (!errResponse) {
                    reject(new Error('The request failed because the hyperdeck connection was lost.'));
                }
                else {
                    reject(new Error('The hyperdeck returned an error with status code ' + errResponse.code + '.'));
                }
            });
        });
    };
    Mitti.prototype.stop = function () {
        var _this_1 = this;
        return new Promise(function (resolve, reject) {
            //console.log('Stopping');
            _this_1.hyperdeck
                .stop()
                .then(function (response) {
                //console.log('Got response with code ' + response.code + '.');
                resolve(true);
            })
                .catch(function (errResponse) {
                if (!errResponse) {
                    reject(new Error('The request failed because the hyperdeck connection was lost.'));
                }
                else {
                    reject(new Error('The hyperdeck returned an error with status code ' + errResponse.code + '.'));
                }
            });
        });
    };
    Mitti.prototype.next = function (wrap) {
        var _this_1 = this;
        return new Promise(function (resolve, reject) {
            //console.log('Next clip');
            _this_1.getTransportInfo()
                .then(function (transportInfo) {
                var newClip = parseInt(transportInfo['clip id']) + 1;
                if (wrap == true) {
                    _this_1.getClipCount()
                        .then(function (clipCount) {
                        if (newClip > clipCount)
                            newClip = 1;
                        _this_1.selectClip(newClip).then(resolve).catch(reject);
                    })
                        .catch(reject);
                }
                else
                    _this_1.selectClip(newClip).then(resolve).catch(reject);
            })
                .catch(reject);
        });
    };
    Mitti.prototype.previous = function (wrap) {
        var _this_1 = this;
        return new Promise(function (resolve, reject) {
            //console.log('Previous clip');
            _this_1.getTransportInfo()
                .then(function (transportInfo) {
                var newClip = parseInt(transportInfo['clip id']) - 1;
                if (wrap == true) {
                    _this_1.getClipCount()
                        .then(function (clipCount) {
                        if (newClip < 1)
                            newClip = clipCount;
                        _this_1.selectClip(newClip).then(resolve).catch(reject);
                    })
                        .catch(reject);
                }
                else
                    _this_1.selectClip(newClip).then(resolve).catch(reject);
            })
                .catch(reject);
        });
    };
    return Mitti;
}());
exports.Mitti = Mitti;
//# sourceMappingURL=MittiClass.js.map