import { InputParseError } from "../../entities/errors/common";
import {
  signUpInputSchema,
  type ISignUpUseCase,
  type SignUpOutput,
} from "../../application/use-cases/sign-up.use-case";

function presenter(value: SignUpOutput) {
  return value.cookie;
}

export type ISignUpController = ReturnType<typeof signUpController>;

export const signUpController =
  (signUpUseCase: ISignUpUseCase) =>
  async (input: unknown): Promise<ReturnType<typeof presenter>> => {
    const parsed = signUpInputSchema.safeParse(input);
    if (!parsed.success) {
      throw new InputParseError("Invalid sign-up input", { cause: parsed.error });
    }
    const result = await signUpUseCase(parsed.data);
    return presenter(result);
  };
