import { ref, watch, onUnmounted } from 'vue';
import '../../../../utils/index.mjs';
import { raf } from '../../../../utils/vue/raf.mjs';

function useHeights(mergedData, getKey, onItemAdd, onItemRemove) {
  const instance = /* @__PURE__ */ new Map();
  const heights = /* @__PURE__ */ new Map();
  const updatedMark = ref(Symbol("update"));
  watch(mergedData, () => {
    updatedMark.value = Symbol("update");
  });
  let collectRaf;
  function cancelRaf() {
    raf.cancel(collectRaf);
  }
  function collectHeight() {
    cancelRaf();
    collectRaf = raf(() => {
      instance.forEach((element, key) => {
        if (element && element.offsetParent) {
          const {
            offsetHeight
          } = element;
          if (heights.get(key) !== offsetHeight) {
            updatedMark.value = Symbol("update");
            heights.set(key, element.offsetHeight);
          }
        }
      });
    });
  }
  function setInstance(item, ins) {
    const key = getKey(item);
    const origin = instance.get(key);
    if (ins) {
      instance.set(key, ins.$el || ins);
      collectHeight();
    } else {
      instance.delete(key);
    }
    if (!origin !== !ins) {
      if (ins)
        onItemAdd == null ? void 0 : onItemAdd(item);
      else
        onItemRemove == null ? void 0 : onItemRemove(item);
    }
  }
  onUnmounted(() => {
    cancelRaf();
  });
  return [setInstance, collectHeight, heights, updatedMark];
}

export { useHeights as default };
//# sourceMappingURL=useHeights.mjs.map
