import i18n from "./i18n";
import Cookies from "js-cookie";
import { ZodObject } from "zod";
import type { ZodRawShape } from "zod";

export const changeTranslation = () => {
  const clientLng = i18n.language;

  if (clientLng === "en") {
    i18n.changeLanguage("de");
    Cookies.set("language", "de");
  } else if (clientLng === "de") {
    i18n.changeLanguage("en");
    Cookies.set("language", "en");
  } else {
    alert("Cannot change language.");
  }
};

/**
 * Validates a single field against the full Zod schema.
 * Returns the first error message for that field, or undefined if valid.
 */
export function validateFieldWithZod(
  schema: ZodObject<ZodRawShape>,
  fieldName: string,
  allValues: Record<string, unknown>,
): string | undefined {
  const result = schema.safeParse(allValues);
  if (result.success) return undefined;
  const issue = result.error.issues.find(
    (i) => i.path.length === 1 && i.path[0] === fieldName,
  );
  return issue?.message;
}

export const PAYMENT_METHODS = ["bar", "paypal", "andere"] as const;
export const PAYMENT_LABELS: Record<string, string> = {
  bar: "Cash",
  paypal: "PayPal",
  andere: "Transfer",
};