'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var window = require('../window.js');

function dispatchEvent(node, type, params) {
  var window$1 = window.default(node),
      event = window$1.CustomEvent;

  if (typeof event === "function") {
    event = new event(type, params);
  } else {
    event = window$1.document.createEvent("Event");
    if (params) event.initEvent(type, params.bubbles, params.cancelable), event.detail = params.detail;
    else event.initEvent(type, false, false);
  }

  node.dispatchEvent(event);
}

function dispatchConstant(type, params) {
  return function() {
    return dispatchEvent(this, type, params);
  };
}

function dispatchFunction(type, params) {
  return function() {
    return dispatchEvent(this, type, params.apply(this, arguments));
  };
}

function selection_dispatch(type, params) {
  return this.each((typeof params === "function"
      ? dispatchFunction
      : dispatchConstant)(type, params));
}

exports.default = selection_dispatch;
