import Component from "dlib/ecs/Component.js";

export default class View extends Component {
  constructor(entity, view, {
    onHide = (resolve) => resolve(),
    onShow = (resolve) => resolve()
  } = {}) {
    super(entity);

    this.onShow = onShow;
    this.onHide = onHide;

    this._view = view;

    this._visibilityAnimationPromise = null;

    this._children = new Set();
    this._visible = true;
  }

  set visible(value) {
    this._visible = value;

    if(this._visible) {
      this._view.visible = true;
    }

    let promises = [];

    promises.push(new Promise(this._visible ? this.onShow : this.onHide));

    for (let child of this._children) {
      child.visible = this._visible;
      promises.push(child._visibilityAnimationPromise);
    }

    this._visibilityAnimationPromise = Promise.all(promises).then(() => {
      this._view.visible = this._visible;
      this._visibilityAnimationPromise = null;
    });
  }

  get visible() {
    return this._visible;
  }

  get view() {
    return this._view;
  }

  add(view) {
    this._children.add(view);
    view.visible = this.visible;
    this._view.add(view._view);
  }

  remove(view) {
    this._children.delete(view);
    this._view.remove(view._view);
  }
}

@@ -125,51 +125,26 @@
     
-onHide = (resolve) =%3E resolve(),%0A    onShow
+visibilityExecutor
  = (
@@ -210,49 +210,47 @@
 his.
-onShow = onShow;%0A    this.onHide = onHide
+visibilityExecutor = visibilityExecutor
 ;%0A%0A 
@@ -273,33 +273,32 @@
 view;%0A%0A    this.
-_
 visibilityAnimat
@@ -287,33 +287,24 @@
 s.visibility
-Animation
 Promise = nu
@@ -340,16 +340,17 @@
  Set();%0A
+%0A
     this
@@ -550,44 +550,26 @@
 his.
-_
 visib
-le ? this.onShow : this.onHide
+ilityExecutor
 ));%0A
@@ -672,17 +672,16 @@
 h(child.
-_
 visibili
@@ -678,33 +678,24 @@
 d.visibility
-Animation
 Promise);%0A  
@@ -700,33 +700,32 @@
     %7D%0A%0A    this.
-_
 visibilityAnimat
@@ -714,33 +714,24 @@
 s.visibility
-Animation
 Promise = Pr
@@ -812,25 +812,24 @@
 %0A      this.
-_
 visibilityAn
@@ -830,17 +830,8 @@
 lity
-Animation
 Prom

replace onShow/onHide by visibilityExecutor