import {
    ObjectBasedClassSerializationAdapter
} from "../../../ecs/storage/binary/object/ObjectBasedClassSerializationAdapter.js";
import { SequenceBehavior } from "./SequenceBehavior.js";

export class SequenceBehaviorSerializationAdapter extends ObjectBasedClassSerializationAdapter {
    constructor() {
        super();

        this.klass = SequenceBehavior;
        this.version = 0;
    }

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {SequenceBehavior} value
     */
    serialize(buffer, value) {
        const children = value.children;

        const n = children.length;
        buffer.writeUintVar(n);

        for (let i = 0; i < n; i++) {
            const behavior = children[i];

            this.objectAdapter.serialize(buffer, behavior);
        }
    }

    /**
     *
     * @param {BinaryBuffer} buffer
     * @param {SequenceBehavior} value
     */
    deserialize(buffer, value) {
        const n = buffer.readUintVar();

        value.clearChildren();

        for (let i = 0; i < n; i++) {
            /**
             *
             * @type {Behavior}
             */
            const behavior = this.objectAdapter.deserialize(buffer);

            value.addChild(behavior);
        }
    }
}
