import { defineStore } from "pinia";
import { ref } from "vue";
import { help_module_details } from "src/modules/help/models/help-module.model";
import { useAuthStore } from "src/stores/auth-store";
import type { HelpModuleOptions } from "src/modules/help/models/help-module.model";
import type { UserModuleAccess } from "src/modules/shared/models/user.models";



export const useHelpStore = defineStore('help', () => {
    const is_loading = ref(false);
    const user_modules = ref<Record<UserModuleAccess, HelpModuleOptions[]>>();
    const auth_store = useAuthStore();

    const getFilteredModule = () => {
        if(!auth_store.user) return;

        const entries = auth_store.user.user_module_access
            .map((key) => {
                const options = help_module_details[key];
                return options ? ([key, options] as const) : null;
            })
            .filter(
                (entry): entry is readonly [UserModuleAccess, HelpModuleOptions[]] =>
                    entry !== null
            );

        user_modules.value = Object.fromEntries(entries) as Record<UserModuleAccess, HelpModuleOptions[]>;
    }

    return {
        is_loading,
        user_modules,
        help_module_details,
        getFilteredModule,
    };
});