import _extends from "@babel/runtime/helpers/esm/extends";
import create from "../utils/create";
import { createPopper as _createPopper, offsetModifier } from "@vant/popperjs"; // Mixins

import { ClickOutsideMixin } from "../mixins/click-outside"; // Components

import Icon from "../icon";
import Popup from "../popup";
export default create({
  render: function render() {
    var _vm = this;

    var _h = _vm.$createElement;

    var _c = _vm._self._c || _h;

    return _c('span', {
      ref: "wrapper",
      class: _vm.b('wrapper'),
      on: {
        "click": _vm.onClickWrapper
      }
    }, [_c('Popup', {
      ref: "popover",
      class: _vm.b([_vm.theme]),
      attrs: {
        "value": _vm.value,
        "overlay": _vm.overlay,
        "position": null,
        "transition": "vm-popover-zoom",
        "lockScroll": false,
        "getContainer": _vm.getContainer
      },
      on: {
        "open": _vm.onOpen,
        "close": _vm.onClose,
        "input": _vm.onToggle,
        "opened": _vm.onOpened,
        "closed": _vm.onClosed
      },
      nativeOn: {
        "touchstart": function touchstart($event) {
          return _vm.onTouchstart($event);
        }
      }
    }, [_c('div', {
      class: _vm.b('arrow')
    }), _c('div', {
      class: _vm.b('content'),
      attrs: {
        "role": "menu"
      }
    }, [_vm._t("default", _vm._l(_vm.actions, function (action, index) {
      return _c('div', {
        key: "action-" + index,
        class: [_vm.b('action', {
          disabled: action.disabled,
          'with-icon': action.icon
        }), action.className],
        attrs: {
          "role": "menuitem"
        },
        on: {
          "click": function click($event) {
            return _vm.onClickAction(action, index);
          }
        }
      }, [action.icon ? _c('Icon', {
        class: _vm.b('action-icon'),
        attrs: {
          "name": action.icon
        }
      }) : _vm._e(), _c('div', {
        class: [_vm.b('action-text'), 'vm-hairline-bottom']
      }, [_vm._v("\n            " + _vm._s(action.text) + "\n          ")])], 1);
    }))], 2)]), _vm._t("reference")], 2);
  },
  components: {
    Icon: Icon,
    Popup: Popup
  },
  name: "popover",
  mixins: [ClickOutsideMixin({
    event: "click",
    method: "onClickOutside"
  })],
  props: {
    value: Boolean,
    trigger: String,
    overlay: Boolean,
    offset: {
      type: Array,
      default: function _default() {
        return [0, 8];
      }
    },
    theme: {
      type: String,
      default: "light"
    },
    actions: {
      type: Array,
      default: function _default() {
        return [];
      }
    },
    placement: {
      type: String,
      default: "bottom"
    },
    getContainer: {
      type: [String, Function],
      default: "body"
    },
    closeOnClickAction: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    value: "updateLocation",
    placement: "updateLocation"
  },
  mounted: function mounted() {
    this.updateLocation();
  },
  beforeDestroy: function beforeDestroy() {
    if (this.popper) {
      this.popper.destroy();
      this.popper = null;
    }
  },
  methods: {
    createPopper: function createPopper() {
      return _createPopper(this.$refs.wrapper, this.$refs.popover.$el, {
        placement: this.placement,
        modifiers: [{
          name: "computeStyles",
          options: {
            adaptive: false,
            gpuAcceleration: false
          }
        }, _extends({}, offsetModifier, {
          options: {
            offset: this.offset
          }
        })]
      });
    },
    updateLocation: function updateLocation() {
      var _this = this;

      this.$nextTick(function () {
        if (!_this.value) {
          return;
        }

        if (!_this.popper) {
          _this.popper = _this.createPopper();
        } else {
          _this.popper.setOptions({
            placement: _this.placement
          });
        }
      });
    },
    onToggle: function onToggle(value) {
      this.$emit("input", value);
    },
    onClickWrapper: function onClickWrapper() {
      if (this.trigger === "click") {
        this.onToggle(!this.value);
      }
    },
    onTouchstart: function onTouchstart(event) {
      event.stopPropagation();
      this.$emit("touchstart", event);
    },
    onClickAction: function onClickAction(action, index) {
      if (action.disabled) {
        return;
      }

      this.$emit("select", action, index);

      if (this.closeOnClickAction) {
        this.$emit("input", false);
      }
    },
    onClickOutside: function onClickOutside() {
      this.$emit("input", false);
    },
    onOpen: function onOpen() {
      this.$emit("open");
    },

    /* istanbul ignore next */
    onOpened: function onOpened() {
      this.$emit("opened");
    },
    onClose: function onClose() {
      this.$emit("close");
    },

    /* istanbul ignore next */
    onClosed: function onClosed() {
      this.$emit("closed");
    }
  }
});