"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var parseCssTransformString_1 = require("./../helpers/parseCssTransformString");
var visitManaTree_1 = require("./../helpers/visitManaTree");
var css_value_1 = require("./../vendor/css-value");
var Layout3D_1 = require("./../Layout3D");
var ROOT_LOCATOR = '0';
var TRANSFORM_COMPONENT_WHITELIST = {
    'rotation.x': true,
    'rotation.y': true,
    'rotation.z': true,
    'rotation.w': true,
    'scale.x': true,
    'scale.y': true,
    'scale.z': true,
    'translation.x': true,
    'translation.y': true,
    'translation.z': true,
    'mount.x': true,
    'mount.y': true,
    'mount.z': true,
    'origin.x': true,
    'origin.y': true,
    'origin.z': true,
    'align.x': true,
    'align.y': true,
    'align.z': true,
};
function isNumericDefined(value) {
    return typeof value === 'number';
}
function determineSizingProp(sizeAxis, attributeValue) {
    var parsedValues = css_value_1.default(attributeValue + '');
    var parsedValue = parsedValues[0];
    switch (parsedValue.unit) {
        case '%':
            return {
                name: 'sizeProportional',
                value: parsedValue.value / 100,
                mode: 0,
            };
        case 'px':
            return {
                name: 'sizeAbsolute',
                value: parsedValue.value,
                mode: 1,
            };
        case '':
            return {
                name: 'sizeAbsolute',
                value: parsedValue.value,
                mode: 1,
            };
        default:
            return false;
    }
}
var DEFAULT_SIZE_ABSOLUTE = 100;
var DEFAULT_SIZE_PROPORTIONAL = 1.0;
function fallbackSizeAbsolute(node, axis) {
    if (node && node.attributes) {
        var viewboxString = (node.attributes.viewBox || node.attributes.viewbox) + '';
        var viewboxPieces = viewboxString.split(/\s+/).map(function (str) { return Number(str); });
        if (axis === 'x') {
            return viewboxPieces[2] || DEFAULT_SIZE_ABSOLUTE;
        }
        if (axis === 'y') {
            return viewboxPieces[3] || DEFAULT_SIZE_ABSOLUTE;
        }
    }
    return DEFAULT_SIZE_ABSOLUTE;
}
function fallbackSizeProportional(node, axis) {
    return DEFAULT_SIZE_PROPORTIONAL;
}
function convertManaLayout(mana) {
    visitManaTree_1.default(ROOT_LOCATOR, mana, function (name, attributes, children, node, locator, parent, index) {
        if (!name || !attributes) {
            return void 0;
        }
        if (name.states) {
            var width = name.states.width;
            var height = name.states.height;
            if (width && width.type === 'number') {
                attributes['sizeAbsolute.x'] = width.value;
                attributes['sizeMode.x'] = 1;
            }
            if (height && height.type === 'number') {
                attributes['sizeAbsolute.y'] = height.value;
                attributes['sizeMode.y'] = 1;
            }
        }
        if (attributes.width !== undefined && attributes.width !== null) {
            var widthProp = determineSizingProp('x', attributes.width);
            if (widthProp) {
                attributes[widthProp.name + '.x'] = widthProp.value;
                attributes['sizeMode.x'] = widthProp.mode;
                delete attributes.width;
            }
        }
        if (attributes.height !== undefined && attributes.height !== null) {
            var heightProp = determineSizingProp('y', attributes.height);
            if (heightProp) {
                attributes[heightProp.name + '.y'] = heightProp.value;
                attributes['sizeMode.y'] = heightProp.mode;
                delete attributes.height;
            }
        }
        if (attributes.style && typeof attributes.style === 'object') {
            if (attributes.style.width !== undefined &&
                attributes.style.width !== null) {
                var widthStyleProp = determineSizingProp('x', attributes.style.width);
                if (widthStyleProp) {
                    attributes[widthStyleProp.name + '.x'] = widthStyleProp.value;
                    attributes['sizeMode.x'] = widthStyleProp.mode;
                    delete attributes.style.width;
                }
            }
            if (attributes.style.height !== undefined &&
                attributes.style.height !== null) {
                var heightStyleProp = determineSizingProp('y', attributes.style.height);
                if (heightStyleProp) {
                    attributes[heightStyleProp.name + '.y'] = heightStyleProp.value;
                    attributes['sizeMode.y'] = heightStyleProp.mode;
                    delete attributes.style.height;
                }
            }
        }
        if (!parent) {
            if (!isNumericDefined(attributes['sizeMode.x'])) {
                attributes['sizeMode.x'] = Layout3D_1.default.SIZE_ABSOLUTE;
            }
            if (!isNumericDefined(attributes['sizeMode.y'])) {
                attributes['sizeMode.y'] = Layout3D_1.default.SIZE_ABSOLUTE;
            }
            if (!isNumericDefined(attributes['sizeAbsolute.x'])) {
                attributes['sizeAbsolute.x'] = fallbackSizeAbsolute(node, 'x');
            }
            if (!isNumericDefined(attributes['sizeAbsolute.y'])) {
                attributes['sizeAbsolute.y'] = fallbackSizeAbsolute(node, 'y');
            }
            if (attributes['sizeMode.x'] === Layout3D_1.default.SIZE_PROPORTIONAL &&
                (!isNumericDefined(attributes['sizeProportional.x']))) {
                attributes['sizeProportional.x'] = fallbackSizeProportional(node, 'x');
            }
            if (attributes['sizeMode.y'] === Layout3D_1.default.SIZE_PROPORTIONAL &&
                (!isNumericDefined(attributes['sizeProportional.y']))) {
                attributes['sizeProportional.y'] = fallbackSizeProportional(node, 'y');
            }
        }
        if (attributes.transform !== undefined && attributes.transform !== null) {
            var transformAttributes = parseCssTransformString_1.default(attributes.transform);
            for (var transformAttributeName in transformAttributes) {
                var transformValue = transformAttributes[transformAttributeName];
                if (!TRANSFORM_COMPONENT_WHITELIST[transformAttributeName]) {
                    console.warn('Skipping transform attribute ' +
                        transformAttributeName +
                        ' because it is not yet supported');
                    continue;
                }
                attributes[transformAttributeName] = transformValue;
            }
            delete attributes.transform;
            if (node.elementName !== 'image') {
                delete attributes.x;
                delete attributes.y;
            }
        }
    }, null, null);
    return mana;
}
exports.default = convertManaLayout;
//# sourceMappingURL=convertManaLayout.js.map