import VariablePlayClient from "./VariablePlayClient";
import type {
  VariableMaxTermCount,
  VariableProblemCount,
} from "@/store/variableGameStore";

interface VariablePlayPageProps {
  searchParams?: {
    count?: string | string[];
    terms?: string | string[];
  };
}

function parseProblemCount(count?: string | string[]): VariableProblemCount {
  if (Array.isArray(count)) {
    return parseProblemCount(count[0]);
  }

  return count === "5" ? 5 : 10;
}

function parseMaxTermCount(terms?: string | string[]): VariableMaxTermCount {
  if (Array.isArray(terms)) {
    return parseMaxTermCount(terms[0]);
  }

  if (terms === "3") return 3;
  if (terms === "4") return 4;

  return 2;
}

export default function VariablePlayPage({
  searchParams,
}: VariablePlayPageProps) {
  return (
    <VariablePlayClient
      problemCount={parseProblemCount(searchParams?.count)}
      maxTermCount={parseMaxTermCount(searchParams?.terms)}
    />
  );
}
