"use strict";

require("core-js/modules/es.object.assign.js");

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

require("core-js/modules/web.dom-collections.iterator.js");

require("core-js/modules/es.string.includes.js");

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

var THREE = _interopRequireWildcard(require("three"));

var _fiber = require("@react-three/fiber");

var _drei = require("@react-three/drei");

var _GLTFLoader = require("three/examples/jsm/loaders/GLTFLoader");

var _config = _interopRequireDefault(require("../config"));

var _threeStdlib = require("three-stdlib");

var _Eye = _interopRequireDefault(require("./body/Eye"));

var _Glasses = _interopRequireDefault(require("./body/Glasses"));

var _Hair = _interopRequireDefault(require("./body/Hair"));

var _Hat = _interopRequireDefault(require("./body/Hat"));

var _Pants = _interopRequireDefault(require("./body/Pants"));

var _Shirt = _interopRequireDefault(require("./body/Shirt"));

var _Shoe = _interopRequireDefault(require("./body/Shoe"));

var _Skin = _interopRequireDefault(require("./body/Skin"));

var _Head = _interopRequireDefault(require("./body/Head"));

var _Eyelash = _interopRequireDefault(require("./body/Eyelash"));

var _Eyebrow = _interopRequireDefault(require("./body/Eyebrow"));

