// stores/cms.ts
import { defineStore } from 'pinia';

export interface Action {
  id: string;
  label: string;
}

export const useCmsStore = defineStore('cms', {
  state: () => ({
    // sidebar
    sidebarOpen: false as boolean,

    // upload actions
    actions: [] as Action[],
    mainActionId: '' as string,
    isProcessing: false as boolean,
    currentActionId: null as string | null,
  }),
  getters: {
    mainAction(state) {
      return state.actions.find((a) => a.id === state.mainActionId)!;
    },
    dropdownActions(state) {
      return state.actions.filter((a) => a.id !== state.mainActionId);
    },
  },
  actions: {
    toggleSidebar() {
      this.sidebarOpen = !this.sidebarOpen;
    },
    setActions(payload: { actions: Action[]; mainId: string }) {
      this.actions = payload.actions;
      this.mainActionId = payload.mainId;
    },
    async performAction(id: string) {
      if (this.isProcessing) return;
      this.isProcessing = true;
      this.currentActionId = id;

      // ▶︎ call your API or form submit here
      try {
        console.log('performance Called'); // you could show a toast, etc.
      } finally {
        this.isProcessing = false;
        this.currentActionId = null;
      }
    },
  },
});
