"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var assignClass_1 = require("./assignClass");
var assignStyle_1 = require("./assignStyle");
var attachEventListener_1 = require("./attachEventListener");
var getFlexId_1 = require("./getFlexId");
var STYLE = 'style';
var OBJECT = 'object';
var FUNCTION = 'function';
var CLASS = 'class';
var CLASS_NAME = 'className';
function setAttribute(el, key, val, cache) {
    if (key[0] === 'x' && key[1] === 'l' && key[2] === 'i' && key[3] === 'n' && key[4] === 'k') {
        var ns = 'http://www.w3.org/1999/xlink';
        if (val[0] === 'd' &&
            val[1] === 'a' &&
            val[2] === 't' &&
            val[3] === 'a' &&
            val[4] === ':' &&
            val[5] === 'i' &&
            val[6] === 'm' &&
            val[7] === 'a' &&
            val[8] === 'g' &&
            val[9] === 'e' &&
            val[10] === '/') {
            if (!cache.base64image) {
                el.setAttributeNS(ns, key, val);
                cache.base64image = true;
            }
        }
        else {
            var p0 = el.getAttributeNS(ns, key);
            if (p0 !== val) {
                el.setAttributeNS(ns, key, val);
            }
        }
    }
    else {
        var p1 = el.getAttribute(key);
        if (p1 !== val) {
            el.setAttribute(key, val);
        }
    }
}
function assignAttributes(domElement, virtualElement, component, isPatchOperation) {
    var flexId = getFlexId_1.default(virtualElement);
    var cache = component.cache[flexId];
    if (!isPatchOperation) {
        if (domElement.haiku && domElement.haiku.element) {
            for (var oldKey in domElement.haiku.element.attributes) {
                var oldValue = domElement.haiku.element.attributes[oldKey];
                var newValue = virtualElement.attributes[oldKey];
                if (oldKey !== STYLE) {
                    if (newValue === null || newValue === undefined || oldValue !== newValue) {
                        domElement.removeAttribute(oldKey);
                        if (cache[oldKey]) {
                            cache[oldKey] = null;
                        }
                    }
                }
            }
        }
    }
    for (var key in virtualElement.attributes) {
        var anotherNewValue = virtualElement.attributes[key];
        if (key === STYLE &&
            anotherNewValue &&
            typeof anotherNewValue === OBJECT) {
            if (Object.keys(anotherNewValue).length > 0) {
                assignStyle_1.default(domElement, anotherNewValue, component, isPatchOperation);
            }
            continue;
        }
        if ((key === CLASS || key === CLASS_NAME) && anotherNewValue) {
            assignClass_1.default(domElement, anotherNewValue);
            continue;
        }
        if (key[0] === 'o' && key[1] === 'n' && typeof anotherNewValue === FUNCTION) {
            attachEventListener_1.default(virtualElement, domElement, key.slice(2).toLowerCase(), anotherNewValue, component);
            continue;
        }
        setAttribute(domElement, key, anotherNewValue, cache);
    }
    if (virtualElement.__handlers) {
        for (var eventName in virtualElement.__handlers) {
            var handler = virtualElement.__handlers[eventName];
            if (!handler.__subscribed) {
                attachEventListener_1.default(virtualElement, domElement, eventName, handler, component);
                handler.__subscribed = true;
            }
        }
    }
    return domElement;
}
exports.default = assignAttributes;
//# sourceMappingURL=assignAttributes.js.map