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

import { usePrefixClass } from '../../hooks/useConfig.mjs';
import { isServer, on } from '../../utils/dom.mjs';
import 'vue';
import '../../config-provider/useConfig.mjs';
import '../../_chunks/dep-44417261.mjs';
import '../../_chunks/dep-a0b4a044.mjs';
import '../../_chunks/dep-bf74f1c2.mjs';
import '../../_chunks/dep-79821ff2.mjs';
import '../../_chunks/dep-0bfbf721.mjs';
import '../../_chunks/dep-39cc276d.mjs';
import '../../_chunks/dep-6b22dd27.mjs';
import '../../_chunks/dep-c959a6fb.mjs';
import '../../_chunks/dep-b4041200.mjs';
import '../../_chunks/dep-0f0c89ff.mjs';
import '../../_chunks/dep-8f777748.mjs';
import '../../_chunks/dep-ae357f26.mjs';
import '../../_chunks/dep-352691c2.mjs';
import '../../_chunks/dep-2cfd5b3a.mjs';
import '../../_chunks/dep-05fa17c9.mjs';
import '../../_chunks/dep-c31e1d9e.mjs';
import '../../_chunks/dep-e3b14d70.mjs';
import '../../_chunks/dep-66d6733b.mjs';
import '../../_chunks/dep-a9c8a5d5.mjs';
import '../../_chunks/dep-10085c61.mjs';
import '../../_chunks/dep-1b058de3.mjs';
import '../../_chunks/dep-c852a61a.mjs';
import '../../_chunks/dep-92e7085e.mjs';
import '../../_chunks/dep-44f056cd.mjs';
import '../../_chunks/dep-23a0f453.mjs';
import '../../_chunks/dep-1cf2b129.mjs';
import '../../_chunks/dep-99920870.mjs';
import '../../_chunks/dep-c7e9ce16.mjs';
import '../../_chunks/dep-3afa94cf.mjs';
import '../../_chunks/dep-ba3c0b2e.mjs';
import '../../_chunks/dep-ee57d235.mjs';
import '../../_common/js/global-config/default-config.mjs';
import '../../_common/js/global-config/locale/zh_CN.mjs';
import '../../_chunks/dep-6b4aa943.mjs';
import '../../_chunks/dep-d52291a9.mjs';
import '../../config-provider/type.mjs';
import '../../utils/easing.mjs';

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var nodeList = /* @__PURE__ */new Map();
var startClick;
var uid = 0;
if (!isServer && window.document) {
  on(document, "mousedown", function (e) {
    return startClick = e;
  });
  on(document, "mouseup", function (e) {
    var _iterator = _createForOfIteratorHelper(nodeList.values()),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var handler = _step.value.handler;
        handler(e);
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
  });
}
var createDocumentHandler = function createDocumentHandler(elements, handler) {
  var includePopup = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  var POPUP_SELECTOR = usePrefixClass("popup");
  return function (e) {
    var _startClick;
    if (includePopup) {
      document.querySelectorAll(POPUP_SELECTOR.value).forEach(function (ele) {
        elements.push(ele);
      });
    }
    elements = Array.from(new Set(elements));
    var mouseUpTarget = e.target;
    var mouseDownTarget = (_startClick = startClick) === null || _startClick === void 0 ? void 0 : _startClick.target;
    var isTargetUnExists = !mouseUpTarget || !mouseDownTarget;
    if (isTargetUnExists) return;
    var isContained = elements.some(function (el) {
      var isSelf = el === mouseUpTarget;
      var isContainedByEl = el.contains(mouseUpTarget) || el.contains(mouseDownTarget);
      return isSelf || isContainedByEl;
    });
    if (isContained) return;
    handler();
  };
};
var useClickOutsider = function useClickOutsider() {
  uid++;
  var clickOutsiderId = uid;
  var addClickOutsider = function addClickOutsider(els, handler) {
    var elements = Array.from(new Set(els.filter(function (el) {
      return el;
    }))).map(function (el) {
      var node = el.el || el.$el || el;
      return node;
    });
    var documentHandler = createDocumentHandler(elements, handler, true);
    nodeList.set(clickOutsiderId, {
      elements: elements,
      handler: documentHandler
    });
  };
  var removeClickOutsider = function removeClickOutsider() {
    nodeList.has(clickOutsiderId) && nodeList["delete"](clickOutsiderId);
  };
  return {
    clickOutsiderId: clickOutsiderId,
    addClickOutsider: addClickOutsider,
    removeClickOutsider: removeClickOutsider
  };
};

export { useClickOutsider };
//# sourceMappingURL=click-outsider.mjs.map
