Object.defineProperty(exports, "__esModule", { value: true });
module.exports = function (ActionAPI) {
    ActionAPI.handle(function (properties, status) {
        var connectionID = properties.connectionID != undefined ? properties.connectionID : 'none';
        var layer1 = properties.layer1 != undefined && properties.layer1.length > 0 ? properties.layer1 : 'ignore';
        var layer2 = properties.layer2 != undefined && properties.layer2.length > 0 ? properties.layer2 : 'ignore';
        var layer3 = properties.layer3 != undefined && properties.layer3.length > 0 ? properties.layer3 : 'ignore';
        var layer4 = properties.layer4 != undefined && properties.layer4.length > 0 ? properties.layer4 : 'ignore';
        var layer5 = properties.layer5 != undefined && properties.layer5.length > 0 ? properties.layer5 : 'ignore';
        if (connectionID != 'none') {
            var connection = ActionAPI.getConnection('wirecast-bridge', connectionID);
            if (connection && connection.instance) {
                connection.instance
                    .multiShot(layer1, layer2, layer3, layer4, layer5)
                    .then(function () {
                    status('Shots has been published', 'info');
                })
                    .catch(function (error) {
                    status(error, 'error');
                });
            }
        }
        else
            status('No connection specified', 'error');
    });
    function filterValues(values) {
        var newValues = [
            { id: 'ignore', text: 'Ignore' },
            { id: 'clear', text: 'Clear Layer' }
        ];
        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.length > 0
            ? properties.connectionID
            : 'none';
        var layerValues = {
            '1': properties.layer1 != undefined && properties.layer1.length > 0 ? properties.layer1 : 'ignore',
            '2': properties.layer2 != undefined && properties.layer2.length > 0 ? properties.layer2 : 'ignore',
            '3': properties.layer3 != undefined && properties.layer3.length > 0 ? properties.layer3 : 'ignore',
            '4': properties.layer4 != undefined && properties.layer4.length > 0 ? properties.layer4 : 'ignore',
            '5': properties.layer5 != undefined && properties.layer5.length > 0 ? properties.layer5 : 'ignore'
        };
        var connection = ActionAPI.getConnection('wirecast-bridge', connectionID);
        var connectionField = {
            id: 'connectionID',
            name: 'Connection',
            type: 'connection',
            connectionType: 'wirecast-bridge',
            value: connectionID
        };
        var fields = {};
        for (var i = 1; i < 6; i++) {
            fields[i] = {
                id: "layer".concat(i),
                name: "Layer ".concat(i),
                type: 'select',
                values: [],
                value: layerValues[i]
            };
        }
        var setFields = function () {
            EditorAPI.setFields([
                connectionField,
                fields[1],
                fields[2],
                fields[3],
                fields[4],
                fields[5]
            ]);
        };
        function updateLayerFields() {
            if (connection && connection.instance) {
                var instance = connection.instance;
                instance.getShots(function (shots) {
                    for (var layer in shots) {
                        var shotList = [];
                        for (var shotIndex in shots[layer].shots) {
                            var shot = shots[layer].shots[shotIndex];
                            shotList.push({ id: String(shot.id), text: shot.name });
                        }
                        fields[layer].value = layerValues[layer];
                        fields[layer].values = filterValues(shotList);
                    }
                    setFields();
                });
            }
            else
                setFields();
        }
        EditorAPI.onFieldChanges(function (fields) {
            var fieldObject = EditorAPI.tools.objectifyFieldsValues(fields);
            var toSave = {
                connectionID: fieldObject.connectionID
            };
            connectionField.value = fieldObject.connectionID;
            for (var i = 1; i < 6; i++) {
                fields[i].value = fieldObject["layer".concat(i)];
                toSave["layer".concat(i)] = fieldObject["layer".concat(i)];
            }
            EditorAPI.saveProperties(toSave);
            if (connectionID != fieldObject.connectionID) {
                connectionID = fieldObject.connectionID;
                if (fieldObject.connectionID != 'none') {
                    connection = ActionAPI.getConnection('wirecast-bridge', connectionID);
                    updateLayerFields();
                }
                else {
                    for (var i = 1; i < 6; i++) {
                        fields[i].values = [];
                    }
                    setFields();
                }
            }
        });
        updateLayerFields();
    });
};
//# sourceMappingURL=multiShot.js.map