import { InputParseError } from "../../entities/errors/common";
import {
  listMediaInputSchema,
  type ListMediaOutput,
  type IListMediaUseCase,
} from "../../application/use-cases/list-media.use-case";

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

export type IListMediaController = ReturnType<typeof listMediaController>;

export const listMediaController =
  (listMediaUseCase: IListMediaUseCase) =>
  async (input: unknown): Promise<ReturnType<typeof presenter>> => {
    const parsed = listMediaInputSchema.safeParse(input);
    if (!parsed.success) {
      throw new InputParseError("Invalid list-media input", { cause: parsed.error });
    }
    const result = await listMediaUseCase(parsed.data);
    return presenter(result);
  };
