"use strict";

import { LayoutStore } from "../Stores/LayoutStore.js";
import LayoutNodeFactory from "../Layouts/LayoutNodeFactory.js";
import { events } from "../Stores/EventsStore.js";
import _ from 'lodash';
import { CommandName } from "../../interfaces/CommandName.js";
export class NativeCommandsSender {
  constructor() {}
  setRoot(commandId, layout) {
    return new Promise(resolve => {
      if (LayoutStore.getVisibleLayout()) {
        LayoutStore.getVisibleLayout().componentDidDisappear();
        LayoutStore.setRoot({});
      }
      const layoutNode = LayoutNodeFactory.create(layout.root);
      LayoutStore.setRoot(layoutNode);
      layoutNode.getVisibleLayout().componentDidAppear();
      resolve(layout.root.nodeId);
      this.reportCommandCompletion(CommandName.SetRoot, commandId);
    });
  }
  setDefaultOptions(options) {
    LayoutStore.setDefaultOptions(options);
  }
  mergeOptions(componentId, options) {
    LayoutStore.mergeOptions(componentId, options);
  }
  push(commandId, onComponentId, layout) {
    return new Promise(resolve => {
      const stack = LayoutStore.getLayoutById(onComponentId).getStack();
      const layoutNode = LayoutNodeFactory.create(layout, stack);
      stack.getVisibleLayout().componentDidDisappear();
      LayoutStore.push(layoutNode, stack);
      LayoutStore.applyOptions(layoutNode.nodeId, layoutNode.data.options);
      stack.getVisibleLayout().componentDidAppear();
      resolve(stack.getVisibleLayout().nodeId);
      this.reportCommandCompletion(CommandName.Push, commandId);
    });
  }
  pop(commandId, componentId, _options) {
    return new Promise((resolve, reject) => {
      if (!LayoutStore.getLayoutById(componentId)) {
        reject(`Popping component failed - componentId '${componentId}' not found`);
        return;
      }
      const poppedChild = _.last(LayoutStore.getLayoutById(componentId).getStack().children);
      LayoutStore.pop(componentId);
      events.invokeScreenPopped({
        componentId
      });
      resolve(poppedChild.nodeId);
      this.reportCommandCompletion(CommandName.Pop, commandId);
    });
  }
  popTo(commandId, componentId, _options) {
    return new Promise(resolve => {
      LayoutStore.popTo(componentId);
      resolve(componentId);
      this.reportCommandCompletion(CommandName.PopTo, commandId);
    });
  }
  popToRoot(commandId, componentId, _options) {
    LayoutStore.popToRoot(componentId);
    this.reportCommandCompletion(CommandName.PopToRoot, commandId);
  }
  setStackRoot(commandId, onComponentId, layout) {
    LayoutStore.setStackRoot(onComponentId, layout);
    this.reportCommandCompletion(CommandName.SetStackRoot, commandId);
  }
  showModal(commandId, layout) {
    return new Promise(resolve => {
      const layoutNode = LayoutNodeFactory.create(layout);
      LayoutStore.getVisibleLayout().componentDidDisappear();
      LayoutStore.showModal(layoutNode);
      layoutNode.componentDidAppear();
      resolve(layoutNode.nodeId);
      this.reportCommandCompletion(CommandName.ShowModal, commandId);
    });
  }
  dismissModal(commandId, componentId, _options) {
    return new Promise((resolve, reject) => {
      const modal = LayoutStore.getModalById(componentId);
      if (modal) {
        const modalTopParent = modal.getTopParent();
        modalTopParent.componentDidDisappear();
        LayoutStore.dismissModal(componentId);
        events.invokeModalDismissed({
          componentName: modalTopParent.data.name,
          componentId: modalTopParent.nodeId,
          modalsDismissed: 1
        });
        resolve(modalTopParent.nodeId);
        LayoutStore.getVisibleLayout().componentDidAppear();
        this.reportCommandCompletion(CommandName.DismissModal, commandId);
      } else {
        reject(`component with id: ${componentId} is not a modal`);
      }
    });
  }
  dismissAllModals(commandId, _options) {
    LayoutStore.dismissAllModals();
    this.reportCommandCompletion(CommandName.DismissAllModals, commandId);
  }
  showOverlay(commandId, layout) {
    const layoutNode = LayoutNodeFactory.create(layout);
    LayoutStore.showOverlay(layoutNode);
    layoutNode.componentDidAppear();
    this.reportCommandCompletion(CommandName.ShowOverlay, commandId);
  }
  dismissOverlay(commandId, componentId) {
    LayoutStore.dismissOverlay(componentId);
    this.reportCommandCompletion(CommandName.DismissOverlay, commandId);
  }
  dismissAllOverlays(commandId) {
    LayoutStore.dismissAllOverlays();
    this.reportCommandCompletion(CommandName.DismissAllOverlays, commandId);
  }
  getLaunchArgs(commandId) {
    this.reportCommandCompletion(CommandName.GetLaunchArgs, commandId);
  }
  getNavigationConstants() {
    return Promise.resolve({
      topBarHeight: 0,
      backButtonId: 'RNN.back',
      bottomTabsHeight: 0,
      statusBarHeight: 0
    });
  }
  getNavigationConstantsSync() {
    return {
      topBarHeight: 0,
      backButtonId: 'RNN.back',
      bottomTabsHeight: 0,
      statusBarHeight: 0
    };
  }
  reportCommandCompletion(commandName, commandId) {
    events.invokeCommandCompleted({
      commandName,
      commandId,
      completionTime: 0
    });
  }
}
//# sourceMappingURL=NativeCommandsSender.js.map