import native from './native';

const { MutationObserver } = window;

function microtaskDebounce (cbFunc) {
  let called = false;
  let i = 0;
  let cbArgs = [];
  const elem = document.createElement('span');
  const observer = new MutationObserver(() => {
    cbFunc(...cbArgs);
    called = false;
    cbArgs = null;
  });

  observer.observe(elem, { childList: true });

  return (...args) => {
    cbArgs = args;
    if (!called) {
      called = true;
      elem.textContent = `${i}`;
      i += 1;
    }
  };
}

// We have to use setTimeout() for IE9 and 10 because the Mutation Observer
// polyfill requires that the element be in the document to trigger Mutation
// Events. Mutation Events are also synchronous and thus wouldn't debounce.
//
// The soonest we can set the timeout for in IE is 1 as they have issues when
// setting to 0.
function taskDebounce (cbFunc) {
  let called = false;
  return (...args) => {
    if (!called) {
      called = true;
      setTimeout(() => {
        called = false;
        cbFunc(...args);
      }, 1);
    }
  };
}
export default native(MutationObserver) ? microtaskDebounce : taskDebounce;

@@ -869,35 +869,38 @@
 cbFunc) %7B%0A  let 
-cal
+schedu
 led = false;%0A  r
@@ -896,16 +896,35 @@
  false;%0A
+  let cbArgs = %5B%5D;%0A
   return
@@ -939,32 +939,54 @@
 s) =%3E %7B%0A    
-if (!cal
+cbArgs = args;%0A    if (!schedu
 led) %7B%0A     
@@ -978,35 +978,38 @@
 eduled) %7B%0A      
-cal
+schedu
 led = true;%0A    
@@ -1033,27 +1033,30 @@
 %3E %7B%0A        
-cal
+schedu
 led = false;
@@ -1070,25 +1070,27 @@
   cbFunc(...
-a
+cbA
 rgs);%0A      

fix failing tests for IE9 / IE10