"use strict";

exports.__esModule = true;
exports.useEventListener = useEventListener;

var _vue = require("vue");

var _onMountedOrActivated = require("../onMountedOrActivated");

var _utils = require("../utils");

var supportsPassive = false;

if (_utils.inBrowser) {
  try {
    var opts = {};
    Object.defineProperty(opts, 'passive', {
      get: function get() {
        supportsPassive = true;
      }
    });
    window.addEventListener('test-passive', null, opts); // eslint-disable-next-line no-empty
  } catch (e) {}
}

function useEventListener(type, listener, options) {
  if (options === void 0) {
    options = {};
  }

  if (!_utils.inBrowser) {
    return;
  }

  var _options = options,
      _options$target = _options.target,
      target = _options$target === void 0 ? window : _options$target,
      _options$passive = _options.passive,
      passive = _options$passive === void 0 ? false : _options$passive,
      _options$capture = _options.capture,
      capture = _options$capture === void 0 ? false : _options$capture;
  var attached;

  var add = function add() {
    var element = (0, _vue.unref)(target);

    if (element && !attached) {
      element.addEventListener(type, listener, supportsPassive ? {
        capture: capture,
        passive: passive
      } : capture);
      attached = true;
    }
  };

  var remove = function remove() {
    var element = (0, _vue.unref)(target);

    if (element && attached) {
      element.removeEventListener(type, listener, capture);
      attached = false;
    }
  };

  (0, _vue.onUnmounted)(remove);
  (0, _vue.onDeactivated)(remove);
  (0, _onMountedOrActivated.onMountedOrActivated)(add);
}