'use strict';

function PerspectiveManager(element, controller, notificationService) {
  var smallMenu = element.querySelectorAll ? element.querySelectorAll(".trv-menu-small")[0] : false;
  var perspectives = {
    "small": {
      documentMapVisible: false,
      parametersAreaVisible: false,
      onDocumentMapVisibleChanged: (e, args) => {
        if (args.visible) {
          notificationService.setParametersAreaVisible({ visible: false });
        }
      },
      onParameterAreaVisibleChanged: (e, args) => {
        if (args.visible) {
          notificationService.setDocumentMapVisible({ visible: false });
        }
      },
      onBeforeLoadReport: () => {
        notificationService.setParametersAreaVisible({ visible: false });
        notificationService.setDocumentMapVisible({ visible: false });
      },
      onNavigateToPage: () => {
        notificationService.setParametersAreaVisible({ visible: false });
        notificationService.setDocumentMapVisible({ visible: false });
      }
    },
    "large": {
      documentMapVisible: true,
      parametersAreaVisible: true
    }
  };
  var currentPerspective;
  function init() {
    currentPerspective = getPerspective();
    initStateFromController(perspectives["large"]);
  }
  function setPerspective(beforeApplyState) {
    var perspective = getPerspective();
    if (perspective !== currentPerspective) {
      var oldState = perspectives[currentPerspective];
      var newState = perspectives[perspective];
      currentPerspective = perspective;
      if (beforeApplyState) {
        beforeApplyState.call(void 0, oldState, newState);
      }
      applyState(newState);
    }
  }
  function onDocumentMapVisibleChanged(e, args) {
    dispatch("onDocumentMapVisibleChanged", arguments);
  }
  function onParameterAreaVisibleChanged(e, args) {
    dispatch("onParameterAreaVisibleChanged", arguments);
  }
  function onBeforeLoadReport() {
    dispatch("onBeforeLoadReport", arguments);
  }
  function onNavigateToPage() {
    dispatch("onNavigateToPage", arguments);
  }
  function onReportLoadComplete() {
    dispatch("onReportLoadComplete", arguments);
  }
  function onWindowResize() {
    setPerspective((oldState, newState) => {
      initStateFromController(oldState);
    });
  }
  function onCssLoaded() {
    setPerspective(null);
  }
  function dispatch(func, args) {
    var activePerspective = perspectives[currentPerspective];
    var handler = activePerspective[func];
    if (typeof handler === "function") {
      handler.apply(activePerspective, args);
    }
  }
  function attach() {
    window.addEventListener("resize", onWindowResize);
    controller.onAsync("beforeLoadReport", async () => onBeforeLoadReport()).on("navigateToPage", onNavigateToPage).onAsync("reportLoadComplete", async () => onReportLoadComplete());
    notificationService.setDocumentMapVisible(onDocumentMapVisibleChanged);
    notificationService.setParametersAreaVisible(onParameterAreaVisibleChanged);
    notificationService.cssLoaded(onCssLoaded);
  }
  function dispose() {
    window.removeEventListener("resize", onWindowResize);
  }
  function getPerspective() {
    var windowWidthInEm = $(window).width() / parseFloat($("body").css("font-size"));
    var windowMinWidth = 40.5;
    return smallMenu && windowWidthInEm <= windowMinWidth ? "small" : "large";
  }
  function initStateFromController(state) {
    state.documentMapVisible = documentMapVisible();
    state.parametersAreaVisible = parametersAreaVisible();
  }
  function applyState(state) {
    documentMapVisible(state.documentMapVisible);
    parametersAreaVisible(state.parametersAreaVisible);
  }
  function documentMapVisible() {
    if (arguments.length === 0) {
      var args1 = {};
      notificationService.getDocumentMapState(args1);
      return args1.visible;
    }
    notificationService.setDocumentMapVisible({
      visible: Boolean(arguments[0])
    });
    return this;
  }
  function parametersAreaVisible() {
    if (arguments.length === 0) {
      var args1 = {};
      notificationService.getParametersAreaState(args1);
      return args1.visible;
    }
    notificationService.setParametersAreaVisible({
      visible: Boolean(arguments[0])
    });
    return this;
  }
  init();
  return {
    attach,
    dispose
  };
}

exports.PerspectiveManager = PerspectiveManager;
