import "core-js/modules/es6.regexp.split";
import _inheritsLoose from "../../inheritsLoose";
import _defineProperty from "@babel/runtime-corejs2/helpers/defineProperty";
import Intact from 'intact-angular';

var MoveWrapper =
/*#__PURE__*/
function (_Intact) {
  _inheritsLoose(MoveWrapper, _Intact);

  function MoveWrapper() {
    return _Intact.apply(this, arguments) || this;
  }

  var _proto = MoveWrapper.prototype;

  _proto.template = function template(self, Vdt) {
    var children = self.get('children');
    return children || Vdt.miss.hc('MoveWrapper');
  };

  _proto.defaults = function defaults() {
    return {
      autoDestroy: true,
      container: undefined
    };
  };

  _proto.init = function init() {
    var _Intact$prototype$ini;

    // we must append the element before _mount lifecycles of children are called
    this.mountedQueue.push(this._appendElement);

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

    (_Intact$prototype$ini = _Intact.prototype.init).call.apply(_Intact$prototype$ini, [this].concat(args));

    return this.placeholder = document.createComment("placeholder");
  };

  _proto.hydrate = function hydrate() {
    var _Intact$prototype$hyd;

    this.mountedQueue.push(this._appendElement);

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

    (_Intact$prototype$hyd = _Intact.prototype.hydrate).call.apply(_Intact$prototype$hyd, [this].concat(args));

    return this.placeholder = document.createComment('placeholder');
  };

  _proto.update = function update(lastVNode, nextVNode) {
    _Intact.prototype.update.call(this, lastVNode, nextVNode);

    return this.placeholder;
  };

  _proto.destroy = function destroy() {
    if (this.get('autoDestroy')) {
      this._$destroy.apply(this, arguments);
    }
  }
  /**
   * @brief override super destroy 
   *
   * MoveWraper component has changed the dom struction.
   * It is always the top level dom, so we should 
   * remove it from parent dom. By passing the container
   * to remove function can do this. We can't remove it
   * directly by calling removeChild method, beacause it
   * maybe have leave animation.
   *
   * @param ...args
   */
  ;

  _proto._$destroy = function _$destroy(lastVNode, nextVNode) {
    // if (this.destroyed) {
    // return console.warn('destroyed multiple times');
    // }
    if (this.rendered && (!nextVNode || !(nextVNode.type & Intact.Vdt.miss.Types.ComponentClassOrInstance) || nextVNode.key !== lastVNode.key)) {
      Intact.Vdt.miss.remove(this.vdt.vNode, this.container);
    }

    this._destroy(lastVNode, nextVNode);

    this.destroyed = true;
    this.trigger('$destroyed', this);
    this.off();
  };

  _proto._appendElement = function _appendElement() {
    var container = this.get('container');

    if (container) {
      if (typeof container === 'string') {
        this.container = document.querySelector(container);
      } else {
        this.container = container(this.placeholder);
      }
    }

    if (!this.container) {
      // find the closest dialog if exists
      var dom = this.placeholder;
      var found;

      while ((dom = dom.parentNode) && dom.nodeType === 1) {
        if (dom.className && dom.className.split(' ').indexOf('k-dialog') > -1) {
          found = dom;
          break;
        }
      }

      this.container = found || document.body;
    }

    this.container.appendChild(this.element);
    this.trigger('appended');
  };

  return MoveWrapper;
}(Intact);

_defineProperty(MoveWrapper, "propTypes", {
  autoDestroy: Boolean,
  container: [Function, String]
});

export { MoveWrapper as default };
export { MoveWrapper };