"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
var Logger_1 = tslib_1.__importDefault(require("../Logger"));
var IGXC = tslib_1.__importStar(require("../IGXC"));
var Math_1 = require("../Math");
var Category_1 = require("../Category");
exports.preProcessIGXC = function (igxc, api, superRootId) {
    var e_1, _a;
    var validatedIGXC = IGXC.createScene();
    for (var key in igxc) {
        var value = igxc[key];
        if (ig_utilities_1.isValue(value)) {
            validatedIGXC[key] = value;
        }
    }
    for (var index = 0; index < validatedIGXC.Objects.length; index++) {
        var rawObject = validatedIGXC.Objects[index];
        if (ig_utilities_1.isNullOrUndefined(rawObject.Path)) {
            throw new Error("Refusing to load igxc, because the object at index \"" + index + "\" is missing a path!");
        }
        var validatedObject = IGXC.createObject(rawObject.Path);
        validatedIGXC.Objects[index] = validatedObject;
        for (var key in rawObject) {
            var value = rawObject[key];
            if (ig_utilities_1.isValue(value)) {
                validatedObject[key] = value;
            }
        }
    }
    try {
        for (var _b = tslib_1.__values(validatedIGXC.Objects), _c = _b.next(); !_c.done; _c = _b.next()) {
            var object = _c.value;
            preProcessNodeData(object, validatedIGXC, api, superRootId);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return validatedIGXC;
};
var preProcessNodeData = function (node, igxc, api, superRootId) {
    if (!node.Material && node.MaterialCategory) {
        var tAtPosition = node.MaterialCategory.indexOf("@");
        if (tAtPosition === 0) {
            // implicit material category, skip first char:
            node.Material = node.MaterialCategory.substring(1);
        }
        else if (tAtPosition > 0) {
            // Deprecated. To be removed sooner or later.
            // concatenation of material and category:
            Logger_1.default.warn("Deprecated Material Assignment: " + node.MaterialCategory + " in " + superRootId);
            var tMaterialCategory = node.MaterialCategory.substring(tAtPosition + 1);
            node.Material = node.MaterialCategory.substring(0, tAtPosition);
            node.MaterialCategory = tMaterialCategory;
        }
        else {
            node.Material = igxc.Categories[node.MaterialCategory];
        }
    }
    node.Categories = sanitizeObjectCategories(node.Categories);
    if (!ig_utilities_1.isNullOrUndefined(node.Transform) &&
        !ig_utilities_1.isNullOrUndefined(node.Transform.Rotation)) {
        var rotationNode = node.Transform.Rotation;
        if ("W" in rotationNode) {
            var quaternion = Math_1.createQuaternion(rotationNode.X, rotationNode.Y, rotationNode.Z, rotationNode.W);
            var euler = Math_1.createEulerFromQuaternion(quaternion, "xyz");
            rotationNode.X = Math_1.MathUtils.radToDeg(euler.x);
            rotationNode.Y = Math_1.MathUtils.radToDeg(euler.y);
            rotationNode.Z = Math_1.MathUtils.radToDeg(euler.z);
            delete rotationNode.W;
        }
        if ((rotationNode.X !== 0 && rotationNode.Y !== 0) ||
            (rotationNode.Y !== 0 && rotationNode.Z !== 0) ||
            (rotationNode.X !== 0 && rotationNode.Z !== 0)) {
            Logger_1.default.warn("Node " + node.Path + " in " + superRootId + ": only 1 rotation allowed, invalid: " + rotationNode.X + ", " + rotationNode.Y + ", " + rotationNode.Z);
        }
    }
    if (node.Geometry) {
        if (igxc.Geometries) {
            if (Object.keys(igxc.Geometries).indexOf(node.Geometry) < 0) {
                console.warn('Geometry "' +
                    node.Geometry +
                    '" of "' +
                    node.Path +
                    '" does not exist in .Geometries');
                node.Geometry = undefined;
            }
            else {
                api.GeometryLinks[node.Geometry] =
                    igxc.Geometries[node.Geometry];
            }
        }
        else {
            console.warn(".Geometry set for" +
                node.Path +
                " but no global .Geometries entry ?");
        }
    }
};
/**
 * while importing categories support now string[] as well as string and normalize to lowercase except standardized "durchWand"
 * @param input
 */
var sanitizeObjectCategories = function (input) {
    if (ig_utilities_1.isNullOrUndefined(input)) {
        return undefined;
    }
    var tTemp = [];
    if (typeof input === "string") {
        tTemp = input.split(",");
    }
    else {
        // array
        tTemp = input;
    }
    return tTemp
        .map(function (rawCategory) {
        var lowerCaseCategory = rawCategory.toLowerCase();
        if (rawCategory === Category_1.Category.CuttingThroughWall) {
            return rawCategory;
        }
        if (rawCategory !== lowerCaseCategory) {
            Logger_1.default.info("ObjectCategories: \"" + input + "\" must be lowercase! \"" + rawCategory + "\" modified to \"" + lowerCaseCategory + "\"");
            return lowerCaseCategory;
        }
        return rawCategory;
    })
        .join(",");
};
