import { MathUtils } from "@intelligentgraphics/ig.gfx.core";
import { CurrentMajorVersion, CurrentMinorVersion, EItemType, } from "./IGXB";
export const convertIGXB = (source) => {
    const [major, minor] = source.FormatVersion.split(".").map(num => parseInt(num));
    if (major === CurrentMajorVersion && minor === CurrentMinorVersion) {
        return source;
    }
    const converter = converters[source.FormatVersion];
    if (converter === undefined) {
        throw new Error(`Could not find converter for "${source.FormatVersion}"`);
    }
    const result = converter(source);
    result.FormatVersion = `${CurrentMajorVersion}.${CurrentMinorVersion}`;
    return result;
};
const converters = {
    "1.0": (source) => {
        patchRotations(source.Items);
        return source;
    },
};
const patchRotations = (items) => {
    for (const item of items) {
        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;
        }
    }
};
