'use strict';

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

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

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

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