"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 _core = require("../core");

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

var _cursorEvent = require("../utils/cursorEvent");

var _excluded = ["src", "onClick", "onMouseEnter", "onMouseLeave", "animationMixerClip", "animationMixerDuration", "animationMixerClampWhenFinished", "animationMixerCrossFadeDuration", "animationMixerLoop", "animationMixerRepetitions", "animationMixerTimeScale", "animationMixerStartFrame"];
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,
      animationMixerClip = props.animationMixerClip,
      animationMixerDuration = props.animationMixerDuration,
      animationMixerClampWhenFinished = props.animationMixerClampWhenFinished,
      animationMixerCrossFadeDuration = props.animationMixerCrossFadeDuration,
      animationMixerLoop = props.animationMixerLoop,
      animationMixerRepetitions = props.animationMixerRepetitions,
      animationMixerTimeScale = props.animationMixerTimeScale,
      animationMixerStartFrame = props.animationMixerStartFrame,
      restProps = (0, _objectWithoutPropertiesLoose2.default)(props, _excluded);
  var uid = (0, _react.useId)();
  var gltfId = "" + PREFIX_ASSET + uid + "gltf";
  var toAnimationMixer = (0, _react.useCallback)(function () {
    var compose = (0, _extends2.default)({}, animationMixerClip && {
      clip: animationMixerClip
    }, animationMixerDuration && {
      duration: animationMixerDuration
    }, animationMixerClampWhenFinished && {
      clampWhenFinished: animationMixerClampWhenFinished
    }, animationMixerCrossFadeDuration && {
      crossFadeDuration: animationMixerCrossFadeDuration
    }, animationMixerLoop && {
      loop: animationMixerLoop
    }, animationMixerRepetitions && {
      repetitions: animationMixerRepetitions
    }, animationMixerTimeScale && {
      timeScale: animationMixerTimeScale
    }, animationMixerStartFrame && {
      startFrame: animationMixerStartFrame
    });

    if (Object.keys(compose).length === 0) {
      return false;
    }

    return Object.entries(compose).map(function (_ref) {
      var key = _ref[0],
          value = _ref[1];
      return key + ": " + value.toString();
    }).join(';');
  }, [animationMixerClip, animationMixerDuration, animationMixerClampWhenFinished, animationMixerCrossFadeDuration, animationMixerLoop, animationMixerRepetitions, animationMixerTimeScale, animationMixerStartFrame]);
  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(e) {
        e.stopPropagation();

        if (onClick) {
          onClick();
        }
      },
      mouseenter: function mouseenter() {
        if (onClick) {
          (0, _cursorEvent.onEventMouseEnter)();
        }

        if (onMouseEnter) {
          onMouseEnter();
        }
      },
      mouseleave: function mouseleave() {
        if (onClick) {
          (0, _cursorEvent.onEventMouseLeave)();
        }

        if (onMouseLeave) {
          onMouseLeave();
        }
      }
    }
  }, (0, _core.toProps)(restProps), toAnimationMixer() && {
    'animation-mixer': toAnimationMixer()
  }, {
    src: "#" + gltfId
  })));
};

exports.GLTFModel = GLTFModel;