"use strict";

exports.__esModule = true;
exports.useScannerImpl = useScannerImpl;
exports.default = exports.useScanner = void 0;

var _react = require("react");

var _reactSingletonHook = require("react-singleton-hook");

function useScannerImpl() {
  const [{
    subsonic,
    isLoggedIn
  }, setScannerConfig] = (0, _react.useState)({});
  const [isScanning, setIsScanning] = (0, _react.useState)(false);
  const [scanCount, setScanCount] = (0, _react.useState)(0);
  const [scannerErrorMessage, setScannerErrorMessage] = (0, _react.useState)(null);
  const timerRef = (0, _react.useRef)(null);

  const startScan = () => {
    subsonic._execute('startScan').then(res => {
      // console.log(res);
      if (timerRef.current) {
        // console.log('wiping (startScan)', timerRef.current);
        clearTimeout(timerRef.current);
        timerRef.current = null;
      }

      setScannerErrorMessage(null);
      setIsScanning(true);
    }).catch(err => {
      // console.error(err);
      setScannerErrorMessage(err.error.message);
      setIsScanning(false); // need to wipe the message or else the same string won't redraw if they try it again.

      setTimeout(() => {
        setScannerErrorMessage(null);
      }, 1000);
    });
  };

  (0, _react.useEffect)(() => {
    // console.log('useEffect', isScanning);
    function checkCount() {
      // console.log('wiping (executed)', timerRef.current);
      timerRef.current = null;

      subsonic._execute('getScanStatus').then(res => {
        // console.log('getScanStatus', res);
        const newIsScanning = res.scanStatus.scanning;
        const newScanCount = res.scanStatus.count;
        setIsScanning(newIsScanning);
        setScanCount(newScanCount);

        if (timerRef.current) {
          // console.warn('already running?', timerRef.current);
          return;
        }

        timerRef.current = setTimeout(checkCount, newIsScanning ? 1500 : 20000); // console.log('next timer', timerRef.current, newIsScanning ? 1000 : 5000);
      });
    }

    if (!isLoggedIn) {
      // console.log('isLoggedIn false');
      setIsScanning(false);
      setScanCount(0);
      setScannerErrorMessage(null);

      if (timerRef.current) {
        // console.log('wiping (logged out)', timerRef.current);
        clearTimeout(timerRef.current);
        timerRef.current = null;
      }
    } else {
      // console.log('isLoggedIn true');
      if (!timerRef.current) {
        timerRef.current = setTimeout(checkCount, isScanning ? 1500 : 20000); // console.log('first timer', timerRef.current, isScanning ? 1000 : 5000);
      } else {// console.warn('already running?', timerRef.current);
        }
    }

    return () => {
      // console.log('wiping (unmounted)', timerRef.current);
      if (timerRef.current) clearTimeout(timerRef.current);
      timerRef.current = null;
    };
  }, [isLoggedIn, isScanning, setIsScanning, setScanCount, subsonic]);
  return {
    setScannerConfig,
    startScan,
    isScanning,
    scanCount,
    scannerErrorMessage
  };
}

const useScanner = (0, _reactSingletonHook.singletonHook)({}, useScannerImpl);
exports.useScanner = useScanner;
var _default = useScanner;
exports.default = _default;