import {
  countries
} from "./chunk-JRTZWK77.js";

// src/components/inputs/country-select/country-select.tsx
import {
  forwardRef,
  useImperativeHandle,
  useMemo,
  useRef
} from "react";
import { useTranslation } from "react-i18next";
import { Select } from "@medusajs/ui";
import { jsx, jsxs } from "react/jsx-runtime";
var getDisplayNames = (language) => {
  try {
    return new Intl.DisplayNames([language], { type: "region" });
  } catch {
    return null;
  }
};
var CountrySelect = forwardRef(({ disabled, placeholder, defaultValue, onChange, ...field }, ref) => {
  const { t, i18n } = useTranslation();
  const innerRef = useRef(null);
  useImperativeHandle(ref, () => innerRef.current);
  const localizedCountries = useMemo(() => {
    const displayNames = getDisplayNames(i18n.language);
    const collator = new Intl.Collator(i18n.language, { sensitivity: "base" });
    return countries.map((country) => ({
      ...country,
      localized_name: displayNames?.of(country.iso_2.toUpperCase()) ?? country.display_name
    })).sort((a, b) => collator.compare(a.localized_name, b.localized_name));
  }, [i18n.language]);
  return /* @__PURE__ */ jsx("div", { className: "relative", children: /* @__PURE__ */ jsxs(
    Select,
    {
      ...field,
      value: field.value ? field.value?.toLowerCase() : void 0,
      onValueChange: onChange,
      defaultValue: defaultValue ? defaultValue.toLowerCase() : void 0,
      disabled,
      children: [
        /* @__PURE__ */ jsx(Select.Trigger, { ref: innerRef, className: "w-full", children: /* @__PURE__ */ jsx(
          Select.Value,
          {
            placeholder: placeholder || t("fields.selectCountry")
          }
        ) }),
        /* @__PURE__ */ jsx(Select.Content, { children: localizedCountries.map((country) => /* @__PURE__ */ jsx(
          Select.Item,
          {
            value: country.iso_2.toLowerCase(),
            children: country.localized_name
          },
          country.iso_2
        )) })
      ]
    }
  ) });
});
CountrySelect.displayName = "CountrySelect";

export {
  CountrySelect
};
