import {fromJSON as behaviorItemFromJSON} from './registry'; export function Collection(type) { const plural = `${type}s`; return class Collection { static type() { return plural; } constructor() { this[plural] = []; } clone(other) { if (0 === other[plural].length) { return; } const Item = other[plural][0].constructor; for (let i = 0; i < other[plural].length; ++i) { this[plural][i] = new Item(); this[plural][i].clone(other[plural][i]); } } createClone() { const Items = this.constructor; const items = new Items(); items.clone(this); return items; } count() { return this[plural].length; } fromJSON(json) { this[plural] = []; for (const item of json[plural]) { this[plural].push(behaviorItemFromJSON(item)); } return this; } toJSON() { return this[plural].map((item) => item.toJSON()); } }; }