import getRequestAnimationFrame, { cancelRequestAnimationFrame } from './getRequestAnimationFrame';
var oriRaf = getRequestAnimationFrame(); // Support call raf with delay specified frame

export default function raf(callback) {
  var delayFrames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  var restFrames = delayFrames;

  function internalCallback() {
    restFrames -= 1;

    if (restFrames <= 0) {
      callback();
    } else {
      frame.id = oriRaf(internalCallback);
    }
  }

  var frame = {
    id: oriRaf(internalCallback)
  };
  return frame;
}

raf.cancel = function cancel(frame) {
  if (!frame) return;
  cancelRequestAnimationFrame(frame.id);
};