"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.GaugePhase = GaugePhase;
require("core-js/modules/web.dom-collections.iterator.js");
var _react = require("react");
var _greenTick = require("../assets/green-tick.svg");
var _stylesModule = _interopRequireDefault(require("./styles.module.css"));
var _CogManager = require("../utils/CogManager");
var _TiltGauge = _interopRequireDefault(require("../TiltGauge"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function GaugePhase(_ref) {
  let {
    nextPhase,
    BlocksConnectedIcon,
    initialBlock,
    blocksGif
  } = _ref;
  const [cogTiltValue, setCogTiltValue] = (0, _react.useState)(0);
  const [tickIconState, setTickIconState] = (0, _react.useState)("hidden");
  const [blocksIconState, setBlocksIconState] = (0, _react.useState)("visible");
  (0, _react.useEffect)(() => {
    const intervalId = setInterval(() => {
      const connectedCog = (0, _CogManager.getConnectedCog)();
      if (connectedCog) {
        const data = connectedCog.raftStateInfo.accelerometer;
        const tiltValue = data.ax;
        if (blocksIconState === "visible") {
          setCogTiltValue(tiltValue);
        }
      }
    }, 200);
    return () => clearInterval(intervalId);
  }, []);
  const onBlocksAnimationEnd = (0, _react.useCallback)(() => {
    setBlocksIconState("hidden");
    setTickIconState("animation");
  }, []);
  const onTickIconAnimationEnd = (0, _react.useCallback)(() => {
    nextPhase();
  }, []);
  const blocksIconContainerClasses = [_stylesModule.default.doubleColumnContainer, blocksIconState === "animation" ? _stylesModule.default.fadeOutAnimation : "", blocksIconState === "hidden" ? _stylesModule.default.hide : ""].join(" ");
  const tickIconClassNames = [_stylesModule.default.tickIconFullScreen, tickIconState === "animation" ? _stylesModule.default.fadeInAnimation : "", tickIconState === "hidden" ? _stylesModule.default.hide : "", tickIconState === "visible" ? _stylesModule.default.show : ""].join(" ");
  let cogTiltValue_ = Math.abs(cogTiltValue);
  if (initialBlock.block === "tiltleft") {
    if (cogTiltValue > 0) {
      cogTiltValue_ = 0;
    }
  } else if (initialBlock.block === "tiltright") {
    if (cogTiltValue < 0) {
      cogTiltValue_ = 0;
    }
  }
  if (cogTiltValue_ > 1) {
    cogTiltValue_ = 1;
  } else if (cogTiltValue_ < 0) {
    cogTiltValue_ = 0;
  }
  if (cogTiltValue_ > 0.95) {
    if (blocksIconState === "visible") {
      setBlocksIconState("animation");
    }
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: _stylesModule.default.singleColumnContainer,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: blocksIconContainerClasses,
      onAnimationEnd: onBlocksAnimationEnd,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _stylesModule.default.blocksGifContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
          src: blocksGif,
          alt: "Blocks Gif",
          className: _stylesModule.default.blocksGif
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _stylesModule.default.gaugeContainer,
        style: {},
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_TiltGauge.default, {
          score: cogTiltValue_,
          title: ""
        })
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_greenTick.ReactComponent, {
      className: tickIconClassNames,
      onAnimationEnd: onTickIconAnimationEnd
    })]
  });
}