text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
      value: true
  });
  exports.default = DefaultBehaviorsMixin;
  
  var _from = require('babel-runtime/core-js/array/from');
  
  var _from2 = _interopRequireDefault(_from);
  
  var _keys = require('babel-runtime/core-js/object/keys');
  
  var _keys2 = _interopRequireDefault(_keys);
  
  var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
  
  var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  
  var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  
  var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  
  var _createClass2 = require('babel-runtime/helpers/createClass');
  
  var _createClass3 = _interopRequireDefault(_createClass2);
  
  var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  
  var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  
  var _get2 = require('babel-runtime/helpers/get');
  
  var _get3 = _interopRequireDefault(_get2);
  
  var _inherits2 = require('babel-runtime/helpers/inherits');
  
  var _inherits3 = _interopRequireDefault(_inherits2);
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function DefaultBehaviorsMixin(ElementClass) {
  
      // TODO This is here for now. Make it an extension to
      // element-behaviors so that it can be applied to any element
      // generically.
      return function (_ElementClass) {
          (0, _inherits3.default)(DefaultBehaviors, _ElementClass);
  
          function DefaultBehaviors() {
              (0, _classCallCheck3.default)(this, DefaultBehaviors);
  
              return (0, _possibleConstructorReturn3.default)(this, (DefaultBehaviors.__proto__ || (0, _getPrototypeOf2.default)(DefaultBehaviors)).apply(this, arguments));
          }
  
          (0, _createClass3.default)(DefaultBehaviors, [{
              key: 'construct',
              value: function construct() {
                  var this$1 = this;
                  var args = [],
                      len = arguments.length;
                  while (len--) args[len] = arguments[len];
  
                  if ((0, _get3.default)(DefaultBehaviors.prototype.__proto__ || (0, _getPrototypeOf2.default)(DefaultBehaviors.prototype), 'construct', this)) {
                      (ref = (0, _get3.default)(DefaultBehaviors.prototype.__proto__ || (0, _getPrototypeOf2.default)(DefaultBehaviors.prototype), 'construct', this)).call.apply(ref, [this].concat(args));
                  }
  
                  // If no geometry or material behavior is detected, add default ones.
                  //
                  // Deferring to a microtask doesn't work here, we must defer to a
                  // macrotask with setTimeout, so we can detect if the element has
                  // initial behaviors, otherwise the element's initial attributes
                  // haven't been added yet (this is how HTML engines work, see
                  // https://github.com/whatwg/dom/issues/522).
                  //
                  // TODO: If we use setTimeout (macrotask) deferral anywhere (like we do
                  // here), and maybe even with microtask deferral (f.e. Promise), maybe
                  // we should have a single place that initiate this deferral so that
                  // everything in the engine can hook into it. Otherwise if different
                  // call sites use setTimeout, logic will be firing at random and in
                  // different order.
                  setTimeout(function () {
                      return this$1._setDefaultBehaviorsIfNeeded();
                  }, 0);
                  var ref;
              }
          }, {
              key: '_setDefaultBehaviorsIfNeeded',
              value: function _setDefaultBehaviorsIfNeeded() {
                  var defaultBehaviors = this.constructor.defaultBehaviors;
  
                  // do nothing if there's no defaults
                  if (!defaultBehaviors) {
                      return;
                  }
                  if ((0, _keys2.default)(defaultBehaviors).length == 0) {
                      return;
                  }
  
                  var initialBehaviorNames = (0, _from2.default)(this.behaviors.keys());
  
                  // small optimization: if there are no initial behaviors and we
                  // have default behaviors, just set the default behaviors.
                  if (initialBehaviorNames.length == 0) {
  
                      // if not an array, then it's an object.
                      if (!(defaultBehaviors instanceof Array)) {
                          defaultBehaviors = (0, _keys2.default)(defaultBehaviors);
                      }
  
                      this.setAttribute('has', this.getAttribute('has') + " " + defaultBehaviors.join(' '));
                  }
  
                  // otherwise detect which default behavior(s) to add
                  else {
  
                          var behaviorNamesToAdd = '';
  
                          // if defaultBehaviors is an array, use default logic to add
                          // behaviors that aren't already added.
                          if (defaultBehaviors instanceof Array) {
                              for (var i$1 = 0, list$1 = defaultBehaviors; i$1 < list$1.length; i$1 += 1) {
  
                                  var defaultBehaviorName = list$1[i$1];
  
                                  var hasBehavior = false;
  
                                  for (var i = 0, list = initialBehaviorNames; i < list.length; i += 1) {
                                      var initialBehaviorName = list[i];
  
                                      if (defaultBehaviorName == initialBehaviorName) {
                                          hasBehavior = true;
                                          break;
                                      }
                                  }
  
                                  if (hasBehavior) {
                                      continue;
                                  } else {
                                      // TODO programmatic API:
                                      //this.behaviors.add('box-geometry')
  
                                      // add a space in front of each name except the first
                                      if (behaviorNamesToAdd) {
                                          behaviorNamesToAdd += ' ';
                                      }
  
                                      behaviorNamesToAdd += defaultBehaviorName;
                                  }
                              }
                          }
  
                          // if defaultBehaviors is an object, then behaviors are added
                          // based on conditions.
                          else if (typeof defaultBehaviors == 'object') {
                                  var defaultBehaviorNames = (0, _keys2.default)(defaultBehaviors);
  
                                  for (var i$2 = 0, list$2 = defaultBehaviorNames; i$2 < list$2.length; i$2 += 1) {
                                      var defaultBehaviorName$1 = list$2[i$2];
  
                                      var condition = defaultBehaviors[defaultBehaviorName$1];
  
                                      if (typeof condition == 'function' && condition(initialBehaviorNames) || typeof condition != 'function' && condition) {
  
                                          // add a space in front of each name except the first
                                          if (behaviorNamesToAdd) {
                                              behaviorNamesToAdd += ' ';
                                          }
  
                                          behaviorNamesToAdd += defaultBehaviorName$1;
                                      }
                                  }
                              }
  
                          // add the needed behaviors all at once.
                          if (behaviorNamesToAdd) {
                              var currentHasValue = this.getAttribute('has');
  
                              if (currentHasValue) {
                                  currentHasValue += ' ';
                              }
  
                              this.setAttribute('has', currentHasValue + behaviorNamesToAdd);
                          }
                      }
              }
          }], [{
              key: 'defaultBehaviors',
  
              // override in subclasses
              get: function () {
                  return [];
              }
          }]);
  
          return DefaultBehaviors;
      }(ElementClass);
  }