"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

exports.__esModule = true;
exports.GLTFModel = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

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

var _aframe = _interopRequireDefault(require("aframe"));

var _core = require("../core");

var _EventListener = _interopRequireDefault(require("../event/EventListener"));

var _excluded = ["src", "onClick", "onMouseEnter", "onMouseLeave"];
var PREFIX_ASSET = 'model';

/**
 * The glTF model primitive displays a 3D glTF model
 * created from a 3D modeling program or downloaded from the web.
 *
 * @see https://aframe.io/docs/master/primitives/a-gltf-model.html
 *
 * @example
 * ```tsx
 * <Scene>
 *   <Assets>
 *     <AssetItem id="tree" src="tree.gltf" />
 *   </Assets>
 *
 *   // Using the asset management system.
 *   <GLTFModel src="#tree" />
 *
 *   // Defining the URL inline.
 *   // Not recommended but more comfortable for web developers.
 *   <GLTFModel src="tree.gltf" />
 * </Scene>
 * ```
 */
var GLTFModel = function GLTFModel(props) {
  var src = props.src,
      onClick = props.onClick,
      onMouseEnter = props.onMouseEnter,
      onMouseLeave = props.onMouseLeave,
      restProps = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
  var uid = (0, _react.useId)();
  var gltfId = "" + PREFIX_ASSET + uid + "gltf";
  return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/_react.default.createElement(_core.CreateAsset, null, /*#__PURE__*/_react.default.createElement(_core.AssetItem, {
    id: gltfId,
    src: src
  })), /*#__PURE__*/_react.default.createElement(_EventListener.default, (0, _extends2.default)({
    primitive: "a-gltf-model",
    events: {
      click: function click() {
        if (onClick) {
          onClick();
        }
      },
      mouseenter: function mouseenter() {
        if (onClick) {
          var canvas = window.document.querySelector('canvas.a-canvas');
          canvas.style.cursor = 'pointer';
        }

        if (onMouseEnter) {
          onMouseEnter();
        }
      },
      mouseleave: function mouseleave() {
        if (onClick) {
          var canvas = window.document.querySelector('canvas.a-canvas');
          canvas.style.cursor = 'grab';
        }

        if (onMouseLeave) {
          onMouseLeave();
        }
      }
    }
  }, (0, _core.toProps)(restProps), {
    src: "#" + gltfId
  })));
};

exports.GLTFModel = GLTFModel;

_aframe.default.registerComponent('activeAnimation', {
  init: function init() {
    var _this = this;

    // wait for the model to load
    this.el.addEventListener('model-loaded', function () {
      // create new material - with it skinning flag raised
      // @ts-ignore
      var newMaterial = new window.THREE.MeshNormalMaterial({
        skinning: true
      });

      var mesh = _this.el.getObject3D('mesh'); // traverse the mesh and apply the material


      mesh.traverse(function (node) {
        // @ts-ignore
        if (!node.isMesh) return; // @ts-ignore

        node.material = newMaterial; // @ts-ignore

        node.material.needsUpdate = true;
      });
    });
  }
});