import { useRuntimeConfig } from '#imports';
import type { BoardItem, BoardAccessMode } from '~/types';

interface FetchSingleItemParams {
  collection: string;
  docId: string;
  access?: BoardAccessMode;
}

const FUNCTION_BASE = 'https://fetchsingleitem-edvvp3hbnq-du.a.run.app';

/**
 * Fetch a single board item from Firebase HTTPS function
 */
export async function fetchSingleItemFromFunction<T extends BoardItem>({
  collection,
  docId,
  access = 'public',
}: FetchSingleItemParams): Promise<T> {
  const params = {
    collection,
    docId,
    access,
  };

  const result = await $fetch(`${FUNCTION_BASE}`, {
    method: 'GET',
    params,
    credentials: access !== 'public' ? 'include' : undefined,
  });

  return result as T;
}