const _excluded = ["modelConfig", "rotate", "autoRotate", "touchControl"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

let animationss_ = [];
const MINUTE_MS = 5000;
const {
  characters
} = _config.default;

const Player = _ref => {
  var _modelConfig$Eye, _modelConfig$Hair, _modelConfig$Pants, _modelConfig$Shirt, _modelConfig$Shoe, _modelConfig$Skin, _modelConfig$Skin2, _modelConfig$Skin3, _modelConfig$Skin4, _modelConfig$Glasses, _modelConfig$Hat;

  let {
    modelConfig,
    rotate,
    autoRotate = true,
    touchControl
  } = _ref,
      props = _objectWithoutProperties(_ref, _excluded);

  const {
    camera,
    size,
    viewport
  } = (0, _fiber.useThree)();
  const model = (0, _react.useRef)();
  const light = (0, _react.useRef)();
  const [mixer] = (0, _react.useState)(() => new THREE.AnimationMixer());
  const [Animation, setAnimation] = (0, _react.useState)([]);
  const modelPlayer = (0, _drei.useGLTF)("".concat(characters.path, "/main/").concat(characters.lod0.base));
  const clone = (0, _react.useMemo)(() => _threeStdlib.SkeletonUtils.clone(modelPlayer.scene), [modelPlayer.scene]);
  const {
    nodes,
    materials,
    scenes
  } = (0, _fiber.useGraph)(clone);
  const [touch, settouch] = (0, _react.useState)(0);
  const [Autorotate, setAutorotate] = (0, _react.useState)(autoRotate);
  const [aName, setaName] = (0, _react.useState)('');
  const LOD = characters.lod0.Lod;
  const act = (0, _react.useMemo)(() => {
    let obj = [];
    characters.animations.map(i => {
      obj.push("".concat(characters.path, "/actions/").concat(i.fileName));
    });
    return obj;
  });
  const obj = (0, _drei.useGLTF)(act);

  const _onTouchMove = e => {
    var x = e.touches[0].clientX;
    settouch(x / 10);
    setAutorotate(false);
  };

  const _mousemove = e => {
    var x = e.x;
    settouch(x / 10);
    setAutorotate(false);
  };

  const _mouseDown = e => {
    document.querySelector('canvas').onmousemove = function (e) {
      _mousemove(e);
    };
  };

  const _mouseUp = e => {
    document.querySelector('canvas').onmousemove = null;
    autoRotate && setAutorotate(true);
  };

  const _onTouchEnd = e => {
    autoRotate && setAutorotate(true);
  };

  (0, _react.useEffect)(() => {
    if (touchControl) {
      document.querySelector('canvas').addEventListener('touchmove', e => _onTouchMove(e));
      document.querySelector('canvas').addEventListener('touchend', e => _onTouchEnd(e));
      document.querySelector('canvas').addEventListener('mousedown', e => _mouseDown(e));
      document.querySelector('canvas').addEventListener('mouseup', e => _mouseUp(e));
    }

    modelPlayer.scene.traverse(c => {
      c.castShadow = true;
      c.receiveShadow = false;

      if (c.material && c.material.map) {
        c.material.metalness = 0;
        c.material.roughness = 0.5;
      }
    });
  }, []);

  const _OnLoad = (animName, anim) => {
    const clip = anim.animations[0];
    const action = mixer.clipAction(clip, model.current);
    setAnimation(pre => {
      return _objectSpread(_objectSpread({}, pre), {}, {
        ["".concat(animName)]: {
          clip: clip,
          action: action
        }
      });
    });

    if (animName == 'idle') {
      setaName('idle');
    }
  };

  (0, _react.useEffect)(() => {
    if (act && obj) {
      characters.animations.forEach((animationData, index) => {
        _OnLoad(animationData.name, obj[index]);
      });
    }
  }, [obj]);
  (0, _fiber.useFrame)((state, delta) => {
    (0, _fiber.invalidate)();
    mixer === null || mixer === void 0 ? void 0 : mixer.update(delta);

    if (Autorotate) {
      model.current.rotation.y -= 0.005;
    } else if (rotate) {
      model.current.rotation.y = touch;
    }
  });
  (0, _react.useEffect)(() => {
    nodes.Armature.visible = false;
  }, [nodes]); // useEffect(() => {
  //   let interval
  //   if (Object.keys(Animation).length > 0) {
  //     interval = setInterval(() => {
  //       console.log('Logs every minute');
  //       const arr = Object.keys(Animation).filter(i=>  i.includes('Gestures'))
  //       arr.push('idle')
  //       const anim = arr[Math.floor(Math.random() *  arr.length)]
  //       setaName(anim)
  //     }, MINUTE_MS);
  //   }
  //   return () => clearInterval(interval); // This represents the unmount function, in which you need to clear your interval to prevent memory leaks.
  // }, [Animation])

  mixer.addEventListener('loop', function (e) {
    const arr = Object.keys(Animation).filter(i => i.includes('Gestures'));
    arr.push('idle');
    const anim = arr[Math.floor(Math.random() * arr.length)];
    setaName(anim);
  });
  (0, _react.useEffect)(() => {
    if (Animation && Object.keys(Animation).length > 0) {
      var _Animation$aName;

      (_Animation$aName = Animation[aName]) === null || _Animation$aName === void 0 ? void 0 : _Animation$aName.action.reset().fadeIn(0.5).play();
    }

    return () => {
      var _Animation$aName2, _Animation$aName2$act;

      return (_Animation$aName2 = Animation[aName]) === null || _Animation$aName2 === void 0 ? void 0 : (_Animation$aName2$act = _Animation$aName2.action) === null || _Animation$aName2$act === void 0 ? void 0 : _Animation$aName2$act.fadeOut(0.5);
    };
  }, [aName]);
  return /*#__PURE__*/_react.default.createElement("group", _extends({
    ref: model
  }, props, {
    dispose: null
  }), /*#__PURE__*/_react.default.createElement("mesh", {
    scale: 1,
    position: [0, 0.7, 0],
    onClick: () => {
      if (!props.gender) return;
      props.select(props.gender);
    }
  }, /*#__PURE__*/_react.default.createElement("boxGeometry", {
    args: [0.5, 1.5, 0.5]
  }), /*#__PURE__*/_react.default.createElement("shadowMaterial", {
    transparent: true,
    depthTest: false,
    depthWrite: false
  })), /*#__PURE__*/_react.default.createElement("group", {
    name: "Player",
    rotation: [0, 0, 0],
    dispose: null,
    scale: 1
  }, (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Eye = modelConfig.Eye) === null || _modelConfig$Eye === void 0 ? void 0 : _modelConfig$Eye.name) && /*#__PURE__*/_react.default.createElement(_Eye.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Hair = modelConfig.Hair) === null || _modelConfig$Hair === void 0 ? void 0 : _modelConfig$Hair.name) && /*#__PURE__*/_react.default.createElement(_Hair.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Pants = modelConfig.Pants) === null || _modelConfig$Pants === void 0 ? void 0 : _modelConfig$Pants.name) && /*#__PURE__*/_react.default.createElement(_Pants.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Shirt = modelConfig.Shirt) === null || _modelConfig$Shirt === void 0 ? void 0 : _modelConfig$Shirt.name) && /*#__PURE__*/_react.default.createElement(_Shirt.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Shoe = modelConfig.Shoe) === null || _modelConfig$Shoe === void 0 ? void 0 : _modelConfig$Shoe.name) && /*#__PURE__*/_react.default.createElement(_Shoe.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Skin = modelConfig.Skin) === null || _modelConfig$Skin === void 0 ? void 0 : _modelConfig$Skin.name) && /*#__PURE__*/_react.default.createElement(_Skin.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Skin2 = modelConfig.Skin) === null || _modelConfig$Skin2 === void 0 ? void 0 : _modelConfig$Skin2.name) && /*#__PURE__*/_react.default.createElement(_Head.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Skin3 = modelConfig.Skin) === null || _modelConfig$Skin3 === void 0 ? void 0 : _modelConfig$Skin3.name) && /*#__PURE__*/_react.default.createElement(_Eyelash.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Skin4 = modelConfig.Skin) === null || _modelConfig$Skin4 === void 0 ? void 0 : _modelConfig$Skin4.name) && /*#__PURE__*/_react.default.createElement(_Eyebrow.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Glasses = modelConfig.Glasses) === null || _modelConfig$Glasses === void 0 ? void 0 : _modelConfig$Glasses.name) && /*#__PURE__*/_react.default.createElement(_Glasses.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), (modelConfig === null || modelConfig === void 0 ? void 0 : (_modelConfig$Hat = modelConfig.Hat) === null || _modelConfig$Hat === void 0 ? void 0 : _modelConfig$Hat.name) && /*#__PURE__*/_react.default.createElement(_Hat.default, {
    modelConfig: modelConfig,
    nodes: nodes,
    LOD: LOD
  }), /*#__PURE__*/_react.default.createElement("primitive", {
    object: nodes.Armature
  })));
};

var _default = Player; // useGLTF.preload(`${characters.path}/main/${characters.base}`)

exports.default = _default;