const store = require('./store');

module.exports = class Entity {

    constructor (name, components = [ ]) {
        this.kind = name;
        this.components = components;

        store.get('entities').set(this, new WeakSet());
    }

    * [ Symbol.iterator ] () {
        let index = 0;

        while (true) {
            const entities = store.get('entities').get(this);
            const next = entites.get(index);

            index++;

            yield next;
        }
    }

    forEach (callback) {
        return store.get('entities').get(this).forEach(callback);
    }

    create (data = { }) {
        const serial = store.get('serial');
        const entity = new Number(serial);

        for (const [ kind, options ] of Object.entries(data)) {
            const component = this.components.find(c => c.kind === kind);

            if (component) {
                component.create(entity, options);
            }
        }

        store.set('serial', serial + 1);
        store.get('entities').get(this).add(entity);

        return entity;
    }

    destroy (entity) {
        return store.get('entities').get(this).delete(entity);
    }

    extend (components) {
        this.components.concat(components);
    }
}

Object.defineProperty(module.exports, '__instances__', {
    get () {
        return store.get('systems').values();
    }
});

@@ -1232,17 +1232,18 @@
 %0A    %7D%0A%7D
+;
 %0A
-
 %0AObject.
@@ -1335,25 +1335,431 @@
 et('
-systems').values(
+entities').values();%0A    %7D%0A%7D);%0A%0AObject.defineProperty(module.exports, 'serialize', %7B%0A    value (entity) %7B%0A        const components = %7B %7D;%0A        %0A        for (const component of store.get('components').keys()) %7B%0A            const data = component.of(entity);%0A            %0A            if (data) %7B%0A                components%5B component.kind %5D = data;%0A            %7D%0A        %7D%0A        %0A        return JSON.stringify(components
 );%0A 

Add ability to serialize entity components to JSON