/*! bxs-vue-ui v3.3.0 */
import '../utils/entry-virtual.js';
import '../utils/sloppy-array-method.js';
import '../utils/index-of.js';
import 'vue';
import { a as addXSupport, i as isAfterIphoneX } from '../utils/env.js';

var patchBlock = {
  node: null,
  windowHeight: null,
  getWindowHeight: function getWindowHeight() {
    if (!this.windowHeight) {
      this.windowHeight = window.outerHeight;
    }

    return this.windowHeight;
  },
  createAndAppendBody: function createAndAppendBody(height) {
    if (!this.node) {
      var div = document.createElement('div');
      div.style.width = '100%';
      div.style.height = height + "px";
      this.node = div;
      document.body.append(this.node);
    }
  },
  update: function update(height) {
    if (this.node) {
      this.node.style.height = height + "px";
    }
  },
  reset: function reset() {
    if (this.node) {
      this.node.style.height = 0;
    }
  },
  remove: function remove() {
    if (this.node) {
      document.body.removeChild(this.node);
      this.node = null;
    }
  }
};

var getRemainHeight = function getRemainHeight() {
  var windowHeight = patchBlock.getWindowHeight();
  var bodyHeight = document.body.clientHeight;
  return {
    remainHeight: windowHeight - bodyHeight,
    // bodyHeight与windowHeight相等时，也会出现该bug，因此+1强制body超出window
    computedHeight: windowHeight - bodyHeight + 1
  };
};

var SafeAreaPatch = {
  // 自动探测viewport-fit，并添加
  addXSupport: addXSupport,
  // 判断是否是iphoneX之后的机型
  isAfterIphoneX: isAfterIphoneX,
  // 添加安全区域补丁
  create: function create() {
    if (isAfterIphoneX()) {
      addXSupport();

      var _getRemainHeight = getRemainHeight(),
          remainHeight = _getRemainHeight.remainHeight,
          computedHeight = _getRemainHeight.computedHeight;

      if (remainHeight > 0) {
        {
          patchBlock.createAndAppendBody(computedHeight);
        }
      }
    }

    return patchBlock.node;
  },
  // 更新安全区域补丁
  update: function update() {
  },
  // 移除安全区域补丁
  remove: function remove() {
    patchBlock.remove();
  },
  // 供Vue.use使用
  install: function install(vueGlobal) {
    /* eslint no-param-reassign: ["error", { "props": false }] */
    vueGlobal.prototype.$safeAreaPatch = this;
  }
};

export default SafeAreaPatch;
