import { SessionManager } from '../session/manager.js';
import { errorCode, errorMessage } from "../error_utils.js";
import { resolveSessionForTool, mergeSessionResolutionMetadata } from './session_resolution.js';
import { ok, err, type ToolResponse } from './types.js';

export async function getFootnotes(
  manager: SessionManager,
  params: {
    file_path?: string;
  },
): Promise<ToolResponse> {
  const resolved = await resolveSessionForTool(manager, params, { toolName: 'get_footnotes' });
  if (!resolved.ok) return resolved.response;
  const { session, metadata } = resolved;

  try {
    const footnotes = await session.doc.getFootnotes();
    return ok(mergeSessionResolutionMetadata({
      footnotes: footnotes.map((f) => ({
        id: f.id,
        display_number: f.displayNumber,
        text: f.text,
        anchored_paragraph_id: f.anchoredParagraphId,
      })),
      file_path: manager.normalizePath(session.originalPath),
    }, metadata));
  } catch (e: unknown) {
    return err('FOOTNOTE_ERROR', errorMessage(e));
  }
}
