"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ig_gfx_core_1 = require("@intelligentgraphics/ig.gfx.core");
var IGXB_1 = require("./IGXB");
exports.convertIGXB = function (source) {
    var _a = tslib_1.__read(source.FormatVersion.split(".").map(function (num) {
        return parseInt(num);
    }), 2), major = _a[0], minor = _a[1];
    if (major === IGXB_1.CurrentMajorVersion && minor === IGXB_1.CurrentMinorVersion) {
        return source;
    }
    var converter = converters[source.FormatVersion];
    if (converter === undefined) {
        throw new Error("Could not find converter for \"" + source.FormatVersion + "\"");
    }
    var result = converter(source);
    result.FormatVersion = IGXB_1.CurrentMajorVersion + "." + IGXB_1.CurrentMinorVersion;
    return result;
};
var converters = {
    "1.0": function (source) {
        patchRotations(source.Items);
        return source;
    },
};
var patchRotations = function (items) {
    var e_1, _a;
    try {
        for (var items_1 = tslib_1.__values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
            var item = items_1_1.value;
            if (item.ItemType === IGXB_1.EItemType.Article) {
                if (item.Rotation !== undefined) {
                    item.Rotation = {
                        x: ig_gfx_core_1.MathUtils.radToDeg(item.Rotation.x),
                        y: ig_gfx_core_1.MathUtils.radToDeg(item.Rotation.y),
                        z: ig_gfx_core_1.MathUtils.radToDeg(item.Rotation.z),
                    };
                }
                if (item.SubItems !== undefined) {
                    patchRotations(item.SubItems);
                }
                continue;
            }
            if (item.ItemType === IGXB_1.EItemType.Group) {
                patchRotations(item.Items);
                continue;
            }
            if (item.ItemType === IGXB_1.EItemType.Set) {
                patchRotations(item.Items);
                continue;
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
};
