Object.defineProperty(exports, "__esModule", { value: true });
var Atem = require('atem-connection').Atem;
module.exports = function (ActionAPI) {
    ActionAPI.onExecute(function (properties, status) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : "none";
        var input = properties.input != undefined ? properties.input : "none";
        if (connectionID != 'none') {
            if (input != 'none') {
                var connection = ActionAPI.getConnection('bm_atem', connectionID);
                if (connection && connection.instance) {
                    var atem = connection.instance;
                    try {
                        atem.changePreviewInput(input);
                        status("Atem preview was set to ".concat(input), 'info');
                    }
                    catch (error) {
                        status("Error whilst setting atem input: ".concat(error), 'error');
                    }
                }
                else
                    status('Atem is not online', 'error');
            }
            else
                status('No input specified', 'error');
        }
        else
            status('No connection specified', 'error');
    });
    ActionAPI.onOpenEditor(function (EditorAPI, properties) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : "none";
        var input = properties.input != undefined ? properties.input : "none";
        var connectionField = {
            id: 'connectionID',
            name: "Connection",
            type: "connection",
            connectionType: "bm_atem",
            value: connectionID
        };
        var inputField = {
            id: "input",
            name: "Input",
            type: "select",
            value: input,
            values: [{ id: 'none', text: "None" }]
        };
        var validate = function () {
            if (connectionID != 'none') {
                var connection = ActionAPI.getConnection('bm_atem', connectionID);
                if (connection && connection.instance) {
                    var atem = connection.instance;
                    var inputs = atem.state.inputs;
                    var values = [{ id: 'none', text: 'None' }];
                    for (var key in inputs)
                        if (parseInt(key) < 1000)
                            values.push({ id: inputs[key].inputId, text: inputs[key].longName });
                    inputField.values = values;
                }
                else
                    inputField.values = [{ id: 'none', text: 'none' }];
            }
            else
                inputField.values = [{ id: 'none', text: 'none' }];
            EditorAPI.setFields([connectionField, inputField]);
        };
        EditorAPI.onFieldChanges(function (fields) {
            var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields);
            connectionID = fieldObject.connectionID;
            input = fieldObject.input;
            connectionField.value = connectionID;
            inputField.value = input;
            EditorAPI.saveProperties({ connectionID: connectionID, input: input });
            validate();
        });
        validate();
    });
};
//# sourceMappingURL=setPreview.js.map