"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var to_style_1 = require("./../vendor/to-style");
var styleStringToObject = to_style_1.default.object;
var SELF_CLOSING_TAG_NAMES = [
    'area',
    'base',
    'br',
    'col',
    'command',
    'embed',
    'hr',
    'img',
    'input',
    'keygen',
    'link',
    'meta',
    'param',
    'source',
    'track',
    'wbr',
];
var COLON = ':';
var SEMI = ';';
var OPEN_TAG = '<';
var CLOSE_TAG = '>';
var SPACE = ' ';
var EMPTY = '';
var EQ = '=';
var DQUOTE = '"';
var SLASH = '/';
var STYLE = 'style';
var CLASS_NAME = 'className';
var CLASS = 'class';
var manaChildToHtml = function (child, mapping, options) {
    if (cannotUse(child)) {
        return EMPTY;
    }
    if (alreadySerial(child)) {
        return child;
    }
    return manaToXml(EMPTY, child, mapping, options);
};
var isEmptyObject = function (object) {
    return object === null || object === undefined;
};
var styleToJSXString = function (style) {
    var obj;
    if (typeof style === 'string') {
        obj = styleStringToObject(style, { camelize: true }, null, null);
    }
    else {
        obj = style;
    }
    var out = '{' + JSON.stringify(obj) + '}';
    return out;
};
function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}
var styleToString = function (style) {
    var out = '';
    if (!style) {
        return out;
    }
    if (typeof style === 'string') {
        return style;
    }
    if (typeof style !== 'object') {
        return out;
    }
    for (var styleKey in style) {
        var styleValue = style[styleKey];
        if (typeof styleValue === 'string' ||
            typeof styleValue === 'boolean' ||
            isNumeric(styleValue)) {
            out += styleKey + COLON + styleValue + SEMI;
        }
    }
    return out;
};
var cannotUse = function (object) {
    return object === false || object === null || object === undefined || typeof object === 'function';
};
var alreadySerial = function (object) {
    return typeof object === 'string' || typeof object === 'number';
};
function manaToXml(accumulator, object, mapping, options) {
    var out = accumulator;
    if (alreadySerial(object)) {
        return object;
    }
    if (cannotUse(object)) {
        return EMPTY;
    }
    var name = object[(mapping && mapping.name) || 'elementName'];
    if (name && typeof name === 'object') {
        name = name.name;
    }
    var attributes = object[(mapping && mapping.attributes) || 'attributes'];
    var children = object[(mapping && mapping.children) || 'children'];
    if (name) {
        out += OPEN_TAG + name;
        var style = attributes && attributes[STYLE];
        if (style) {
            if (options && options.jsx) {
                style = styleToJSXString(style);
            }
            else {
                style = styleToString(style);
            }
            if (attributes) {
                attributes[STYLE] = style;
            }
        }
        if (attributes && !isEmptyObject(attributes)) {
            for (var attributeName in attributes) {
                var attrVal = attributes[attributeName];
                if (attributeName === STYLE) {
                    if (attrVal === EMPTY || isEmptyObject(attrVal)) {
                        continue;
                    }
                }
                if (attributeName === CLASS_NAME) {
                    attributeName = CLASS;
                }
                if (options && options.jsx && attributeName === STYLE) {
                    out += SPACE + attributeName + EQ + attrVal;
                }
                else {
                    out += SPACE + attributeName + EQ + DQUOTE + attrVal + DQUOTE;
                }
            }
        }
        out += CLOSE_TAG;
        if (Array.isArray(children)) {
            if (children && children.length > 0) {
                for (var i = 0; i < children.length; i++) {
                    out += manaChildToHtml(children[i], mapping, options);
                }
            }
        }
        else {
            out += manaChildToHtml(children, mapping, options);
        }
        if (SELF_CLOSING_TAG_NAMES.indexOf(name) === -1) {
            out += OPEN_TAG + SLASH + name + CLOSE_TAG;
        }
    }
    return out;
}
exports.default = manaToXml;
//# sourceMappingURL=manaToXml.js.map