"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";
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("div", { className: "relative", children: [
      /* @__PURE__ */ jsx(
        "select",
        {
          id: selectId,
          className: "appearance-none rounded-md border bg-background px-3 py-1 pr-8 text-xs focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-1 disabled:opacity-60",
          value: current,
          onChange: (event) => setLocale(event.target.value),
          disabled: pending,
          children: locales.map((locale) => /* @__PURE__ */ jsx("option", { value: locale, children: languageLabels[locale] }, locale))
        }
      ),
      /* @__PURE__ */ jsx("span", { className: "pointer-events-none absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground", children: /* @__PURE__ */ jsx("svg", { width: "10", height: "10", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", children: /* @__PURE__ */ jsx("path", { d: "M6 9l6 6 6-6" }) }) })
    ] })
  ] });
}
export {
  LanguageSwitcher
};
//# sourceMappingURL=LanguageSwitcher.js.map
