import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
var _excluded = ["children", "primitive", "events"];

/* eslint-disable */
import { Component, createElement } from 'react';
import addEventListeners from './addEventListeners';
import removeEventListeners from './removeEventListeners';
import updateAttributes from './updateAttributes';
import updateEventListeners from './updateEventListeners';
var options = {
  runSetAttributeOnUpdates: true
};

var Entity = /*#__PURE__*/function (_Component) {
  _inheritsLoose(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 = _assertThisInitialized(_this),
          props = _assertThisInitialize.props;

      var eventName;

      if (!el) {
        return;
      }

      _this.el = el;

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

      updateAttributes(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;
    updateEventListeners(el, prevProps.events, props.events);

    if (options.runSetAttributeOnUpdates) {
      updateAttributes(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) {
        removeEventListeners(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 = _objectWithoutPropertiesLoose(_this$props, _excluded);

    var elementName = primitive || 'a-entity';

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

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

  return Entity;
}(Component);

export { Entity as default };