/**
 * Created by geweimin on 17/5/27.
 */

const scrollListener = () => {
  const activeElement = document.activeElement;
  /* eslint-disable */
  if (activeElement.className === 'mx-input mx-cascader-input ' || activeElement.className === 'mx-calendar' || activeElement.className.replace(/\ +/g,'').replace(/[\r\n]/g,'') === `mx-select-selectionmx-select-selection--single`) {
    const selectDropdowns = document.querySelectorAll(".mx-select-dropdown");
    let isClick = false;
    selectDropdowns.forEach(item => {
      if (item.className.indexOf('mx-select-dropdown-hidden') === -1) {
        activeElement.click();
        activeElement.blur();
        isClick = true;
      }
    });
    if (!isClick) {
      const mxCalendar = document.querySelector('.mx-calendar-picker-container');
      if (mxCalendar) {
        mxCalendar.style.display = 'none';
        activeElement.click();
        activeElement.blur();
      }
    }

    // document.querySelector('#root').click();

  }
  return false;
};

export const addEventToBlurForm = ele => {
  ele && ele.addEventListener('scroll', scrollListener, { detailPage: true });
};

export const removeEventToBlurForm = ele => {
  ele && ele.removeEventListener('scroll', scrollListener, { detailPage: true });
};
