Object.defineProperty(exports, "__esModule", { value: true });
module.exports = function (actionAPI) {
    //Handle the action when executed
    actionAPI.handle(function (properties, status) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
        var sourceID = properties.sourceID != undefined ? properties.sourceID : 'none';
        if (connectionID != 'none') {
            if (sourceID != 'none') {
                var connection = actionAPI.getConnection('studiomonitor', connectionID);
                if (connection.instance != undefined) {
                    var monitor = connection.instance;
                    monitor
                        .setSource(sourceID)
                        .then(function () {
                        status('Source was set');
                    })
                        .catch(function (error) {
                        status("Error whilst setting source: ".concat(error.message), 'error');
                    });
                }
            }
            else
                status('No source specified', 'error');
        }
        else
            status('No connection specified', 'error');
    });
    //Handle the interactive editor
    actionAPI.onOpenEditor(function (editorAPI, properties) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
        var sourceID = properties.sourceID != undefined ? properties.sourceID : 'none';
        var monitor = null;
        var currentIP = null;
        var currentPort = null;
        var fields = [
            {
                id: 'connectionID',
                name: 'Connection',
                type: 'connection',
                value: connectionID,
                connectionType: 'studiomonitor'
            },
            {
                id: 'sourceID',
                name: 'Source',
                type: 'select',
                value: sourceID,
                values: []
            }
        ];
        function updateAddress(connectionID, callback) {
            if (connectionID != 'none') {
                var connection = actionAPI.getConnection('studiomonitor', connectionID);
                var ip = connection.properties.ip;
                var port = connection.properties.port;
                if (currentIP != ip || currentPort != port) {
                    currentIP = ip;
                    currentPort = port;
                    monitor = connection.instance;
                    callback(true);
                }
                else {
                    callback(false);
                }
            }
            else {
                if (currentIP != null || currentPort != null) {
                    currentIP = null;
                    currentPort = null;
                    monitor = null;
                    callback(true);
                }
                else {
                    callback(false);
                }
            }
        }
        function updateSources(callback) {
            if (monitor != null)
                monitor
                    .getSources()
                    .then(function (sources) {
                    fields[1].values = sources.map(function (source) {
                        return { id: source, text: source };
                    });
                    callback();
                })
                    .catch(function (error) {
                    fields[1].values = [];
                    callback();
                });
            else {
                fields[1].values = [];
                callback();
            }
        }
        function validate(fieldValues) {
            fields[0].value = fieldValues.connectionID;
            fields[1].value = fieldValues.sourceID;
            editorAPI.saveProperties({ connectionID: fieldValues.connectionID, sourceID: fieldValues.sourceID });
            updateAddress(fieldValues.connectionID, function (changed) {
                if (changed) {
                    updateSources(function () {
                        editorAPI.setFields(fields);
                    });
                }
                else
                    editorAPI.setFields(fields);
            });
        }
        validate({ connectionID: connectionID, sourceID: sourceID });
        editorAPI.onFieldChanges(function (fields) {
            var fieldValues = editorAPI.tools.objectifyFieldsValues(fields);
            validate(fieldValues);
        });
    });
};
//# sourceMappingURL=source.js.map