"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _context = require("./context");

var _overlay = require("./overlay");

var _scroll = _interopRequireDefault(require("../../utils/dom/scroll"));

var _node = require("../../utils/dom/node");

var _event = require("../../utils/dom/event");

var _touch = _interopRequireDefault(require("../touch"));

var _portal = require("../portal");

var _closeOnPopstate = require("../close-on-popstate");

// Context
var _default = {
  mixins: [_touch.default, _closeOnPopstate.CloseOnPopstateMixin, (0, _portal.PortalMixin)({
    afterPortal: function afterPortal() {
      if (this.overlay) {
        (0, _overlay.updateOverlay)();
      }
    }
  })],
  props: {
    // whether to show popup
    value: Boolean,
    // whether to show overlay
    overlay: Boolean,
    // overlay custom style
    overlayStyle: Object,
    // overlay custom class name
    overlayClass: String,
    // whether to close popup when click overlay
    closeOnClickOverlay: Boolean,
    // z-index
    zIndex: [String, Number],
    // return the mount node for popup
    getContainer: [String, Function],
    // prevent body scroll
    lockScroll: {
      type: Boolean,
      default: true
    },
    // whether to lazy render
    lazyRender: {
      type: Boolean,
      default: true
    }
  },
  data: function data() {
    return {
      inited: this.value
    };
  },
  computed: {
    shouldRender: function shouldRender() {
      return this.inited || !this.lazyRender;
    }
  },
  watch: {
    value: function value(val) {
      var type = val ? "open" : "close";
      this.inited = this.inited || this.value;
      this[type]();
      this.$emit(type);
    },
    overlay: function overlay() {
      this.renderOverlay();
    }
  },
  mounted: function mounted() {
    if (this.value) {
      this.open();
    }
  },
  activated: function activated() {
    /* istanbul ignore next */
    if (this.shouldReopen) {
      this.$emit("input", true);
      this.shouldReopen = false;
    }
  },
  beforeCreate: function beforeCreate() {
    var _this = this;

    var createEmitter = function createEmitter(eventName) {
      return function (event) {
        return _this.$emit(eventName, event);
      };
    };

    this.onClick = createEmitter("click");
    this.onOpened = createEmitter("opened");
    this.onClosed = createEmitter("closed");
  },
  beforeDestroy: function beforeDestroy() {
    (0, _overlay.removeOverlay)(this);

    if (this.opened) {
      this.removeLock();
    }

    if (this.getContainer) {
      (0, _node.removeNode)(this.$el);
    }
  },
  deactivated: function deactivated() {
    /* istanbul ignore next */
    if (this.value) {
      this.close();
      this.shouldReopen = true;
    }
  },
  methods: {
    addLock: function addLock() {
      if (this.lockScroll) {
        (0, _event.on)(document, "touchstart", this.touchStart);
        (0, _event.on)(document, "touchmove", this.onTouchMove);

        if (!_context.context.lockCount) {
          document.body.classList.add("van-overflow-hidden");
        }

        _context.context.lockCount++;
      }
    },
    removeLock: function removeLock() {
      if (this.lockScroll && _context.context.lockCount) {
        _context.context.lockCount--;
        (0, _event.off)(document, "touchstart", this.touchStart);
        (0, _event.off)(document, "touchmove", this.onTouchMove);

        if (!_context.context.lockCount) {
          document.body.classList.remove("van-overflow-hidden");
        }
      }
    },
    open: function open() {
      /* istanbul ignore next */
      if (this.$isServer || this.opened) {
        return;
      } // cover default zIndex


      if (this.zIndex !== undefined) {
        _context.context.zIndex = this.zIndex;
      }

      this.opened = true;
      this.renderOverlay();
      this.addLock();
    },
    close: function close() {
      if (!this.opened) {
        return;
      }

      (0, _overlay.closeOverlay)(this);
      this.opened = false;
      this.removeLock();
      this.$emit("input", false);
    },
    onTouchMove: function onTouchMove(event) {
      this.touchMove(event);
      var direction = this.deltaY > 0 ? "10" : "01";

      var el = _scroll.default.getScrollEventTarget(event.target, this.$el);

      var scrollHeight = el.scrollHeight,
          offsetHeight = el.offsetHeight,
          scrollTop = el.scrollTop;
      var status = "11";
      /* istanbul ignore next */

      if (scrollTop === 0) {
        status = offsetHeight >= scrollHeight ? "00" : "01";
      } else if (scrollTop + offsetHeight >= scrollHeight) {
        status = "10";
      }
      /* istanbul ignore next */


      if (status !== "11" && this.direction === "vertical" && !(parseInt(status, 2) & parseInt(direction, 2))) {
        (0, _event.preventDefault)(event, true);
      }
    },
    renderOverlay: function renderOverlay() {
      var _this2 = this;

      if (this.$isServer || !this.value) {
        return;
      }

      this.$nextTick(function () {
        _this2.updateZIndex(_this2.overlay ? 1 : 0);

        if (_this2.overlay) {
          (0, _overlay.openOverlay)(_this2, {
            zIndex: _context.context.zIndex++,
            duration: _this2.duration,
            className: _this2.overlayClass,
            customStyle: _this2.overlayStyle
          });
        } else {
          (0, _overlay.closeOverlay)(_this2);
        }
      });
    },
    updateZIndex: function updateZIndex(value) {
      if (value === void 0) {
        value = 0;
      }

      this.$el.style.zIndex = ++_context.context.zIndex + value;
    }
  }
};
exports.default = _default;