import { InputParseError } from "../../entities/errors/common";
import {
  getArticleBySlugInputSchema,
  type GetArticleBySlugOutput,
  type IGetArticleBySlugUseCase,
} from "../../application/use-cases/get-article-by-slug.use-case";

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

export type IGetArticleBySlugController = ReturnType<typeof getArticleBySlugController>;

export const getArticleBySlugController =
  (getArticleBySlugUseCase: IGetArticleBySlugUseCase) =>
  async (input: unknown): Promise<ReturnType<typeof presenter>> => {
    const parsed = getArticleBySlugInputSchema.safeParse(input);
    if (!parsed.success) {
      throw new InputParseError("Invalid get-article-by-slug input", { cause: parsed.error });
    }
    const result = await getArticleBySlugUseCase(parsed.data);
    return presenter(result);
  };
