import { sortChildren } from "../utils/vnodes";
export function ChildrenMixin(parent, options) {
  var _inject, _computed;

  if (options === void 0) {
    options = {};
  }

  var indexKey = options.indexKey || "index";
  return {
    inject: (_inject = {}, _inject[parent] = {
      default: null
    }, _inject),
    computed: (_computed = {
      parents: function parents() {
        if (this.disableBindRelation) {
          return null;
        }

        return this[parent];
      }
    }, _computed[indexKey] = function () {
      this.bindRelation();

      if (this.parents) {
        return this.parents.children.indexOf(this);
      }

      return null;
    }, _computed),
    watch: {
      disableBindRelation: function disableBindRelation(val) {
        if (!val) {
          this.bindRelation();
        }
      }
    },
    mounted: function mounted() {
      this.bindRelation();
    },
    beforeDestroy: function beforeDestroy() {
      var _this = this;

      if (this.parents) {
        this.parents.children = this.parents.children.filter(function (item) {
          return item !== _this;
        });
      }
    },
    methods: {
      bindRelation: function bindRelation() {
        if (!this.parents || this.parents.children.indexOf(this) !== -1) {
          return;
        }

        var children = [].concat(this.parents.children, [this]);
        sortChildren(children, this.parents);
        this.parents.children = children;
      }
    }
  };
}
export function ParentMixin(parent) {
  return {
    provide: function provide() {
      var _ref;

      return _ref = {}, _ref[parent] = this, _ref;
    },
    data: function data() {
      return {
        children: []
      };
    }
  };
}