"use strict";

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

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

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

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

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _react = require("react");

var _addEventListeners = _interopRequireDefault(require("./addEventListeners"));

var _removeEventListeners = _interopRequireDefault(require("./removeEventListeners"));

var _updateAttributes = _interopRequireDefault(require("./updateAttributes"));

var _updateEventListeners = _interopRequireDefault(require("./updateEventListeners"));

var _excluded = ["children", "primitive", "events"];
var options = {
  runSetAttributeOnUpdates: true
};

var Entity = /*#__PURE__*/function (_Component) {
  (0, _inheritsLoose2.default)(Entity, _Component);

  function Entity() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _Component.call.apply(_Component, [this].concat(args)) || this;

    _this.initEntity = function (el) {
      var _assertThisInitialize = (0, _assertThisInitialized2.default)(_this),
          props = _assertThisInitialize.props;

      var eventName;

      if (!el) {
        return;
      }

      _this.el = el;

      if (props.events) {
        for (eventName in props.events) {
          (0, _addEventListeners.default)(el, eventName, props.events[eventName]);
        }
      }

      (0, _updateAttributes.default)(el, null, props);

      if (props._ref) {
        props._ref(el);
      }
    };

    return _this;
  }

  var _proto = Entity.prototype;

  _proto.componentDidUpdate = function componentDidUpdate(prevProps, prevState) {
    var el = this.el;
    var props = this.props;
    (0, _updateEventListeners.default)(el, prevProps.events, props.events);

    if (options.runSetAttributeOnUpdates) {
      (0, _updateAttributes.default)(el, prevProps, props);
    }
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    var el = this.el;
    var props = this.props;
    var eventName;

    if (props.events) {
      for (eventName in props.events) {
        (0, _removeEventListeners.default)(el, eventName, props.events[eventName]);
      }
    }
  };

  _proto.render = function render() {
    var propName;
    var reactProps = {};
    var _this$props = this.props,
        children = _this$props.children,
        primitive = _this$props.primitive,
        events = _this$props.events,
        restProps = (0, _objectWithoutPropertiesLoose2.default)(_this$props, _excluded);
    var elementName = primitive || 'a-entity';

    for (propName in restProps) {
      if (restProps[propName] !== undefined) {
        reactProps[propName] = restProps[propName];
      }
    }

    return /*#__PURE__*/(0, _react.createElement)(elementName, (0, _extends2.default)({
      ref: this.initEntity
    }, reactProps), children);
  };

  return Entity;
}(_react.Component);

exports.default = Entity;