"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Phase4 = Phase4;
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 _PhaserGame = require("../tutorial-game/PhaserGame");
var _EventBus = require("../tutorial-game/EventBus");
var _blockSelector = _interopRequireDefault(require("../block-selector"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function Phase4(_ref) {
  let {
    nextPhase
  } = _ref;
  const [gameContainerDimensions, setGameContainerDimensions] = (0, _react.useState)({
    width: "40vw",
    height: "30vh",
    visibility: "hidden"
  });
  const [tickIconState, setTickIconState] = (0, _react.useState)("hidden");
  const [blocksIconState, setBlocksIconState] = (0, _react.useState)("visible");
  const phaserGameRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    _EventBus.EventBus.on('sky-ready', sky => {
      const skyHeightScaled = sky.height * sky.scaleFactors.height;
      const skyWidthScaled = sky.width * sky.scaleFactors.width;
      setGameContainerDimensions({
        width: skyWidthScaled + "px",
        height: skyHeightScaled + "px",
        visibility: "visible"
      });
    });
    _EventBus.EventBus.on('current-scene-ready', scene => {
      setTimeout(() => {
        var _scene$player, _scene$stars, _scene$stars2;
        scene.physics.world.setBounds(0, 0, 400, scene.scale.height);
        (_scene$player = scene.player) === null || _scene$player === void 0 || _scene$player.setCollideWorldBounds(true);
        scene.spawnStar();
        Phaser.Actions.SetX(((_scene$stars = scene.stars) === null || _scene$stars === void 0 ? void 0 : _scene$stars.getChildren()) || [], 300);
        Phaser.Actions.SetY(((_scene$stars2 = scene.stars) === null || _scene$stars2 === void 0 ? void 0 : _scene$stars2.getChildren()) || [], 180);
      }, 1000);
    });
    _EventBus.EventBus.on('star-collected', () => {
      setTimeout(() => {
        var _phaserGameRef$curren;
        setBlocksIconState("animation");
        (_phaserGameRef$curren = phaserGameRef.current) === null || _phaserGameRef$curren === void 0 || (_phaserGameRef$curren = _phaserGameRef$curren.game) === null || _phaserGameRef$curren === void 0 || _phaserGameRef$curren.destroy(true);
      }, 1500);
    });
    return () => {
      _EventBus.EventBus.removeListener('star-collected');
      _EventBus.EventBus.removeListener('sky-ready');
      _EventBus.EventBus.removeListener('current-scene-ready');
      _EventBus.EventBus.removeListener('player-jumped');
    };
  }, []);
  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(" ");
  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)(_blockSelector.default, {
        gameRef: phaserGameRef
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _stylesModule.default.gameContainer,
        style: gameContainerDimensions,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_PhaserGame.PhaserGame, {
          ref: phaserGameRef
        })
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_greenTick.ReactComponent, {
      className: tickIconClassNames,
      onAnimationEnd: onTickIconAnimationEnd
    })]
  });
}