"use strict";

exports.__esModule = true;
exports.default = void 0;

require("../_styles/mask.css");

var _dom = require("./dom");

var _default = {
  zIndex: 2000,

  getZindex() {
    this.zIndex += 1;
    return this.zIndex;
  },

  stack: [],
  mask: null,

  pushStack(vm) {
    if (this.stack.indexOf(vm) > -1) {
      return;
    }

    var lastVm = this.stack[this.stack.length - 1];
    var maxZIndex = this.zIndex;

    if (lastVm) {
      maxZIndex = lastVm.zIndex + 2;
    }

    this.stack.push(vm);
    vm.zIndex = maxZIndex;

    if (vm.appendBody) {
      document.body.appendChild(vm.$el);
      vm.$once('destroy', function () {
        if (vm.$el.parentElement) document.body.removeChild(vm.$el);
      });
    }

    if (!this.mask && vm.mask) {
      var mask = document.createElement('div');
      this.mask = mask;
      clearTimeout(this.timmer);
      (0, _dom.addClass)(mask, 'gd-mask gd-fade-enter gd-fade-enter-active');

      if (vm.appendBody) {
        document.body.appendChild(mask);
        mask.style.position = 'fixed';
        document.documentElement.style.paddingRight = `${(0, _dom.getScrollBarWidth)()}px`;
        document.documentElement.style.overflow = 'hidden';
      } else {
        vm.$el.parentNode.appendChild(mask);
        mask.style.position = 'absolute';
      }

      (0, _dom.once)(mask, 'transitionend', function () {
        (0, _dom.removeClass)(mask, 'gd-fade-enter-active');
      }); // 预防动画执行失败，导致遮罩一直存留

      this.timmer = setTimeout(function () {
        return (0, _dom.removeClass)(mask, 'gd-fade-enter-active');
      }, 300);
      requestAnimationFrame(function () {
        (0, _dom.removeClass)(mask, 'gd-fade-enter');
      });
    }

    if (this.mask && vm.mask) {
      this.mask.style.zIndex = maxZIndex - 1;
    }

    if (this.stack.length === 1) {
      this.handleEsc = this.handleEsc.bind(this);
      (0, _dom.on)(document, 'keydown', this.handleEsc);
    }
  },

  popStack(vm) {
    if (!vm) {
      vm = this.stack[this.stack.length - 1];
    }

    if (!vm) return;
    if (this.stack.indexOf(vm) === -1) return;
    this.stack = this.stack.filter(function (v) {
      return v !== vm;
    });

    if (this.stack.filter(function (vm) {
      return vm.mask;
    }).length === 0) {
      document.documentElement.style.paddingRight = '';
      document.documentElement.style.overflow = '';
    }

    if (this.mask) {
      var reverseStack = [].concat(this.stack).reverse();
      var index = reverseStack.findIndex(function (vm) {
        return vm.mask;
      });

      if (index > -1) {
        var _vm = reverseStack[index];
        this.mask.style.zIndex = _vm.zIndex - 1;
      } else {
        var mask = this.mask;
        clearTimeout(this.timmer);
        (0, _dom.addClass)(mask, 'gd-mask gd-fade-leave-active');
        (0, _dom.once)(mask, 'transitionend', function () {
          return mask.parentNode && mask.parentNode.removeChild(mask);
        }); // 预防动画执行失败，导致遮罩一直存留

        setTimeout(function () {
          return mask.parentNode && mask.parentNode.removeChild(mask);
        }, 300);
        window.requestAnimationFrame(function () {
          return (0, _dom.addClass)(mask, 'gd-fade-leave-to');
        });
        this.mask = null;
      }
    }

    if (this.stack.length === 0) {
      (0, _dom.off)(document, 'keydown', this.handleEsc);
    }
  },

  handleEsc(e) {
    if (e.keyCode === 27 && this.stack.length) {
      var vm = this.stack[this.stack.length - 1];

      if (vm.keyborad && vm.cancel) {
        vm.cancel();
      }
    }
  }

};
exports.default = _default;