import { assert } from "../../../core/assert.js";

export class ChildEntities {
    constructor() {
        /**
         * Directly attached children, must have to relevant {@link ParentEntity} components in children as well
         * @readonly
         * @type {number[]}
         */
        this.entities = [];
    }

    /**
     *
     * @param {number[]} elements
     * @returns {ChildEntities}
     */
    static from(elements) {
        assert.isArrayLike(elements, 'elements');

        const r = new ChildEntities();

        Array.prototype.push.apply(r.entities, elements);

        return r;
    }

    toJSON() {
        return {
            entities: this.entities
        };
    }

    /**
     *
     * @param {number[]} entities
     */
    fromJSON({ entities = [] }) {
        this.entities = entities;
    }
}
