"use strict";
/**
 * Copyright (c) Haiku 2016-2018. All rights reserved.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var applyCssLayout_1 = require("./../../layout/applyCssLayout");
var scopeOfElement_1 = require("./../../layout/scopeOfElement");
var modernizr_1 = require("./../../vendor/modernizr");
var getWindowsBrowserVersion_1 = require("./getWindowsBrowserVersion");
var isEdge_1 = require("./isEdge");
var isIE_1 = require("./isIE");
var isMobile_1 = require("./isMobile");
var isTextNode_1 = require("./isTextNode");
var SVG = 'svg';
var safeWindow = typeof window !== 'undefined' && window;
var PLATFORM_INFO = {
    hasWindow: !!safeWindow,
    isMobile: isMobile_1.default(safeWindow),
    isIE: isIE_1.default(safeWindow),
    isEdge: isEdge_1.default(safeWindow),
    windowsBrowserVersion: getWindowsBrowserVersion_1.default(safeWindow),
    hasPreserve3d: modernizr_1.default.hasPreserve3d(safeWindow),
};
var SVG_RENDERABLES = {
    a: true,
    audio: true,
    canvas: true,
    circle: true,
    ellipse: true,
    filter: true,
    foreignObject: true,
    g: true,
    iframe: true,
    image: true,
    line: true,
    mesh: true,
    path: true,
    pattern: true,
    polygon: true,
    polyline: true,
    rect: true,
    svg: true,
    switch: true,
    symbol: true,
    text: true,
    textPath: true,
    tspan: true,
    unknown: true,
    use: true,
    video: true,
};
function applyLayout(domElement, virtualElement, parentDomNode, parentVirtualElement, component, isPatchOperation) {
    if (isTextNode_1.default(virtualElement)) {
        return domElement;
    }
    if (virtualElement.layout) {
        // Don't assign layout to things that never need it like <desc>, <title>, etc.
        // Check if we're inside an <svg> component *and* one of the actually renderable svg-type els
        if (scopeOfElement_1.default(virtualElement) === SVG &&
            !SVG_RENDERABLES[virtualElement.elementName]) {
            return domElement;
        }
        if (!parentVirtualElement) {
            warnOnce('Cannot compute layout without parent element');
            return domElement;
        }
        if (!parentVirtualElement.layout || !parentVirtualElement.layout.computed) {
            warnOnce('Cannot compute layout without parent computed size (child: <' +
                virtualElement.elementName +
                '>; parent: <' +
                parentVirtualElement.elementName +
                '>)');
            return domElement;
        }
        var computedLayout = virtualElement.layout.computed;
        // No computed layout means the el is not shown
        if (!computedLayout || computedLayout.invisible) {
            if (domElement.style.display !== 'none') {
                domElement.style.display = 'none';
            }
        }
        else {
            if (domElement.style.display !== 'block') {
                domElement.style.display = 'block';
            }
            component.config.platform = PLATFORM_INFO;
            applyCssLayout_1.default(domElement, virtualElement, virtualElement.layout, computedLayout, component);
        }
    }
    return domElement;
}
exports.default = applyLayout;
var warnings = {};
function warnOnce(warning) {
    if (warnings[warning]) {
        return void 0;
    }
    warnings[warning] = true;
    console.warn('[haiku core] warning:', warning);
}
//# sourceMappingURL=applyLayout.js.map