import { ZodError } from "zod";

/**
 * @ignore
 */
export const apiResponse = {
  successful: (message: string, data?: any, meta?: {}) => ({
    status: 200,
    message,
    data,
    successful: true,
    meta: {
      timestamp: Date.now(),
      ...meta,
    },
  }),
  created: (message: string, data?: any) => ({
    status: 201,
    message,
    data,
    successful: true,
    meta: {
      timestamp: Date.now(),
    },
  }),
  error: (err: Error) => ({
    // @ts-ignore
    status: err["status"] ?? 400,
    message: err.message,
    error: err.name,
    successful: false,
    meta: {
      timestamp: Date.now(),
    },
  }),
  internalError: () => ({
    status: 500,
    message: "An Internal Server Error has occured...",
    error: "InternalServerError",
    successful: false,
    meta: {
      timestamp: Date.now(),
    },
  }),
  zodError: (err: ZodError) => {
    const data = JSON.parse(err.message);
    return {
      status: 400,
      message: "TypeError",
      error: data,
      successful: false,
      meta: {
        timestamp: Date.now(),
      },
    };
  },
};
