import { loadingHold } from "@racletteCore/store/state"
import { $store } from "@racletteCore"
import type { RouteLocationNormalized } from "vue-router"
import { handleRouteChange } from "../routeStore"

/**
 * afterEachHook
 * @param { object } store, mini-rx store instance
 *
 * removeLoading Animation
 */
export default (to: RouteLocationNormalized, from: RouteLocationNormalized) => {
  const loading = $store.getState(["ui", "loading"])
  if (loading) {
    setTimeout(
      () =>
        $store.dispatch({
          type: "ui/update",
          payload: { loading: false },
        }),
      loadingHold,
    )
  }
  handleRouteChange(to, from)
}
