"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var visitManaTree_1 = require("./visitManaTree");
var xmlToMana_1 = require("./xmlToMana");
var STRING_TYPE = 'string';
function upgradeBytecodeInPlace(bytecode, options) {
    if (!bytecode.states) {
        bytecode.states = {};
    }
    if (bytecode.properties) {
        var properties = bytecode.properties;
        delete bytecode.properties;
        for (var i = 0; i < properties.length; i++) {
            var propertySpec = properties[i];
            var updatedSpec = {};
            if (propertySpec.value !== undefined) {
                updatedSpec['value'] = propertySpec.value;
            }
            if (propertySpec.type !== undefined) {
                updatedSpec['type'] = propertySpec.type;
            }
            if (propertySpec.setter !== undefined) {
                updatedSpec['set'] = propertySpec.setter;
            }
            if (propertySpec.getter !== undefined) {
                updatedSpec['get'] = propertySpec.getter;
            }
            if (propertySpec.set !== undefined) {
                updatedSpec['set'] = propertySpec.set;
            }
            if (propertySpec.get !== undefined) {
                updatedSpec['get'] = propertySpec.get;
            }
            bytecode.states[propertySpec.name] = updatedSpec;
        }
    }
    if (Array.isArray(bytecode.eventHandlers)) {
        var eventHandlers = bytecode.eventHandlers;
        delete bytecode.eventHandlers;
        bytecode.eventHandlers = {};
        for (var j = 0; j < eventHandlers.length; j++) {
            var eventHandlerSpec = eventHandlers[j];
            if (!bytecode.eventHandlers[eventHandlerSpec.selector]) {
                bytecode.eventHandlers[eventHandlerSpec.selector] = {};
            }
            bytecode.eventHandlers[eventHandlerSpec.selector][eventHandlerSpec.name] = {
                handler: eventHandlerSpec.handler,
            };
        }
    }
    if (typeof bytecode.template === STRING_TYPE) {
        bytecode.template = xmlToMana_1.default(bytecode.template);
    }
    var referencesToUpdate = {};
    var alreadyUpdatedReferences = {};
    if (options && options.referenceUniqueness) {
        if (bytecode.template) {
            visitManaTree_1.default('0', bytecode.template, function (elementName, attributes, children, node) {
                if (elementName === 'filter') {
                    if (attributes.id && !alreadyUpdatedReferences[attributes.id]) {
                        var prev = attributes.id;
                        var next = prev + '-' + options.referenceUniqueness;
                        attributes.id = next;
                        referencesToUpdate['url(#' + prev + ')'] = 'url(#' + next + ')';
                        alreadyUpdatedReferences[attributes.id] = true;
                    }
                }
            }, null, 0);
        }
    }
    if (bytecode.timelines) {
        for (var timelineName in bytecode.timelines) {
            for (var selector in bytecode.timelines[timelineName]) {
                if (bytecode.timelines[timelineName][selector]['backgroundColor'] &&
                    !bytecode.timelines[timelineName][selector]['style.backgroundColor']) {
                    bytecode.timelines[timelineName][selector]['style.backgroundColor'] =
                        bytecode.timelines[timelineName][selector]['backgroundColor'];
                    delete bytecode.timelines[timelineName][selector]['backgroundColor'];
                    continue;
                }
                if (bytecode.timelines[timelineName][selector]['filter']) {
                    for (var keyframeMs in bytecode.timelines[timelineName][selector]['filter']) {
                        var keyframeDesc = bytecode.timelines[timelineName][selector]['filter'][keyframeMs];
                        if (keyframeDesc && referencesToUpdate[keyframeDesc.value]) {
                            keyframeDesc.value = referencesToUpdate[keyframeDesc.value];
                        }
                    }
                }
            }
        }
    }
}
exports.default = upgradeBytecodeInPlace;
//# sourceMappingURL=upgradeBytecodeInPlace.js.map