"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _Modal = _interopRequireDefault(require("./Modal.FocusTrap"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var listOfWrapperElementsToFloatingUI = [];
var pageElementReference;
var isKeyDownEventListenerConfigured = false;
var modalImpl = {
  init: function init(modalObj) {
    this.modalroot = modalObj.modalrootNode;
    this.floatinguicontainerelement = modalObj.wrapperObjForFloatingUI;
    this.UID = modalObj.UID;
    this.functionReferenceToFocus = modalObj.focusDialogReference ? modalObj.focusDialogReference : false;
    this.functionReferenceToClose = modalObj.closeDialogReference;
    this.isCloseOnEscapeNeeded = modalObj.isCloseOnEscapeNeeded;
    this.isTrapFocusNeeded = modalObj.isTrapFocusNeeded;
    this.pageElementNode = modalObj.pageElementNode;
    this.name = modalObj.name;
  },
  persistFloatingUIInfoInList: function persistFloatingUIInfoInList() {
    listOfWrapperElementsToFloatingUI.push(_defineProperty({}, this.UID, {
      modalroot: this.modalroot,
      floatinguicontainerelement: this.floatinguicontainerelement,
      functionReferenceToFocus: this.functionReferenceToFocus,
      functionReferenceToClose: this.functionReferenceToClose,
      isOverlayNeeded: !!this.freezeLayer
    }));
    console.log(listOfWrapperElementsToFloatingUI);
  },
  bindKeyDownEventToDocument: function bindKeyDownEventToDocument() {
    var _this = this;

    var modalImplInstance = this;

    if (!this.checkStatusOfKeyDownEventBoundToDocument()) {
      document.addEventListener('keydown', function (event) {
        _this.handleKeyPressEvent(event, modalImplInstance);
      }, true);
      this.setKeyDownEventBoundToDocumentStatusToTrue();
    }
  },
  handleKeyPressEvent: function handleKeyPressEvent(event, modalImplInstance) {
    if (event.eventPhase === 3) {
      return false;
    }

    console.log('event.key in modal behaviour', event.key, modalImplInstance);

    if (event.key === 'Escape' && modalImplInstance.isCloseOnEscapeNeeded) {
      var floatingDialogContinerClassName = modalImplInstance.getClassNameOfClosestContainerElement(event.target);
      var indexOfContainerElement = modalImplInstance.getIndexOfContainerElement(floatingDialogContinerClassName);

      if (floatingDialogContinerClassName !== undefined && modalImplInstance.isCloseOnEscapeNeeded) {
        modalImplInstance.invokeCloseCallBackFunctionOfDialogElement(indexOfContainerElement, floatingDialogContinerClassName);
      }
    } else if (event.key === 'Tab') {
      modalImplInstance.isTabKeyPressed = true;

      var _floatingDialogContinerClassName = modalImplInstance.getClassNameOfClosestContainerElement(event.target);

      if (_floatingDialogContinerClassName !== undefined) {
        var _indexOfContainerElement = modalImplInstance.getIndexOfContainerElement(_floatingDialogContinerClassName);

        if (modalImplInstance.isTrapFocusNeeded) {
          (0, _Modal["default"])(event, listOfWrapperElementsToFloatingUI[_indexOfContainerElement][_floatingDialogContinerClassName].floatinguicontainerelement);
        }
      }
    }
  },
  checkStatusOfKeyDownEventBoundToDocument: function checkStatusOfKeyDownEventBoundToDocument() {
    return isKeyDownEventListenerConfigured;
  },
  setKeyDownEventBoundToDocumentStatusToTrue: function setKeyDownEventBoundToDocumentStatusToTrue() {
    isKeyDownEventListenerConfigured = true;
  },
  applyFocusToFloatingDialogElement: function applyFocusToFloatingDialogElement() {
    if (this.functionReferenceToFocus) {
      var indexOfContainerElement = this.getIndexOfContainerElement(this.UID);
      var floatingContainerObj = listOfWrapperElementsToFloatingUI[indexOfContainerElement][this.UID];
      console.log(_typeof(floatingContainerObj.functionReferenceToFocus));
      floatingContainerObj.functionReferenceToFocus();
    }
  },
  getClassNameOfClosestContainerElement: function getClassNameOfClosestContainerElement(elemObj) {
    if (elemObj.closest('div[class^=floatingcontainer_]') !== null) {
      return elemObj.closest('div[class^=floatingcontainer_]').className;
    }
  },
  getClassNameOfContainerElement: function getClassNameOfContainerElement(index) {
    var containerObj = listOfWrapperElementsToFloatingUI[index];
    return Object.keys(containerObj)[0];
  },
  getContainerElement: function getContainerElement(index) {
    var containerObj = listOfWrapperElementsToFloatingUI[index];
    return containerObj[Object.keys(containerObj)[0]];
  },
  getIndexOfContainerElement: function getIndexOfContainerElement(valueToMatch) {
    var that = this;
    return listOfWrapperElementsToFloatingUI.findIndex(function (element) {
      return that.findIndexOfContainerElement(element, valueToMatch);
    });
  },
  findIndexOfContainerElement: function findIndexOfContainerElement(element, floatingDialogContinerClassName) {
    return Object.keys(element)[0] == floatingDialogContinerClassName;
  },
  removeFloatingUIContainerElement: function removeFloatingUIContainerElement(index, refClass) {
    if (Object.keys(listOfWrapperElementsToFloatingUI[index])[0] === refClass) {
      var floatingContainerObj = listOfWrapperElementsToFloatingUI[index][refClass];
      var floatingContainerElem = floatingContainerObj.floatinguicontainerelement;
      var modalRoot = floatingContainerObj.modalroot;
      modalRoot.removeChild(floatingContainerElem);
      listOfWrapperElementsToFloatingUI.splice(index, 1);

      if (listOfWrapperElementsToFloatingUI.length === 0) {
        this.resetModalBoxValues();
      } else {
        var topElem = this.getContainerElement(listOfWrapperElementsToFloatingUI.length - 1);
        topElem.functionReferenceToFocus && topElem.functionReferenceToFocus();
      }
    }
  },
  invokeCloseCallBackFunctionOfDialogElement: function invokeCloseCallBackFunctionOfDialogElement(indexOfDialogElement, refClass) {
    listOfWrapperElementsToFloatingUI[indexOfDialogElement][refClass].functionReferenceToClose();
  },
  getTopMostFloatingUIContainerElement: function getTopMostFloatingUIContainerElement(refClass) {
    var lengthOfUIContainerElement = listOfWrapperElementsToFloatingUI.length;
    return listOfWrapperElementsToFloatingUI[lengthOfUIContainerElement - 1][refClass].floatinguicontainerelement;
  },
  getFloatingUIContainerElement: function getFloatingUIContainerElement(index, refClass) {
    return listOfWrapperElementsToFloatingUI[index][refClass].floatinguicontainerelement;
  },
  persistPageElementReference: function persistPageElementReference() {
    if (typeof this.pageElementNode !== 'undefined') {
      pageElementReference = this.pageElementNode;
    }
  },
  applyFocusToPageElementReference: function applyFocusToPageElementReference() {
    pageElementReference.current.focus();
  },
  resetPageElementReference: function resetPageElementReference() {
    pageElementReference = null;
  },
  resetisEventListenerAddedProperty: function resetisEventListenerAddedProperty() {
    isKeyDownEventListenerConfigured = false;
  },
  unbindKeyDownEventListener: function unbindKeyDownEventListener() {
    document.removeEventListener('keydown', this.handleKeyPressEvent);
  },
  resetWrapperElementsList: function resetWrapperElementsList() {
    console.log(this);
    listOfWrapperElementsToFloatingUI = [];
  },
  resetModalBoxValues: function resetModalBoxValues() {
    this.resetWrapperElementsList();
    this.applyFocusToPageElementReference();
    this.resetPageElementReference();
    this.resetisEventListenerAddedProperty();
    this.unbindKeyDownEventListener();
  }
};
var _default = modalImpl;
exports["default"] = _default;