"use strict";

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

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

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

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

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

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

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

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

var _excluded = ["children", "onClick", "onMouseEnter", "onMouseLeave", "obj", "mtl"];
var PREFIX_ASSET = 'model';

/**
 * > We recommend glTF for distributing assets in production over the web.
 * > Check out using the [glTF model primitive](https://aframe.io/docs/1.3.0/primitives/a-gltf-model.html).
 * > You can either instead export to COLLADA and
 * > use [the converter](https://cesiumjs.org/convertmodel.html) or try out the [OBJ converter](https://github.com/AnalyticalGraphicsInc/OBJ2GLTF).
 *
 * The .OBJ model primitive displays a 3D Wavefront model.
 *
 * @see https://aframe.io/docs/1.3.0/primitives/a-obj-model.html
 *
 * @example
 * ```tsx
 * <Scene>
 *   <Assets>
 *     <AssetItem id="crate-obj" src="crate.obj" />
 *     <AssetItem id="crate-mtl" src="crate.mtl" />
 *   </Assets>
 *
 *   // Using the asset management system.
 *   <ObjModel src="#crate-obj" mtl="#crate-mtl" />
 *
 *   // Defining the URL inline. Not recommended but may be more comfortable.
 *   <ObjModel src="crate.obj" mtl="crate.mtl" />
 * </Scene>
 * ```
 */
var ObjModel = function ObjModel(props) {
  var children = props.children,
      onClick = props.onClick,
      onMouseEnter = props.onMouseEnter,
      onMouseLeave = props.onMouseLeave,
      obj = props.obj,
      mtl = props.mtl,
      restProps = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
  var uid = (0, _react.useId)();
  var objId = "" + PREFIX_ASSET + uid + "obj";
  var mtlId = "" + PREFIX_ASSET + uid + "mtl";
  return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/_react.default.createElement(_core.CreateAsset, null, /*#__PURE__*/_react.default.createElement(_core.AssetItem, {
    id: objId,
    src: obj
  }), /*#__PURE__*/_react.default.createElement(_core.AssetItem, {
    id: mtlId,
    src: mtl
  })), /*#__PURE__*/_react.default.createElement(_EventListener.default, (0, _extends2.default)({
    primitive: "a-obj-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, ['mtl', 'obj']), {
    mtl: "#" + mtlId,
    src: "#" + objId
  }), children));
};

exports.ObjModel = ObjModel;