"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var assignClass_1 = require("./assignClass");
var assignStyle_1 = require("./assignStyle");
var STYLE = 'style';
var OBJECT = 'object';
var FUNCTION = 'function';
var CLASS = 'class';
var CLASS_NAME = 'className';
var NS = 'http://www.w3.org/1999/xlink';
var XLINK_HREF = 'xlink:href';
function setAttribute(domElement, virtualElement, key, val, component) {
    if (key === XLINK_HREF) {
        var p0 = domElement.getAttributeNS(NS, key);
        if (p0 !== val) {
            domElement.setAttributeNS(NS, key, val);
        }
    }
    else {
        var p1 = domElement.getAttribute(key);
        if (p1 !== val) {
            domElement.setAttribute(key, val);
        }
    }
}
function assignAttributes(domElement, virtualElement, component, isPatchOperation) {
    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);
                    }
                }
            }
        }
    }
    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;
        }
        setAttribute(domElement, virtualElement, key, anotherNewValue, component);
    }
    return domElement;
}
exports.default = assignAttributes;
//# sourceMappingURL=assignAttributes.js.map