import { InputParseError } from "../../entities/errors/common";
import {
  getHeaderInputSchema,
  type GetHeaderOutput,
  type IGetHeaderUseCase,
} from "../../application/use-cases/get-header.use-case";

function presenter(value: GetHeaderOutput) {
  return value;
}

export type IGetHeaderController = ReturnType<typeof getHeaderController>;

export const getHeaderController =
  (getHeaderUseCase: IGetHeaderUseCase) =>
  async (input: unknown): Promise<ReturnType<typeof presenter>> => {
    const parsed = getHeaderInputSchema.safeParse(input);
    if (!parsed.success) {
      throw new InputParseError("Invalid get-header input", { cause: parsed.error });
    }
    const result = await getHeaderUseCase(parsed.data);
    return presenter(result);
  };
