import { InputParseError } from "../../entities/errors/common";
import {
  createArticleInputSchema,
  type CreateArticleOutput,
  type ICreateArticleUseCase,
} from "../../application/use-cases/create-article.use-case";

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

export type ICreateArticleController = ReturnType<typeof createArticleController>;

export const createArticleController =
  (createArticleUseCase: ICreateArticleUseCase) =>
  async (input: unknown): Promise<ReturnType<typeof presenter>> => {
    const parsed = createArticleInputSchema.safeParse(input);
    if (!parsed.success) {
      throw new InputParseError("Invalid create-article input", { cause: parsed.error });
    }
    const result = await createArticleUseCase(parsed.data);
    return presenter(result);
  };
