/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { PREFIX } from '../../../config.js';
import { isServer, on } from '../../../utils/dom.js';
import 'vue';
import 'raf';
import 'lodash/isString';
import '../../../_chunks/dep-70c789e5.js';
import '../../../_chunks/dep-5210251b.js';
import '../../../utils/easing.js';

const POPUP_SELECTOR = `.${PREFIX}-popup`;
const nodeList = /* @__PURE__ */new Map();
let startClick;
let uid = 0;
if (!isServer && window.document) {
  on(document, "mousedown", e => startClick = e);
  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 => {
    var _startClick;
    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 = startClick) === null || _startClick === void 0 ? void 0 : _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
  };
};

export { useClickOutsider as default, useClickOutsider };
//# sourceMappingURL=click-outsider.js.map
