import { createGame } from "./gameFactory";
import type { Game } from "./types";

export const serializeGame = (game: Game): string => {
  // Use a deterministic JSON serializer for snapshots; currently use JSON.stringify
  return JSON.stringify(game);
};

export const deserializeGame = async (serialized: string): Promise<Game> => {
  try {
    return JSON.parse(serialized) as Game;
  } catch (e) {
    // If parsing fails, return a minimal empty game state to avoid crashes
    return await createGame();
  }
};

export const cloneGame = async (game: Game): Promise<Game> => {
  return deserializeGame(serializeGame(game));
};
