text: "use strict";
  
  require("core-js/modules/es6.object.define-property");
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = exports.createDecorator = void 0;
  
  require("core-js/modules/es7.symbol.async-iterator");
  
  require("core-js/modules/es6.symbol");
  
  require("core-js/modules/es6.array.is-array");
  
  require("core-js/modules/es6.array.for-each");
  
  require("core-js/modules/es6.regexp.match");
  
  require("core-js/modules/web.dom.iterable");
  
  require("core-js/modules/es6.array.iterator");
  
  require("core-js/modules/es7.object.entries");
  
  require("core-js/modules/es6.array.map");
  
  var _global = require("global");
  
  var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
  
  var _addons = _interopRequireDefault(require("@storybook/addons"));
  
  var _coreEvents = _interopRequireDefault(require("@storybook/core-events"));
  
  var _actions = _interopRequireDefault(require("./actions"));
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  
  function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  
  var lastSubscription;
  var lastArgs;
  var delegateEventSplitter = /^(\S+)\s*(.*)$/;
  var isIE = _global.Element != null && !_global.Element.prototype.matches;
  var matchesMethod = isIE ? 'msMatchesSelector' : 'matches';
  
  var root = _global.document && _global.document.getElementById('root');
  
  var hasMatchInAncestry = function hasMatchInAncestry(element, selector) {
    if (element[matchesMethod](selector)) {
      return true;
    }
  
    var parent = element.parentElement;
  
    if (!parent) {
      return false;
    }
  
    return hasMatchInAncestry(parent, selector);
  };
  
  var createHandlers = function createHandlers(actionsFn) {
    for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      args[_key - 1] = arguments[_key];
    }
  
    var actionsObject = actionsFn.apply(void 0, args);
    return Object.entries(actionsObject).map(function (_ref) {
      var _ref2 = _slicedToArray(_ref, 2),
          key = _ref2[0],
          action = _ref2[1];
  
      // eslint-disable-next-line no-unused-vars
      var _key$match = key.match(delegateEventSplitter),
          _key$match2 = _slicedToArray(_key$match, 3),
          _ = _key$match2[0],
          eventName = _key$match2[1],
          selector = _key$match2[2];
  
      return {
        eventName: eventName,
        handler: function handler(e) {
          if (!selector || hasMatchInAncestry(e.target, selector)) {
            action(e);
          }
        }
      };
    });
  };
  
  var actionsSubscription = function actionsSubscription() {
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }
  
    if (!(0, _isEqual.default)(args, lastArgs)) {
      lastArgs = args;
      var handlers = createHandlers.apply(void 0, args);
  
      lastSubscription = function lastSubscription() {
        handlers.forEach(function (_ref3) {
          var eventName = _ref3.eventName,
              handler = _ref3.handler;
          return root.addEventListener(eventName, handler);
        });
        return function () {
          return handlers.forEach(function (_ref4) {
            var eventName = _ref4.eventName,
                handler = _ref4.handler;
            return root.removeEventListener(eventName, handler);
          });
        };
      };
    }
  
    return lastSubscription;
  };
  
  var createDecorator = function createDecorator(actionsFn) {
    return function () {
      for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
        args[_key3] = arguments[_key3];
      }
  
      return function (story) {
        if (root != null) {
          _addons.default.getChannel().emit(_coreEvents.default.REGISTER_SUBSCRIPTION, actionsSubscription.apply(void 0, [actionsFn].concat(args)));
        }
  
        return story();
      };
    };
  };
  
  exports.createDecorator = createDecorator;
  
  var _default = createDecorator(_actions.default);
  
  exports.default = _default;