import { BinaryClassSerializationAdapter } from "../storage/binary/BinaryClassSerializationAdapter.js";
import Team from "./Team.js";

export class TeamSerializationAdapter extends BinaryClassSerializationAdapter {

    klass = Team;
    version = 0;

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {Team} value
     */
    serialize(buffer, value) {
        const v = value.getValue();

        if (v === Infinity) {
            buffer.writeInt32(2147483647);
        } else if (v === -Infinity) {
            buffer.writeInt32(-2147483648);
        } else {
            //TODO it's possible to write encoded Infinity values by accident
            buffer.writeInt32(v);
        }
    }

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {Team} value
     */
    deserialize(buffer, value) {
        const v = buffer.readInt32();

        if (v === 2147483647) {
            value.set(Infinity);
        } else if (v === -2147483648) {
            value.set(-Infinity);
        } else {
            value.set(v);
        }
    }
}
