/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

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

var config = require('../../../config.js');
var utils_dom = require('../../../utils/dom.js');
require('vue');
require('raf');
require('lodash/isString');
require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-fe796db6.js');
require('../../../utils/easing.js');

const POPUP_SELECTOR = `.${config.PREFIX}-popup`;
const nodeList = /* @__PURE__ */new Map();
let startClick;
let uid = 0;
if (!utils_dom.isServer && window.document) {
  utils_dom.on(document, "mousedown", e => startClick = e);
  utils_dom.on(document, "mouseup", e => {
    for (const {
      handler
    } of nodeList.values()) handler(e);
  });
}
const createDocumentHandler = function (elements, handler) {
  let includePopup = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  return e => {
    if (includePopup) {
      document.querySelectorAll(POPUP_SELECTOR).forEach(ele => {
        elements.push(ele);
      });
    }
    const elementList = Array.from(new Set(elements));
    const mouseUpTarget = e.target;
    const mouseDownTarget = startClick?.target;
    const isTargetUnExists = !mouseUpTarget || !mouseDownTarget;
    if (isTargetUnExists) return;
    const isContained = elementList.some(el => {
      const isSelf = el === mouseUpTarget;
      const isContainedByEl = el.contains(mouseUpTarget) || el.contains(mouseDownTarget);
      return isSelf || isContainedByEl;
    });
    if (isContained) return;
    handler();
  };
};
const useClickOutsider = () => {
  uid += 1;
  const clickOutsiderId = uid;
  const addClickOutsider = (els, handler) => {
    const elements = Array.from(new Set(els.filter(el => el))).map(el => {
      const node = el.el || el.$el || el;
      return node;
    });
    const documentHandler = createDocumentHandler(elements, handler, true);
    nodeList.set(clickOutsiderId, {
      elements,
      handler: documentHandler
    });
  };
  const removeClickOutsider = () => {
    nodeList.has(clickOutsiderId) && nodeList.delete(clickOutsiderId);
  };
  return {
    clickOutsiderId,
    addClickOutsider,
    removeClickOutsider
  };
};

exports["default"] = useClickOutsider;
exports.useClickOutsider = useClickOutsider;
//# sourceMappingURL=click-outsider.js.map
