import { z } from 'zod';
function createResponseSchema<T extends z.ZodTypeAny>(data: T) {
  return z.union([
    z
      .object({
        code: z.literal(200),
        msg: z.string(),
        time: z.coerce.date()
      })
      .extend({ data: data })
      .transform((raw) =>
        Object.assign(raw, {
          type: 'success' as const
        })
      ),
    z
      .object({
        code: z.number(),
        msg: z.string(),
        time: z.coerce.date()
      })
      .transform((raw) =>
        Object.assign(raw, {
          type: 'error' as const
        })
      )
  ]);
}
export type SucceedResponse<T> = T extends { type: 'success' } ? T : never;
export type ErrorResponse<T> = Exclude<SucceedResponse<T>, T>;
const ordinaryResponse = createResponseSchema(z.literal(true));
export const loginResponseSchema = ordinaryResponse;
export const registerResponseSchema = ordinaryResponse;
const idAndNameSchema = z.object({
  id: z.number(),
  name: z.string()
});
export interface Route extends z.infer<typeof idAndNameSchema> {
  component: string;
  children: Route[];
}
const routeResponsesSchema: z.ZodSchema<Route[]> = z.lazy(() =>
  idAndNameSchema.extend({ component: z.string(), children: routeResponsesSchema }).array()
);
export const userInfoResponseSchema = createResponseSchema(
  idAndNameSchema.extend({
    avatar: z.string(),
    auth: idAndNameSchema.extend({
      permissions: idAndNameSchema.extend({ routers: routeResponsesSchema }).array()
    })
  })
);
