import { defineStore } from "pinia";

export type AppState = {
  userLoaded: boolean;
  routingFinished: boolean;
  showMainNavigationBar: boolean;
};

const showMainNavigationBarInitialState = () => {
  let path = window.location.pathname;

  // remove dangling slash
  if (path.endsWith("/")) {
    path = path.slice(0, -1);
  }

  const numberOfSlashes = (path.match(/\//g) || []).length;

  // it should hide main navigation bar when on learning content page
  if (path.startsWith("/learn/") && numberOfSlashes >= 4) {
    return false;
  }

  return true;
};

export const useAppStore = defineStore({
  id: "app",
  state: () =>
    ({
      showMainNavigationBar: showMainNavigationBarInitialState(),
      userLoaded: false,
      routingFinished: false,
    } as AppState),
  getters: {},
  actions: {},
});
