import { _ as _applyDecoratedDescriptor, b as _defineProperty } from '../_rollupPluginBabelHelpers-9wjJaosZ.js';
import { registerDestructor } from '@ember/destroyable';
import { action } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import Modifier from 'ember-modifier';

var _class;
let OutsideClickDetector = (_class = class OutsideClickDetector extends Modifier {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "id", void 0);
    _defineProperty(this, "capturedDownIds", []);
    _defineProperty(this, "element", null);
    _defineProperty(this, "named", null);
    _defineProperty(this, "positional", null);
  }
  onClickOutside(e) {
    const {
      isDisabled,
      onOutsideClick
    } = this.named;
    if (isDisabled) {
      this.capturedDownIds = [];
      return;
    }
    const event = e;
    if (event.euiGeneratedBy && event.euiGeneratedBy.includes(this.id) || this.capturedDownIds.includes(this.id)) {
      this.capturedDownIds = [];
      return;
    }
    this.capturedDownIds = [];
    return onOutsideClick(event);
  }
  modify(element, positional, named) {
    this.element = element;
    this.named = named;
    this.positional = positional;
    this._setup();
    registerDestructor(this, () => this._teardown());
  }
  _setup() {
    this.id = guidFor({});
    document.addEventListener('mouseup', this.onClickOutside);
    document.addEventListener('touchend', this.onClickOutside);
    this.addElementEvents();
  }
  _teardown() {
    document.removeEventListener('mouseup', this.onClickOutside);
    document.removeEventListener('touchend', this.onClickOutside);
    this.removeElementEvents();
    this.element = null;
    this.named = null;
    this.positional = null;
  }
  addElementEvents() {
    if (this.element) {
      this.element.addEventListener('mousedown', this.onChildMouseDown);
      this.element.addEventListener('touchstart', this.onChildMouseDown);
      this.element.addEventListener('mouseup', this.onChildMouseUp);
      this.element.addEventListener('touchend', this.onChildMouseUp);
    }
  }
  removeElementEvents() {
    if (this.element) {
      this.element.removeEventListener('mousedown', this.onChildMouseDown);
      this.element.removeEventListener('touchstart', this.onChildMouseDown);
      this.element.removeEventListener('mouseup', this.onChildMouseUp);
      this.element.removeEventListener('touchend', this.onChildMouseUp);
    }
  }
  onChildClick(event, cb) {
    // to support nested click detectors, build an array
    // of detector ids that have been encountered;

    if (event.hasOwnProperty('euiGeneratedBy')) {
      event.euiGeneratedBy.push(this.id);
    } else {
      event.euiGeneratedBy = [this.id];
    }
    if (cb) cb(event);
  }
  onChildMouseDown(event) {
    this.onChildClick(event, e => {
      const nativeEvent = e;
      this.capturedDownIds = nativeEvent.euiGeneratedBy;
      this.named?.onMouseDown?.(e);
      this.named?.onTouchStart?.(e);
    });
  }
  onChildMouseUp(event) {
    this.onChildClick(event, e => {
      this.named?.onMouseUp?.(e);
      this.named?.onTouchEnd?.(e);
    });
  }
}, (_applyDecoratedDescriptor(_class.prototype, "onClickOutside", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onClickOutside"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onChildClick", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onChildClick"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onChildMouseDown", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onChildMouseDown"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "onChildMouseUp", [action], Object.getOwnPropertyDescriptor(_class.prototype, "onChildMouseUp"), _class.prototype)), _class);

export { OutsideClickDetector as default };
//# sourceMappingURL=outside-click-detector.js.map
