import { shallowRef, watch, onMounted, onBeforeUnmount } from 'vue';
import '../../../../utils/index.mjs';
import { raf } from '../../../../utils/vue/raf.mjs';

const StatusQueue = ["measure", "align", null, "motion"];
var useVisibleStatus = (visible, doMeasure) => {
  const status = shallowRef(null);
  const rafRef = shallowRef();
  const destroyRef = shallowRef(false);
  function setStatus(nextStatus) {
    if (!destroyRef.value)
      status.value = nextStatus;
  }
  function cancelRaf() {
    raf.cancel(rafRef.value);
  }
  function goNextStatus(callback) {
    cancelRaf();
    rafRef.value = raf(() => {
      let newStatus = status.value;
      switch (status.value) {
        case "align":
          newStatus = "motion";
          break;
        case "motion":
          newStatus = "stable";
          break;
        default:
      }
      setStatus(newStatus);
      callback == null ? void 0 : callback();
    });
  }
  watch(
    visible,
    () => {
      setStatus("measure");
    },
    { immediate: true, flush: "post" }
  );
  onMounted(() => {
    watch(
      status,
      () => {
        switch (status.value) {
          case "measure":
            doMeasure();
            break;
          default:
        }
        if (status.value) {
          rafRef.value = raf(async () => {
            const index = StatusQueue.indexOf(status.value);
            const nextStatus = StatusQueue[index + 1];
            if (nextStatus && index !== -1)
              setStatus(nextStatus);
          });
        }
      },
      { immediate: true, flush: "post" }
    );
  });
  onBeforeUnmount(() => {
    destroyRef.value = true;
    cancelRaf();
  });
  return [status, goNextStatus];
};

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