import { computed, onMounted, onUpdated, ref } from "vue";

export function useChildLinkParent(options: {
  getPositionExtra?: (index: number) => any,
  onClean?: () => void,
}) {

  let currentIndex = 0;
  let length = 0;


  function getPosition() : Record<string, any> {
    const index = currentIndex++;
    length++;
    return {
      index,
      ...options.getPositionExtra?.(index)
    }
  }
  function resetCounter() {
    currentIndex = 0
  }

  onMounted(() => {
    resetCounter();
    options.onClean?.();
  });
  onUpdated(() => {
    resetCounter();
    options.onClean?.();
  });

  return {
    getLength: () => length,
    getPosition,
    resetCounter,
  }
}

export function useChildLinkChild(getPosition: () => any) {

  const position = computed(() => getPosition());

  onMounted(() => {
    position.value;
  });
  onUpdated(() => {
    position.value;
  });

  return {
    position,
  }
}