"use strict";

exports.__esModule = true;
exports.parseDelay = parseDelay;
require("core-js/modules/es.error.cause.js");
var _feature = require("./../helpers/feature");
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
/**
 * @class Interval
 */
var _timer = /*#__PURE__*/new WeakMap();
var _func = /*#__PURE__*/new WeakMap();
var _stopped = /*#__PURE__*/new WeakMap();
var _then = /*#__PURE__*/new WeakMap();
var _callback = /*#__PURE__*/new WeakMap();
var _callback2 = /*#__PURE__*/new WeakSet();
class Interval {
  static create(func, delay) {
    return new Interval(func, delay);
  }

  /**
   * Number of milliseconds that function should wait before next call.
   *
   * @type {number}
   */

  constructor(func, delay) {
    /**
     * Loop callback, fired on every animation frame.
     */
    _classPrivateMethodInitSpec(this, _callback2);
    _defineProperty(this, "delay", void 0);
    /**
     * Animation frame request id.
     *
     * @type {number}
     */
    _classPrivateFieldInitSpec(this, _timer, {
      writable: true,
      value: null
    });
    /**
     * Function to invoke repeatedly.
     *
     * @type {Function}
     */
    _classPrivateFieldInitSpec(this, _func, {
      writable: true,
      value: void 0
    });
    /**
     * Flag which indicates if interval object was stopped.
     *
     * @type {boolean}
     * @default true
     */
    _classPrivateFieldInitSpec(this, _stopped, {
      writable: true,
      value: true
    });
    /**
     * Interval time (in milliseconds) of the last callback call.
     *
     * @type {number}
     */
    _classPrivateFieldInitSpec(this, _then, {
      writable: true,
      value: null
    });
    /**
     * Bounded function `func`.
     *
     * @type {Function}
     */
    _classPrivateFieldInitSpec(this, _callback, {
      writable: true,
      value: void 0
    });
    _classPrivateFieldSet(this, _func, func);
    this.delay = parseDelay(delay);
    _classPrivateFieldSet(this, _callback, () => _classPrivateMethodGet(this, _callback2, _callback3).call(this));
  }

  /**
   * Start loop.
   *
   * @returns {Interval}
   */
  start() {
    if (_classPrivateFieldGet(this, _stopped)) {
      _classPrivateFieldSet(this, _then, Date.now());
      _classPrivateFieldSet(this, _stopped, false);
      _classPrivateFieldSet(this, _timer, (0, _feature.requestAnimationFrame)(_classPrivateFieldGet(this, _callback)));
    }
    return this;
  }

  /**
   * Stop looping.
   *
   * @returns {Interval}
   */
  stop() {
    if (!_classPrivateFieldGet(this, _stopped)) {
      _classPrivateFieldSet(this, _stopped, true);
      (0, _feature.cancelAnimationFrame)(_classPrivateFieldGet(this, _timer));
      _classPrivateFieldSet(this, _timer, null);
    }
    return this;
  }
}
function _callback3() {
  _classPrivateFieldSet(this, _timer, (0, _feature.requestAnimationFrame)(_classPrivateFieldGet(this, _callback)));
  if (this.delay) {
    const now = Date.now();
    const elapsed = now - _classPrivateFieldGet(this, _then);
    if (elapsed > this.delay) {
      _classPrivateFieldSet(this, _then, now - elapsed % this.delay);
      _classPrivateFieldGet(this, _func).call(this);
    }
  } else {
    _classPrivateFieldGet(this, _func).call(this);
  }
}
var _default = Interval;
/**
 * Convert delay from string format to milliseconds.
 *
 * @param {number|string} delay The delay in FPS (frame per second) or number format.
 * @returns {number}
 */
exports.default = _default;
function parseDelay(delay) {
  let result = delay;
  if (typeof result === 'string' && /fps$/.test(result)) {
    result = 1000 / parseInt(result.replace('fps', '') || 0, 10);
  }
  return result;
}