import { z } from 'zod';

const SEED_PHRASE_WORD_COUNTS = [12, 18, 24] as const;

const seedPhraseWordCount = (phrase: string): number => {
  return phrase
    .trim()
    .split(/\s+/)
    .filter((word) => word.length > 0).length;
};

export const createWalletSchema = z.object({
  name: z.string().min(1).max(100),
  syncType: z.enum(['created', 'imported']),
  phrase: z
    .string()
    .min(1)
    .refine(
      (value) => {
        const wordCount = seedPhraseWordCount(value);
        return SEED_PHRASE_WORD_COUNTS.includes(wordCount as 12 | 18 | 24);
      },
      {
        message: `Secret phrase must contain ${SEED_PHRASE_WORD_COUNTS.join(', ')} words`,
      },
    ),
  deviceIds: z.array(z.string().uuid()).min(1),
});

export type CreateWalletInput = z.infer<typeof createWalletSchema>;

export const seedPhraseSchema = z.object({
  wordCount: z.union([z.literal(12), z.literal(24)]).default(12),
});

export type SeedPhraseInput = z.infer<typeof seedPhraseSchema>;

export const walletIdParamSchema = z.object({
  id: z.string().uuid(),
});

export const walletAddressParamsSchema = z.object({
  walletId: z.string().uuid(),
});

export const walletNetworkParamsSchema = z.object({
  walletId: z.string().uuid(),
  networkId: z.string().uuid(),
});

export const walletDeviceParamsSchema = z.object({
  deviceId: z.string().uuid(),
});
