"use client";

import { useTranslation } from "react-i18next";
import {
  components,
  type DropdownIndicatorProps,
  type NoticeProps,
} from "react-select";

/**
 * The chevron both `SelectInput` and `GroupedSelectInput` put in their control.
 *
 * It was written out twice, character for character, with a comment in one
 * saying it was there "to match SelectInput" — so the two were already meant
 * to be the same thing. They render on every form in the app; a divergence
 * would have been visible and no one would have known which was right.
 *
 * Typed against react-select's own props rather than `any`: the component only
 * reads `selectProps.menuIsOpen`, which that type already carries.
 */
export function DropdownIndicator<
  Option,
  IsMulti extends boolean,
  Group extends { options: readonly Option[] },
>(props: DropdownIndicatorProps<Option, IsMulti, Group>) {
  return (
    <components.DropdownIndicator {...props}>
      <svg
        width="16"
        height="16"
        viewBox="0 0 16 16"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
        style={{
          transform: props.selectProps.menuIsOpen
            ? "rotate(180deg)"
            : "rotate(0deg)",
          transition: "transform 0.2s ease",
        }}
      >
        <path
          d="M4 6L8 10L12 6"
          stroke="#29246D"
          strokeWidth="2"
          strokeLinecap="round"
          strokeLinejoin="round"
        />
      </svg>
    </components.DropdownIndicator>
  );
}

/**
 * The two notices react-select shows inside an open menu. They were written
 * with `props: any`; `NoticeProps` is the type react-select declares for
 * exactly this slot, and `NoOptionsMessage` only reads `selectProps.isLoading`
 * off it, which that type carries.
 */
export function LoadingMessage<
  Option,
  IsMulti extends boolean,
  Group extends { options: readonly Option[] },
>(props: NoticeProps<Option, IsMulti, Group>) {
  const { t } = useTranslation();
  return (
    <components.LoadingMessage {...props}>
      <div className="flex items-center justify-center gap-2 py-2">
        <div className="w-5 h-5 border-2 border-primary-5 border-t-transparent rounded-full animate-spin" />
        <span className="text-primary-5 text-sm font-medium">
          {t("COMMON.LOADING") || "Loading..."}
        </span>
      </div>
    </components.LoadingMessage>
  );
}

export function NoOptionsMessage<
  Option,
  IsMulti extends boolean,
  Group extends { options: readonly Option[] },
>(props: NoticeProps<Option, IsMulti, Group>) {
  const { t } = useTranslation();
  return (
    <components.NoOptionsMessage {...props}>
      <div className="flex flex-col items-center justify-center gap-2 py-4 text-gray-400">
        <svg className="w-8 h-8 text-gray-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0a2 2 0 01-2 2H6a2 2 0 01-2-2m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4" />
        </svg>
        <span className="text-sm font-medium">
          {props.selectProps.isLoading
            ? t("COMMON.LOADING") || "Loading..."
            : t("COMMON.NO_DATA") || "No options"}
        </span>
      </div>
    </components.NoOptionsMessage>
  );
}
