import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useState, useCallback } from 'react';
import { Time, Validate } from './utils';
import { useInterval } from './hooks';
var DEFAULT_DELAY = 1000;
function getDelayFromExpiryTimestamp(expiryTimestamp) {
  if (!Validate.expiryTimestamp(expiryTimestamp)) {
    return null;
  }
  var seconds = Time.getSecondsFromExpiry(expiryTimestamp);
  var extraMilliSeconds = Math.floor((seconds - Math.floor(seconds)) * 1000);
  return extraMilliSeconds > 0 ? extraMilliSeconds : DEFAULT_DELAY;
}
export default function useTimer() {
  var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    expiry = _ref.expiryTimestamp,
    onExpire = _ref.onExpire,
    _ref$autoStart = _ref.autoStart,
    autoStart = _ref$autoStart === void 0 ? true : _ref$autoStart;
  var _useState = useState(expiry),
    _useState2 = _slicedToArray(_useState, 2),
    expiryTimestamp = _useState2[0],
    setExpiryTimestamp = _useState2[1];
  var _useState3 = useState(Time.getSecondsFromExpiry(expiryTimestamp)),
    _useState4 = _slicedToArray(_useState3, 2),
    seconds = _useState4[0],
    setSeconds = _useState4[1];
  var _useState5 = useState(autoStart),
    _useState6 = _slicedToArray(_useState5, 2),
    isRunning = _useState6[0],
    setIsRunning = _useState6[1];
  var _useState7 = useState(autoStart),
    _useState8 = _slicedToArray(_useState7, 2),
    didStart = _useState8[0],
    setDidStart = _useState8[1];
  var _useState9 = useState(getDelayFromExpiryTimestamp(expiryTimestamp)),
    _useState0 = _slicedToArray(_useState9, 2),
    delay = _useState0[0],
    setDelay = _useState0[1];
  var handleExpire = useCallback(function () {
    Validate.onExpire(onExpire) && onExpire();
    setIsRunning(false);
    setDelay(null);
  }, [onExpire]);
  var pause = useCallback(function () {
    setIsRunning(false);
  }, []);
  var restart = useCallback(function (newExpiryTimestamp) {
    var newAutoStart = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
    setDelay(getDelayFromExpiryTimestamp(newExpiryTimestamp));
    setDidStart(newAutoStart);
    setIsRunning(newAutoStart);
    setExpiryTimestamp(newExpiryTimestamp);
    setSeconds(Time.getSecondsFromExpiry(newExpiryTimestamp));
  }, []);
  var resume = useCallback(function () {
    var time = new Date();
    time.setMilliseconds(time.getMilliseconds() + seconds * 1000);
    restart(time);
  }, [seconds, restart]);
  var start = useCallback(function () {
    if (didStart) {
      setSeconds(Time.getSecondsFromExpiry(expiryTimestamp));
      setIsRunning(true);
    } else {
      resume();
    }
  }, [expiryTimestamp, didStart, resume]);
  useInterval(function () {
    if (delay !== DEFAULT_DELAY) {
      setDelay(DEFAULT_DELAY);
    }
    var secondsValue = Time.getSecondsFromExpiry(expiryTimestamp);
    setSeconds(secondsValue);
    if (secondsValue <= 0) {
      handleExpire();
    }
  }, isRunning ? delay : null);
  return _extends({}, Time.getTimeFromSeconds(seconds), {
    start: start,
    pause: pause,
    resume: resume,
    restart: restart,
    isRunning: isRunning
  });
}