import type { Ref } from 'vue';
import { fetchBoards } from './boardFetching';
import type { BoardNavConfig } from '@/types/vueRefs';

export const goToPage = async (pageNumber: number, config: BoardNavConfig) => {
  config.currentPage.value = pageNumber;
  await fetchBoards(
    config.currentCollection,
    config.sortingOrder,
    config.userRole,
    config.currentItems,
    config.currentPage,
    config.itemsPerPage,
    config.access || 'public'
  );
};

export const prevPage = async (config: BoardNavConfig) => {
  if (config.currentPage.value > 1) {
    await goToPage(config.currentPage.value - 1, config);
  }
};

export const nextPage = async (config: BoardNavConfig) => {
  if (!config.totalCount) {
    console.warn('⚠️ totalCount is not provided in BoardNavConfig.');
    return;
  }

  const maxPage = Math.ceil(config.totalCount.value / config.itemsPerPage);
  if (config.currentPage.value < maxPage) {
    await goToPage(config.currentPage.value + 1, config);
  }
};
