import { remove, addClass, hasCSSAnimation, createElement } from '@openuxkit/utils/dom';
import { ZIndex } from '@openuxkit/utils/zindex';
import { unblockBodyScroll, blockBodyScroll } from 'openvue/utils';
import BaseComponent from '@openvue/core/basecomponent';
import BlockUIStyle from 'openvue/blockui/style';
import { openBlock, createElementBlock, mergeProps, renderSlot } from 'vue';

var script$1 = {
  name: 'BaseBlockUI',
  "extends": BaseComponent,
  props: {
    blocked: {
      type: Boolean,
      "default": false
    },
    fullScreen: {
      type: Boolean,
      "default": false
    },
    baseZIndex: {
      type: Number,
      "default": 0
    },
    autoZIndex: {
      type: Boolean,
      "default": true
    }
  },
  style: BlockUIStyle,
  provide: function provide() {
    return {
      $pcBlockUI: this,
      $parentInstance: this
    };
  }
};

var script = {
  name: 'BlockUI',
  "extends": script$1,
  inheritAttrs: false,
  emits: ['block', 'unblock'],
  mask: null,
  data: function data() {
    return {
      isBlocked: false
    };
  },
  watch: {
    blocked: function blocked(newValue) {
      if (newValue === true) this.block();else this.unblock();
    }
  },
  mounted: function mounted() {
    if (this.blocked) {
      this.block();
    }
  },
  beforeUnmount: function beforeUnmount() {
    if (this.mask) {
      ZIndex.clear(this.mask);
      remove(this.mask);
      if (this.fullScreen) {
        unblockBodyScroll();
      }
      this.mask = null;
    }
  },
  methods: {
    block: function block() {
      if (this.mask) {
        ZIndex.clear(this.mask);
        remove(this.mask);
        this.mask = null;
      }
      var styleClass = 'p-blockui-mask p-overlay-mask p-overlay-mask-enter-active';
      if (this.fullScreen) {
        styleClass += ' p-blockui-mask-document';
        this.mask = createElement('div', {
          style: {
            position: 'fixed',
            top: '0',
            left: '0',
            width: '100%',
            height: '100%'
          },
          "class": !this.isUnstyled && styleClass,
          'p-bind': this.ptm('mask')
        });
        document.body.appendChild(this.mask);
        blockBodyScroll();
        document.activeElement.blur();
      } else {
        this.mask = createElement('div', {
          style: {
            position: 'absolute',
            top: '0',
            left: '0',
            width: '100%',
            height: '100%'
          },
          "class": !this.isUnstyled && styleClass,
          'p-bind': this.ptm('mask')
        });
        this.$refs.container.appendChild(this.mask);
      }
      if (this.autoZIndex) {
        ZIndex.set('modal', this.mask, this.baseZIndex || this.$primevue.config.zIndex.modal);
      }
      this.isBlocked = true;
      this.$emit('block');
    },
    unblock: function unblock() {
      var _this = this;
      var mask = this.mask;
      this.isBlocked = false;
      if (mask) {
        mask.style.pointerEvents = 'none';
        !this.isUnstyled && addClass(mask, 'p-overlay-mask-leave-active');
        var _handleAnimationEnd = function handleAnimationEnd() {
          clearTimeout(fallbackTimer);
          mask.removeEventListener('animationend', _handleAnimationEnd);
          mask.removeEventListener('webkitAnimationEnd', _handleAnimationEnd);
          _this.removeMask(mask);
        };
        var fallbackTimer = setTimeout(function () {
          _this.removeMask(mask);
        }, 300);
        if (hasCSSAnimation(mask) > 0) {
          mask.addEventListener('animationend', _handleAnimationEnd);
          mask.addEventListener('webkitAnimationEnd', _handleAnimationEnd);
        }
      } else {
        this.removeMask();
      }
    },
    removeMask: function removeMask() {
      var mask = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.mask;
      if (mask && mask !== this.mask) return;
      if (mask) {
        ZIndex.clear(mask);
        remove(mask);
        if (this.fullScreen) {
          unblockBodyScroll();
        }
        this.mask = null;
      }
      this.isBlocked = false;
      this.$emit('unblock');
    }
  }
};

var _hoisted_1 = ["aria-busy"];
function render(_ctx, _cache, $props, $setup, $data, $options) {
  return openBlock(), createElementBlock("div", mergeProps({
    ref: "container",
    "class": _ctx.cx('root'),
    "aria-busy": $data.isBlocked
  }, _ctx.ptmi('root')), [renderSlot(_ctx.$slots, "default")], 16, _hoisted_1);
}

script.render = render;

export { script as default };
//# sourceMappingURL=index.mjs.map
