import { QueryParameter } from '$lib/constants';
import { foldersStore } from '$lib/stores/folders.svelte';
import { authenticate } from '$lib/utils/auth';
import { getFormatter } from '$lib/utils/i18n';
import type { PageLoad } from './$types';

export const load = (async ({ url }) => {
  await authenticate(url);
  const [, $t] = await Promise.all([foldersStore.fetchTree(), getFormatter()]);

  let tree = foldersStore.folders!;
  const path = url.searchParams.get(QueryParameter.PATH);
  if (path) {
    tree = tree.traverse(path);
  } else if (path === null) {
    // If no path is provided, we've just navigated to the folders page.
    // We should bust the asset cache of the folder store, to make sure we don't show stale data
    foldersStore.bustAssetCache();
  }

  // only fetch assets if the folder has assets
  const pathAssets = tree.hasAssets ? await foldersStore.fetchAssetsByPath(tree.path) : null;

  return {
    tree,
    pathAssets,
    meta: {
      title: $t('folders'),
    },
  };
}) satisfies PageLoad;
