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

import { usePrefixClass } from '../../hooks/useConfig.js';
import { isServer, on } from '../../utils/dom.js';
import 'vue';
import '../../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import 'lodash/isFunction';
import 'lodash/isString';
import '../../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../../_common/js/global-config/default-config.js';
import '../../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../../_chunks/dep-2e5d8948.js';
import 'dayjs';
import '../../_chunks/dep-4580aabe.js';
import '../../config-provider/type.js';
import '../../utils/easing.js';

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.js.map
