"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useId, useTransition } from "react";
import { useLocale, useT } from "@open-mercato/shared/lib/i18n/context";
import { useRouter } from "next/navigation";
import { locales } from "@open-mercato/shared/lib/i18n/config";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "@open-mercato/ui/primitives/select";
function LanguageSwitcher() {
  const current = useLocale();
  const t = useT();
  const router = useRouter();
  const [pending, startTransition] = useTransition();
  const selectId = useId();
  const languageLabels = {
    en: t("common.languages.english", "English"),
    pl: t("common.languages.polish", "Polski"),
    es: t("common.languages.spanish", "Espa\xF1ol"),
    de: t("common.languages.german", "Deutsch")
  };
  async function setLocale(locale) {
    if (locale === current) return;
    try {
      const res = await fetch("/api/auth/locale", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ locale })
      });
      if (!res.ok) return;
      startTransition(() => router.refresh());
      try {
        window.dispatchEvent(new Event("om:refresh-sidebar"));
      } catch {
      }
    } catch {
    }
  }
  return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 text-xs text-muted-foreground", children: [
    /* @__PURE__ */ jsx("label", { htmlFor: selectId, children: t("common.language") }),
    /* @__PURE__ */ jsxs(
      Select,
      {
        value: current,
        onValueChange: (value) => setLocale(value),
        disabled: pending,
        children: [
          /* @__PURE__ */ jsx(SelectTrigger, { id: selectId, size: "sm", children: /* @__PURE__ */ jsx(SelectValue, {}) }),
          /* @__PURE__ */ jsx(SelectContent, { children: locales.map((locale) => /* @__PURE__ */ jsx(SelectItem, { value: locale, children: languageLabels[locale] }, locale)) })
        ]
      }
    )
  ] });
}
export {
  LanguageSwitcher
};
//# sourceMappingURL=LanguageSwitcher.js.map
