Object.defineProperty(exports, "__esModule", { value: true });
module.exports = function (ActionAPI) {
    ActionAPI.handle(function (properties, status) {
        var connectionID = properties.connectionID != undefined
            ? properties.connectionID
            : 'none';
        var sceneID = properties.sceneID != undefined ? properties.sceneID : 'none';
        var displayID = properties.displayID != undefined ? properties.displayID : 'none';
        if (connectionID != 'none') {
            if (sceneID != 'none') {
                if (displayID != 'none') {
                    var connection = ActionAPI.getConnection('channel', connectionID);
                    if (connection) {
                        var channel = connection.instance;
                        return channel.publishScene(sceneID, displayID);
                    }
                    else
                        status("Connection doesn't exist", 'error');
                }
                else
                    status('No display specfied', 'error');
            }
            else
                status('No scene specfied', 'error');
        }
        else
            status('No connection specfied', 'error');
    });
    ActionAPI.onOpenEditor(function (EditorAPI, properties) {
        var connectionID = properties.connectionID != undefined
            ? properties.connectionID
            : 'none';
        var sceneID = properties.sceneID != undefined ? properties.sceneID : 'none';
        var displayID = properties.displayID != undefined ? properties.displayID : 'none';
        var connectionField = {
            id: 'connectionID',
            name: 'Connection',
            type: 'connection',
            connectionType: 'channel',
            value: connectionID,
        };
        var sceneField = {
            id: 'sceneID',
            name: 'Scene',
            type: 'select',
            value: sceneID,
            values: [],
        };
        var displayField = {
            id: 'displayID',
            name: 'Display',
            type: 'select',
            value: displayID,
            values: [],
        };
        var lastConnectionID = null;
        var validate = function () {
            if (connectionID != lastConnectionID) {
                lastConnectionID = connectionID;
                if (lastConnectionID != 'none') {
                    var connection = ActionAPI.getConnection('channel', lastConnectionID);
                    if (connection) {
                        var channel = connection.instance;
                        channel
                            .getScenes()
                            .then(function (scenes) {
                            sceneField.values = scenes.map(function (scene) {
                                return { id: scene.id, text: scene.name };
                            });
                            channel
                                .getDisplays()
                                .then(function (displays) {
                                displayField.values = displays.map(function (display) {
                                    return {
                                        id: display.id,
                                        text: display.name,
                                    };
                                });
                                EditorAPI.setFields([
                                    connectionField,
                                    sceneField,
                                    displayField,
                                ]);
                            })
                                .catch(function (error) {
                                sceneField.values = [];
                                EditorAPI.setFields([
                                    connectionField,
                                    sceneField,
                                    displayField,
                                ]);
                            });
                        })
                            .catch(function (error) {
                            sceneField.values = [];
                            EditorAPI.setFields([
                                connectionField,
                                sceneField,
                                displayField,
                            ]);
                        });
                    }
                    else {
                        sceneField.values = [];
                        EditorAPI.setFields([
                            connectionField,
                            sceneField,
                            displayField,
                        ]);
                    }
                }
                else {
                    sceneField.values = [];
                    EditorAPI.setFields([
                        connectionField,
                        sceneField,
                        displayField,
                    ]);
                }
            }
        };
        EditorAPI.onFieldChanges(function (fields) {
            var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields);
            connectionID = fieldObject.connectionID;
            sceneID = fieldObject.sceneID;
            displayID = fieldObject.displayID;
            connectionField.value = connectionID;
            sceneField.value = sceneID;
            EditorAPI.saveProperties({ connectionID: connectionID, sceneID: sceneID, displayID: displayID });
            validate();
        });
        validate();
    });
};
//# sourceMappingURL=publishScene.js.map