Object.defineProperty(exports, "__esModule", { value: true });
module.exports = function (ActionAPI) {
    ActionAPI.handle(function (properties, status) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
        var layerID = properties.layerID != undefined && properties.layerID.length > 0 ? properties.layerID : 'none';
        if (connectionID != 'none' && connectionID.length > 0) {
            if (layerID != 'none' && layerID.length > 0) {
                var connection = ActionAPI.getConnection('wirecast-bridge', connectionID);
                if (connection && connection.instance) {
                    connection.instance
                        .clearLayer(layerID)
                        .then(function () {
                        status('Shot has been published', 'info');
                    })
                        .catch(function (error) {
                        status(error, 'error');
                    });
                }
            }
            else
                status('No layer specified', 'error');
        }
        else
            status('No connection specified', 'error');
    });
    function filterValues(values) {
        var newValues = [
            { id: 'none', text: 'None' }
        ];
        for (var i = 0; i < values.length; i++)
            if (values[i].text != 'Clear Layer')
                newValues.push(values[i]);
        return newValues;
    }
    ActionAPI.onOpenEditor(function (EditorAPI, properties) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
        var layerID = properties.layerID != undefined ? properties.layerID : 'none';
        var connection = ActionAPI.getConnection('wirecast-bridge', connectionID);
        var connectionField = {
            id: 'connectionID',
            name: 'Connection',
            type: 'connection',
            connectionType: 'wirecast-bridge',
            value: connectionID
        };
        var layerField = {
            id: 'layerID',
            name: 'Layer',
            type: 'select',
            values: [],
            value: layerID
        };
        var setFields = function () {
            EditorAPI.setFields([
                connectionField,
                layerField
            ]);
        };
        if (connection != undefined && connection.instance != undefined) {
            connection.instance.getLayersList(function (layers) {
                layerField.values = filterValues(layers.map(function (layer) {
                    return { id: layer.id, text: layer.name };
                }));
                setFields();
            });
        }
        else
            setFields();
        EditorAPI.onFieldChanges(function (fields) {
            var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields);
            connectionField.value = fieldObject.connectionID;
            layerField.value = fieldObject.layerID;
            EditorAPI.saveProperties({ connectionID: fieldObject.connectionID, layerID: fieldObject.layerID });
            if (connectionID != fieldObject.connectionID) {
                if (fieldObject.connectionID != 'none') {
                    connection = ActionAPI.getConnection('wirecast-bridge', fieldObject.connectionID);
                    if (connection != undefined && connection.instance != undefined) {
                        connection.instance.getLayersList(function (layers) {
                            layerField.values = filterValues(layers.map(function (layer) {
                                return { id: layer.id, text: layer.name };
                            }));
                            setFields();
                        });
                    }
                    else
                        setFields();
                }
                else {
                    layerField.values = [];
                    connectionID = 'none';
                    setFields();
                }
            }
        });
    });
};
//# sourceMappingURL=clearLayer.js.map