import { __read, __values } from "tslib";
import { MathUtils } from "@intelligentgraphics/ig.gfx.core";
import { CurrentMajorVersion, CurrentMinorVersion, EItemType, } from "./IGXB";
export var convertIGXB = function (source) {
    var _a = __read(source.FormatVersion.split(".").map(function (num) {
        return parseInt(num);
    }), 2), major = _a[0], minor = _a[1];
    if (major === CurrentMajorVersion && minor === 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 = CurrentMajorVersion + "." + 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 = __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 === EItemType.Article) {
                if (item.Rotation !== undefined) {
                    item.Rotation = {
                        x: MathUtils.radToDeg(item.Rotation.x),
                        y: MathUtils.radToDeg(item.Rotation.y),
                        z: MathUtils.radToDeg(item.Rotation.z),
                    };
                }
                if (item.SubItems !== undefined) {
                    patchRotations(item.SubItems);
                }
                continue;
            }
            if (item.ItemType === EItemType.Group) {
                patchRotations(item.Items);
                continue;
            }
            if (item.ItemType === 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; }
    }
};
