'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../../utils/index.cjs');
var raf = require('../../../../utils/vue/raf.cjs');

function useHeights(mergedData, getKey, onItemAdd, onItemRemove) {
  const instance = /* @__PURE__ */ new Map();
  const heights = /* @__PURE__ */ new Map();
  const updatedMark = vue.ref(Symbol("update"));
  vue.watch(mergedData, () => {
    updatedMark.value = Symbol("update");
  });
  let collectRaf;
  function cancelRaf() {
    raf.raf.cancel(collectRaf);
  }
  function collectHeight() {
    cancelRaf();
    collectRaf = raf.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);
    }
  }
  vue.onUnmounted(() => {
    cancelRaf();
  });
  return [setInstance, collectHeight, heights, updatedMark];
}

exports.default = useHeights;
//# sourceMappingURL=useHeights.cjs.map
