let queue = [];
let waitFlags = [];

function clearFlag (flag) {
  waitFlags = waitFlags.filter(entry => entry !== flag);
}

function addFocusWaitFlag (flag) {
  clearFlag(flag);
  waitFlags.push(flag);
}

function removeFocusWaitFlag (flag) {
  clearFlag(flag);

  if (waitFlags.length === 0 && queue.length > 0) {
    // only call last focus handler (can't focus multiple things at once)
    queue[ queue.length - 1 ]();
    queue = [];
  }
}

function addFocusFn (fn) {
  if (waitFlags.length === 0) {
    fn();
  }
  else {
    queue.push(fn);
  }
}

function removeFocusFn (fn) {
  queue = queue.filter(entry => entry !== fn);
}

export { addFocusFn, addFocusWaitFlag, removeFocusFn, removeFocusWaitFlag };
