import { EntityNode } from "../../../src/engine/ecs/parent/EntityNode.js";
import { Transform } from "../../../src/engine/ecs/transform/Transform.js";

export class GizmoNode extends EntityNode {
    constructor() {
        super();

        /**
         *
         * @type {boolean}
         * @private
         */
        this._visible = true;
        /**
         *
         * @type {EntityComponentDataset|null}
         * @private
         */
        this.__ecd = null;

        this.entity.add(new Transform());
    }

    build(ecd) {
        this.__ecd = ecd;

        if (this._visible) {
            super.build(ecd);
        }
    }

    set visible(v) {
        if (this._visible === v) {
            return;
        }

        this._visible = v;

        if (this.__ecd !== null) {

            if (v) {
                super.build(this.__ecd);
            } else {
                this.destroy();
            }

        }
    }

    get visible() {
        return this._visible;
    }

    update() {
        // do nothing
        const children = this.children;
        const n = children.length;
        for (let i = 0; i < n; i++) {
            const child = children[i];
            if (!(child instanceof GizmoNode)) {
                continue;
            }

            if (!child.isBuilt) {
                continue;
            }

            child.update();
        }
    }
}