"use strict";

import _ from 'lodash';
import { events } from "../Stores/EventsStore.js";
import ParentNode from "./ParentNode.js";
export default class ComponentNode extends ParentNode {
  componentDidMountOnce = false;
  componentDidAppearPending = false;
  constructor(layout, parentNode) {
    super(layout, 'Component', parentNode);
  }
  componentDidMount() {
    this.componentDidMountOnce = true;
    this.componentDidAppearPending && setImmediate(() => this.componentDidAppear());
  }
  componentDidAppear() {
    if (this.componentDidMountOnce) {
      events.invokeComponentWillAppear({
        componentName: this.data.name,
        componentId: this.nodeId,
        componentType: 'Component'
      });
      events.invokeComponentDidAppear({
        componentName: this.data.name,
        componentId: this.nodeId,
        componentType: 'Component'
      });
      this.buttonsDidAppear(_.concat(this.data.options.topBar?.rightButtons || [], this.data.options.topBar?.leftButtons || []));
      this.titleChanged(undefined, this.data.options.topBar?.title);
    } else {
      this.componentDidAppearPending = true;
    }
  }
  componentDidDisappear() {
    events.invokeComponentDidDisappear({
      componentName: this.data.name,
      componentId: this.nodeId,
      componentType: 'Component'
    });
    this.buttonsDidDisappear(_.concat(this.data.options.topBar?.rightButtons || [], this.data.options.topBar?.leftButtons || []));
    this.titleChanged(this.data.options.topBar?.title);
  }
  titleChanged(oldTitle, newTitle) {
    if (oldTitle && oldTitle.component) {
      events.invokeComponentDidDisappear({
        componentName: oldTitle.component.name,
        componentId: oldTitle.component.componentId,
        componentType: 'TopBarTitle'
      });
    }
    if (newTitle && newTitle.component) {
      events.invokeComponentWillAppear({
        componentName: newTitle.component.name,
        componentId: newTitle.component.componentId,
        componentType: 'TopBarTitle'
      });
      events.invokeComponentDidAppear({
        componentName: newTitle.component.name,
        componentId: newTitle.component.componentId,
        componentType: 'TopBarTitle'
      });
    }
  }
  buttonsChanged(oldButtons, newButtons) {
    this.buttonsDidDisappear(oldButtons);
    this.buttonsDidAppear(newButtons);
  }
  buttonsDidAppear(buttons = []) {
    buttons.forEach(button => {
      if (button.component) {
        events.invokeComponentWillAppear({
          componentName: button.component.name,
          // @ts-ignore
          componentId: button.component.componentId,
          componentType: 'TopBarButton'
        });
        events.invokeComponentDidAppear({
          componentName: button.component.name,
          // @ts-ignore
          componentId: button.component.componentId,
          componentType: 'TopBarButton'
        });
      }
    });
  }
  buttonsDidDisappear(buttons = []) {
    buttons.forEach(button => {
      if (button.component) {
        events.invokeComponentDidDisappear({
          componentName: button.component.name,
          // @ts-ignore
          componentId: button.component.componentId,
          componentType: 'TopBarButton'
        });
      }
    });
  }
  getVisibleLayout() {
    return this;
  }
}
//# sourceMappingURL=ComponentNode.js.map