"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactUnityWebgl = _interopRequireWildcard(require("react-unity-webgl"));

var _reactUse = require("react-use");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var UnityGameWrapper = (0, _react.forwardRef)(function (_ref, ref) {
  var buildURL = _ref.buildURL,
      loaderURL = _ref.loaderURL,
      _ref$onCreateUnityCon = _ref.onCreateUnityContent,
      onCreateUnityContent = _ref$onCreateUnityCon === void 0 ? function () {} : _ref$onCreateUnityCon,
      _ref$width = _ref.width,
      width = _ref$width === void 0 ? '100%' : _ref$width,
      _ref$height = _ref.height,
      height = _ref$height === void 0 ? '500px' : _ref$height;

  // const [ref, { x, y, width, height, top, right, bottom, left, ...rest }] = useMeasure();
  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      loaded = _useState2[0],
      setloaded = _useState2[1];

  var unity = (0, _react.useRef)();
  var unityContent = (0, _react.useMemo)(function () {
    var content = new _reactUnityWebgl.UnityContent(buildURL, loaderURL, {
      adjustOnWindowResize: true
    }); // content.on("MoveEnd", () => {
    //   console.log('move end')
    // })

    onCreateUnityContent(content);
    return content;
  }, [buildURL, loaderURL, unity]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      unityContent: unityContent
    };
  }); // useImperativeHandle(ref,() => ({
  //   complete: () => {
  //     // game && game.scene.getScene('GameScene').complete()
  //   },
  //   setScore: (score) => {
  //     // game && game.scene.getScene('GameScene').setScore(score)
  //   },
  //   addScore: (score) => {
  //     // game && game.scene.getScene('GameScene').addScore(score)
  //   },
  //   player: {
  //     turnLeft,
  //     turnRight,
  //     moveForward
  //   },
  //   run: runCode
  // }))
  // const turnLeft = () => {
  //   // game && game.scene.getScene('GameScene').player.turnLeft()
  //   unityContent.send(
  //     "Player", 
  //     "moveLeft"
  //   );
  // }
  // const turnRight = () => {
  //   // game && game.scene.getScene('GameScene').player.turnRight()
  //   unityContent.send(
  //     "Player", 
  //     "moveRight"
  //   );
  // }
  // const moveForward = () => {
  //   // game && game.scene.getScene('GameScene').player.moveForward()
  //   unityContent.send(
  //     "Player", 
  //     "moveUp"
  //   );
  // }
  // const runCode = async (code) => {
  //   const player = this
  //   const interpreter = new Interpreter(code, function(interpreter, scope) {
  //     interpreter.setProperty(scope, 'moveForward',
  //       interpreter.createAsyncFunction(async (callback) => {
  //         await moveForward()
  //         callback()
  //       })
  //     )
  //     interpreter.setProperty(scope, 'turnLeft',
  //       interpreter.createAsyncFunction(async (callback) => {
  //         await turnLeft()
  //         callback()
  //       })
  //     )
  //     interpreter.setProperty(scope, 'turnRight',
  //       interpreter.createAsyncFunction(async (callback) => {
  //         await turnRight()
  //         callback()
  //       })
  //     )
  //   })
  //   async function run() {
  //     if (interpreter.run()) {
  //       setTimeout(run, 500);
  //     } else {
  //       // player.scene.complete()
  //     }
  //   }
  //   run()
  // }

  return _react["default"].createElement("div", {
    style: {
      width: width,
      height: height,
      position: 'relative'
    }
  }, _react["default"].createElement(_reactUnityWebgl["default"], {
    ref: unity,
    unityContent: unityContent
  }));
});
var _default = UnityGameWrapper;
exports["default"] = _default;