import {defineStore} from "pinia";
import {config} from "@/config.js";
import performRequest from "@/backend.js";

export const useHelpStore = defineStore('help', {
    state() {
        return {
            currentPage: null,
            pages: null,
            content: null,
            showHelp: false,
        }
    },
    getters: {
        title(state) {
          return state.pages?.find(p => p.page === state.currentPage)?.title;
        },
        videoUrl(state) {
            return state.content?.video;
        },
        text(state) {
            return state.content?.content;
        },
        showHelpVideo() {
            return this.baseUrl && this.baseUrl !== "";
        },
        enableHelp() {
            return this.pages !== null && this.pages.length > 0;
        }
    },
    actions: {
        async getContent(pageId) {
            const url = `${config.backendUrl}/help/content/${pageId}`;
            const {data: data, headers: headers} = await performRequest(this, 'GET', url, null);
            console.log("help system", data);
            this.content = data;
            this.currentPage = pageId;
        },
        async loadPages() {
            const url = `${config.backendUrl}/help/content`;
            const {data: data, headers: headers} = await performRequest(this, 'GET', url, null);
            this.pages = data;
        },
        async activateHelp(pageId) {
            await this.getContent(pageId);
            this.showHelp = true;

        },
        closeHelp() {
            this.showHelp = false;
        }
    }
});