import { defineStore } from "pinia";
import { computed } from "vue";
import { permissionStore } from "src/stores/permission";

export const navigationStore = defineStore("navigation", () => {
  const navigationStructure = Object.freeze([
    {
      type: "single",
      title: "ui.navigation.dashboard",
      name: "DashboardPage",
      icon: "mdi-home-variant-outline",
      disable: false,
      permission: false,
      permissionScope: "dashboard",
    },
    {
      type: "expansive",
      title: "ui.navigation.registration",
      icon: "mdi-cog-outline",
      disable: false,
      permission: false,
      permissionScope: "config",
      childrens: [
        {
          type: "single",
          title: "ui.navigation.users",
          name: "UsersPage",
          icon: "mdi-account-multiple-outline",
          disable: false,
          permission: false,
          permissionScope: "config.user",
        },
        {
          type: "single",
          title: "ui.navigation.city",
          name: "CityPage",
          icon: "mdi-city-variant-outline",
          disable: false,
          permission: false,
          permissionScope: "config.city",
        },
        {
          type: "single",
          title: "ui.navigation.country",
          name: "CountryPage",
          icon: "mdi-earth",
          disable: false,
          permission: false,
          permissionScope: "config.country",
        },
        {
          type: "single",
          title: "ui.navigation.state",
          name: "StatePage",
          icon: "mdi-map-marker",
          disable: false,
          permission: false,
          permissionScope: "config.state",
        },
      ],
    },
  ]);

  const getNavigationAccess = () => {
    const { getAccess } = permissionStore();
    return navigationStructure
      .map((menu) => {
        if (menu.type === "expansive") {
          if (getAccess(menu.permissionScope, "menu")) {
            menu.permission = true;
          }
          menu.childrens = menu.childrens.filter((children) => {
            children.permission = getAccess(children.permissionScope, "menu");
            return children.permission;
          });
          return menu.childrens.length > 0 ? menu : null;
        } else {
          menu.permission = getAccess(menu.permissionScope, "menu");
          return menu;
        }
      })
      .filter((menu) => menu !== null);
  };

  const navigationItems = computed(() => getNavigationAccess());

  return {
    navigationItems,
  };
});
