import css from 'dom-helpers/css';
import qsa from 'dom-helpers/querySelectorAll';
import getScrollbarSize from 'dom-helpers/scrollbarSize';
import ModalManager from 'react-overlays/ModalManager';

const Selector = {
  FIXED_CONTENT: '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top',
  STICKY_CONTENT: '.sticky-top',
  NAVBAR_TOGGLER: '.navbar-toggler',
};

export default class BootstrapModalManager extends ModalManager {
  adjustAndStore = (prop, element, adjust) => {
    const actual = element.style[prop];
    element.dataset[prop] = actual;
    css(element, {
      [prop]: `${parseFloat(css(element, prop)) + adjust}px`,
    });
  };

  restore = (prop, element) => {
    const value = element.dataset[prop];
    if (value !== undefined) {
      delete element.dataset[prop];
      css(element, { [prop]: value });
    }
  };

  setContainerStyle(containerState, container) {
    super.setContainerStyle(containerState, container);

    if (!containerState.overflowing) return;
    const size = getScrollbarSize();

    qsa(container, Selector.FIXED_CONTENT).forEach((el) =>
      this.adjustAndStore('paddingRight', el, size),
    );
    qsa(container, Selector.STICKY_CONTENT).forEach((el) =>
      this.adjustAndStore('margingRight', el, -size),
    );
    qsa(container, Selector.NAVBAR_TOGGLER).forEach((el) =>
      this.adjustAndStore('margingRight', el, size),
    );
  }

  removeContainerStyle(containerState, container) {
    super.removeContainerStyle(containerState, container);

    qsa(container, Selector.FIXED_CONTENT).forEach((el) =>
      this.restore('paddingRight', el),
    );
    qsa(container, Selector.STICKY_CONTENT).forEach((el) =>
      this.restore('margingRight', el),
    );
    qsa(container, Selector.NAVBAR_TOGGLER).forEach((el) =>
      this.restore('margingRight', el),
    );
  }
}

@@ -1224,33 +1224,32 @@
 AndStore('margin
-g
 Right', el, -siz
@@ -1344,33 +1344,32 @@
 AndStore('margin
-g
 Right', el, size
@@ -1679,33 +1679,32 @@
 .restore('margin
-g
 Right', el),%0A   
@@ -1793,17 +1793,16 @@
 ('margin
-g
 Right', 

fix typo marging -> margin (#5149)