Object.defineProperty(exports, "__esModule", { value: true });
module.exports = function (ActionAPI) {
    ActionAPI.handle(function (properties, status) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
        var game = properties.game != undefined ? properties.game : 'none';
        if (connectionID != 'none') {
            if (game != 'none') {
                var connection = ActionAPI.getConnection('moorentv', connectionID);
                if (connection) {
                    var mtv = connection.instance;
                    mtv.startGame(game).then(function () {
                    }).catch(function (error) {
                        status(error, 'error');
                    });
                }
                else
                    status("Connection doesn't exist", 'error');
            }
            else
                status('No game specified', 'error');
        }
        else
            status('No connection specified', 'error');
    });
    ActionAPI.onOpenEditor(function (EditorAPI, properties) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
        var game = properties.game != undefined ? properties.game : 'none';
        var connectionField = {
            id: 'connectionID',
            name: "Connection",
            type: "connection",
            connectionType: 'moorentv',
            value: connectionID
        };
        var gameField = {
            id: 'game',
            name: 'Game',
            type: 'select',
            value: game,
            values: []
        };
        var sendFields = function (clearGameField) {
            if (clearGameField === void 0) { clearGameField = false; }
            if (clearGameField)
                gameField.values = [{ id: 'none', text: 'None' }];
            EditorAPI.setFields([connectionField, gameField]);
        };
        var lastConnectionID = null;
        var validate = function () {
            if (lastConnectionID != connectionID) {
                lastConnectionID = connectionID;
                if (connectionID != 'none') {
                    var connection = ActionAPI.getConnection('moorentv', connectionID);
                    if (connection) {
                        var mtv = connection.instance;
                        mtv.getGames().then(function (games) {
                            var values = [{ id: 'none', text: 'None' }];
                            for (var gameID in games)
                                values.push({ id: games[gameID].id, text: games[gameID].title });
                            gameField.values = values;
                            sendFields();
                        }).catch(function (error) {
                            sendFields(true);
                        });
                    }
                    else
                        sendFields(true);
                }
                else
                    sendFields(true);
            }
        };
        EditorAPI.onFieldChanges(function (fields) {
            var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields);
            connectionID = fieldObject.connectionID;
            game = fieldObject.game;
            connectionField.value = connectionID;
            gameField.value = game;
            EditorAPI.saveProperties({ connectionID: connectionID, game: game });
            validate();
        });
        validate();
    });
};
//# sourceMappingURL=startGame.js.map