// The Lucia project.
// Copyright 2023-2026 DSP, inc.  All rights reserved.
// Authors:
//   chiayin.kuo@dsp.im (chiayin), 2023/1/31
//   imacat.yang@dsp.im (imacat), 2023/9/23
//   cindy.chang@dsp.im (Cindy Chang), 2024/5/30
/**
 * @module stores/pageAdmin Navigation state management for tracking
 *   active, previous, and pending page states across route transitions
 *   and SweetAlert2 modal interactions.
 */

import { defineStore } from "pinia";
import { mapPageNameToCapitalUnifiedName } from "../utils/pageUtils";

/** @constant {boolean} Enable console logging for page admin debugging. */
const printPageAdminLog = false;

/**
 * Pinia store for managing navigation page state transitions.
 *
 * Handles two navigation triggers: clicking navigation buttons and
 * page refreshes. Manages pending states for SweetAlert2 modal
 * confirmation flows.
 */
export const usePageAdminStore = defineStore("pageAdminStore", {
  state: () => ({
    activePage: "MAP",
    previousPage: "MAP",
    pendingActivePage: "FILES",
    isPagePending: false,
    shouldKeepPreviousPage: false, // false -- meaning modal is not pressed as "NO"
    activePageComputedByRoute: "MAP",
    currentMapFile: "",
  }),
  getters: {},
  actions: {
    /**
     * Sets the active page based on the last matched route record.
     * @param {Array} routeMatched - The route.matched array.
     */
    setActivePageComputedByRoute(routeMatched) {
      if (
        routeMatched.length &&
        routeMatched[routeMatched.length - 1]?.name
      ) {
        printPageAdminLog &&
          console.log(
            "setActivePageComputedByRoute()",
            mapPageNameToCapitalUnifiedName(
              routeMatched[routeMatched.length - 1].name,
            ),
          );
        this.activePageComputedByRoute = mapPageNameToCapitalUnifiedName(
          routeMatched[routeMatched.length - 1].name,
        );
      }
    },
    /**
     * Set Active Page; the page which the user is currently visiting
     * @param {string} activePage
     */
    setActivePage(activePage) {
      printPageAdminLog && console.log("setActivePage", activePage);
      this.activePage = mapPageNameToCapitalUnifiedName(activePage);
    },
    /**
     * Specify previous page state value.
     * @param {string} prevPage
     */
    setPreviousPage(prevPage) {
      this.previousPage = mapPageNameToCapitalUnifiedName(prevPage);
    },
    /**
     * Set the previous(usually current) pages to the ones we just decide.
     */
    setPreviousPageUsingActivePage() {
      this.previousPage = this.activePage;
    },
    /**
     * Set the boolean value of status of pending state of the pate
     * For the control of Swal popup
     * @param {boolean} boolVal
     */
    setIsPagePendingBoolean(boolVal) {
      this.isPagePending = boolVal;
    },
    /**
     * Copy(transit) the value of pendingActivePage to activePage
     */
    copyPendingPageToActivePage() {
      printPageAdminLog &&
        console.log(
          "pinia copying  this.pendingActivePage",
          this.pendingActivePage,
        );
      this.activePage = this.pendingActivePage;
    },
    /**
     * Set Pending active Page, meaning we are not sure if user will chang her mind later on.
     * Also, start pending state.
     * Often, user triggers the modal and the pending state starts.
     * Note: String conversion is needed. For Example, CheckMap is converted into MAP
     * @param {string} argPendingActivePage
     */
    setPendingActivePage(argPendingActivePage) {
      printPageAdminLog &&
        console.log(
          "pinia setting this.pendingActivePage",
          this.pendingActivePage,
        );
      this.pendingActivePage =
        mapPageNameToCapitalUnifiedName(argPendingActivePage);
    },
    /**
     * Set Pending active Page to empty string; we call this right after we just decide an active page.
     * Also, stop pending state.
     */
    clearPendingActivePage() {
      this.pendingActivePage = "";
    },
    /**
     * When user dismiss the popup modal, we don't apply the new page,
     * instead, we apply the previous page.
     */
    keepPreviousPage() {
      printPageAdminLog &&
        console.log("pinia keeping this.previousPage", this.previousPage);
      this.activePage = this.previousPage;
      this.shouldKeepPreviousPage = true;
    },
    /**
     * Clean up the state of the boolean related to modal showing phase
     */
    clearShouldKeepPreviousPageBoolean() {
      printPageAdminLog && console.log("clearShouldKeepPreviousPageBoolean()");
      this.shouldKeepPreviousPage = false;
    },
    /**
     * Stores the name of the currently opened map file.
     * @param {string} fileName - The file name.
     */
    setCurrentMapFile(fileName) {
      this.currentMapFile = fileName;
    },
  },
});
