/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var core = require('@popperjs/core');
var utils_dom = require('../utils/dom.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var utils_mixins = require('../utils/mixins.js');
var popup_utils = require('./utils.js');
var popup_props = require('./props.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-7c884501.js');
require('dayjs');
require('@vue/composition-api');
require('lodash/camelCase');
require('lodash/kebabCase');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var classPrefixMixins = configProvider_configReceiver.getClassPrefixMixins("popup");
var popperInstance;
var overlayInstance;
var timeout;
var triggerEl;
var triggerType = function triggerType(triggerProps) {
  return popup_utils.triggers.reduce(function (map, trigger) {
    return _objectSpread(_objectSpread({}, map), {}, _defineProperty__default["default"]({}, trigger, triggerProps.includes(trigger)));
  }, {});
};
var Overlay = utils_mixins["default"](classPrefixMixins).extend({
  name: "TPopupOverlay",
  data: function data() {
    return {
      visibleState: false,
      contentClicked: false
    };
  },
  props: _objectSpread(_objectSpread({}, popup_props["default"]), {}, {
    triggerEl: HTMLElement
  }),
  computed: {
    hasTrigger: function hasTrigger() {
      return triggerType(this.trigger);
    },
    overlayClasses: function overlayClasses() {
      var _ref;
      return ["".concat(this.componentName, "__content"), (_ref = {}, _defineProperty__default["default"](_ref, "".concat(this.componentName, "__content--text"), this.content === "string"), _defineProperty__default["default"](_ref, "".concat(this.componentName, "__content--arrow"), this.showArrow), _defineProperty__default["default"](_ref, this.commonStatusClassName.disabled, this.disabled), _ref), this.overlayInnerClassName];
    }
  },
  methods: {
    handleDocumentClick: function handleDocumentClick(e) {
      var _triggerEl,
        _this = this;
      if ((_triggerEl = triggerEl) !== null && _triggerEl !== void 0 && _triggerEl.contains(e.target)) return;
      if (this.contentClicked) {
        setTimeout(function () {
          _this.contentClicked = false;
        });
      } else {
        var _popperInstance;
        if (this.destroyOnClose) this.visibleState = false;
        (_popperInstance = popperInstance) === null || _popperInstance === void 0 ? void 0 : _popperInstance.destroy();
        popperInstance = null;
        triggerEl = null;
      }
    },
    handleMouseLeave: function handleMouseLeave() {
      var _popperInstance2;
      if (this.destroyOnClose) this.visibleState = false;
      (_popperInstance2 = popperInstance) === null || _popperInstance2 === void 0 ? void 0 : _popperInstance2.destroy();
      popperInstance = null;
    },
    handleMouseEnter: function handleMouseEnter() {
      clearTimeout(timeout);
    }
  },
  created: function created() {
    this.visibleState = true;
  },
  mounted: function mounted() {
    var _this2 = this;
    setTimeout(function () {
      utils_dom.on(document, "click", _this2.handleDocumentClick);
    });
  },
  beforeDestroy: function beforeDestroy() {
    utils_dom.off(document, "click", this.handleDocumentClick);
  },
  render: function render(h) {
    var _this3 = this;
    var content = utils_renderTnode.renderTNodeJSX(this, "content");
    var hidePopup = this.hideEmptyPopup && ["", void 0, null].includes(content);
    var handleMouseLeave = this.handleMouseLeave,
      handleMouseEnter = this.handleMouseEnter,
      visibleState = this.visibleState,
      hasTrigger = this.hasTrigger;
    var renderNode = h("div", {
      "class": [this.componentName, this.overlayClassName],
      ref: "popper",
      style: [hidePopup && {
        visibility: "hidden",
        pointerEvents: "none"
      }, {
        zIndex: this.zIndex
      }, this.overlayStyle],
      on: _objectSpread({
        mousedown: function mousedown() {
          _this3.contentClicked = true;
        }
      }, hasTrigger.hover && {
        mouseenter: handleMouseEnter,
        mouseleave: handleMouseLeave
      })
    }, [h("div", {
      ref: "overlay",
      "class": this.overlayClasses,
      style: this.overlayInnerStyle
    }, [content, this.showArrow && h("div", {
      "class": "".concat(this.componentName, "__arrow")
    })])]);
    return visibleState ? h("transition", {
      "slot": "content",
      "attrs": {
        "name": "".concat(this.componentName, "--animation"),
        "appear": true
      }
    }, [renderNode]) : null;
  }
});
var removeOverlayInstance = function removeOverlayInstance() {
  if (overlayInstance) {
    overlayInstance.remove();
    overlayInstance = null;
  }
  if (popperInstance) {
    popperInstance.destroy();
    popperInstance = null;
  }
};
var createPopupPlugin = function createPopupPlugin(trigger, content, popupProps) {
  var _popupProps$delay, _delay$;
  var hasTrigger = triggerType((popupProps === null || popupProps === void 0 ? void 0 : popupProps.trigger) || "hover");
  var currentTriggerEl = utils_dom.getAttach(trigger);
  if (triggerEl && hasTrigger.click) return;
  triggerEl = currentTriggerEl;
  removeOverlayInstance();
  var attach = utils_dom.getAttach((popupProps === null || popupProps === void 0 ? void 0 : popupProps.attach) || "body");
  var delay = [].concat((_popupProps$delay = popupProps === null || popupProps === void 0 ? void 0 : popupProps.delay) !== null && _popupProps$delay !== void 0 ? _popupProps$delay : [250, 150]);
  var closeDelay = (_delay$ = delay[1]) !== null && _delay$ !== void 0 ? _delay$ : delay[0];
  if (attach === document.body) {
    var popupDom = document.createElement("div");
    document.body.appendChild(popupDom);
    attach = popupDom;
  }
  overlayInstance = new Overlay({
    propsData: _objectSpread(_objectSpread({}, popupProps), {}, {
      content: content,
      triggerEl: triggerEl
    })
  }).$mount(attach).$el;
  if (hasTrigger.hover) {
    var mouseoutEvent = function mouseoutEvent() {
      timeout = setTimeout(removeOverlayInstance, closeDelay);
    };
    utils_dom.once(triggerEl, "mouseleave", mouseoutEvent);
  } else if (hasTrigger.focus) {
    var focusoutEvent = function focusoutEvent() {
      timeout = setTimeout(removeOverlayInstance, closeDelay);
    };
    utils_dom.once(triggerEl, "focusout", focusoutEvent);
  }
  popperInstance = core.createPopper(triggerEl, overlayInstance, _objectSpread({
    placement: popup_utils.getPopperPlacement((popupProps === null || popupProps === void 0 ? void 0 : popupProps.placement) || "top")
  }, popupProps === null || popupProps === void 0 ? void 0 : popupProps.popperOptions));
  return popperInstance;
};
var PopupPlugin = createPopupPlugin;
PopupPlugin.install = function () {
  Vue__default["default"].prototype.$popup = createPopupPlugin;
};

exports.PopupPlugin = PopupPlugin;
exports.createPopupPlugin = createPopupPlugin;
exports["default"] = PopupPlugin;
//# sourceMappingURL=plugin.js.map
