import { reactive, provide } from 'vue';
import { CHILDREN_KEY } from './const';

var useParent = () => {
  const children = reactive([]);
  const addChild = child => {
    if (children.every(item => {
      return item.uid !== child.uid;
    })) {
      children.push(child);
    }
  };
  const removeChild = child => {
    const index = children.findIndex(item => {
      return item.uid === child.uid;
    });
    if (index !== -1) {
      children.splice(index, 1);
    }
  };
  provide(CHILDREN_KEY, {
    addChild,
    removeChild
  });
  return {
    children
  };
};

export { useParent as default };
