import { Route } from '$lib/route';
import { authenticate } from '$lib/utils/auth';
import { getFormatter } from '$lib/utils/i18n';
import { getLibrary, getLibraryStatistics, type LibraryResponseDto } from '@immich/sdk';
import { redirect } from '@sveltejs/kit';
import type { LayoutLoad } from './$types';

export const load = (async ({ params: { id }, url, depends }) => {
  depends('app:library');
  await authenticate(url, { admin: true });

  let library: LibraryResponseDto;

  try {
    library = await getLibrary({ id });
  } catch {
    redirect(307, Route.libraries());
  }

  const statisticsPromise = getLibraryStatistics({ id });
  const $t = await getFormatter();

  return {
    library,
    statisticsPromise,
    meta: {
      title: $t('admin.library_details'),
    },
  };
}) satisfies LayoutLoad;
