"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = require("vue");

var _raf = _interopRequireDefault(require("../../_util/raf"));

var StatusQueue = ['measure', 'align', null, 'motion'];

var _default = function _default(visible, doMeasure) {
  var status = (0, _vue.ref)(null);
  var rafRef = (0, _vue.ref)();
  var destroyRef = (0, _vue.ref)(false);

  function setStatus(nextStatus) {
    if (!destroyRef.value) {
      status.value = nextStatus;
    }
  }

  function cancelRaf() {
    _raf.default.cancel(rafRef.value);
  }

  function goNextStatus(callback) {
    cancelRaf();
    rafRef.value = (0, _raf.default)(function () {
      // Only align should be manually trigger
      var newStatus = status.value;

      switch (status.value) {
        case 'align':
          newStatus = 'motion';
          break;

        case 'motion':
          newStatus = 'stable';
          break;

        default:
      }

      setStatus(newStatus);
      callback === null || callback === void 0 ? void 0 : callback();
    });
  }

  (0, _vue.watch)(visible, function () {
    setStatus('measure');
  }, {
    immediate: true,
    flush: 'post'
  });
  (0, _vue.onMounted)(function () {
    // Go next status
    (0, _vue.watch)(status, function () {
      switch (status.value) {
        case 'measure':
          doMeasure();
          break;

        default:
      }

      if (status.value) {
        (0, _vue.nextTick)(function () {
          var index = StatusQueue.indexOf(status.value);
          var nextStatus = StatusQueue[index + 1];

          if (nextStatus && index !== -1) {
            setStatus(nextStatus);
          }
        });
      }
    }, {
      immediate: true,
      flush: 'post'
    });
  });
  (0, _vue.onBeforeUnmount)(function () {
    destroyRef.value = true;
    cancelRaf();
  });
  return [status, goNextStatus];
};

exports.default = _default;