import { BinaryClassUpgrader } from "../../ecs/storage/binary/BinaryClassUpgrader.js";

export class GridPositionSerializationUpdater_0_1 extends BinaryClassUpgrader {
    constructor() {
        super();

        this.__targetVersion = 1;
        this.__startVersion = 0;
    }

    upgrade(source, target) {
        const x = source.readFloat64();
        const y = source.readFloat64();

        let header = 0;
        if (
            Number.isInteger(x)
            && Number.isInteger(y)
            && x >= 0
            && y >= 0
        ) {
            header = 1;
        }

        target.writeUint8(header);

        if (header === 1) {
            //both components are uint
            target.writeUintVar(x);
            target.writeUintVar(y);
        } else {
            target.writeFloat32(x);
            target.writeFloat32(y);
        }

    }
}
