/** 美国 50 州（不含 DC），按州码字母序 */
export const US_STATE_ENTRIES = [
  { code: "AL", name: "Alabama" },
  { code: "AK", name: "Alaska" },
  { code: "AZ", name: "Arizona" },
  { code: "AR", name: "Arkansas" },
  { code: "CA", name: "California" },
  { code: "CO", name: "Colorado" },
  { code: "CT", name: "Connecticut" },
  { code: "DE", name: "Delaware" },
  { code: "FL", name: "Florida" },
  { code: "GA", name: "Georgia" },
  { code: "HI", name: "Hawaii" },
  { code: "ID", name: "Idaho" },
  { code: "IL", name: "Illinois" },
  { code: "IN", name: "Indiana" },
  { code: "IA", name: "Iowa" },
  { code: "KS", name: "Kansas" },
  { code: "KY", name: "Kentucky" },
  { code: "LA", name: "Louisiana" },
  { code: "ME", name: "Maine" },
  { code: "MD", name: "Maryland" },
  { code: "MA", name: "Massachusetts" },
  { code: "MI", name: "Michigan" },
  { code: "MN", name: "Minnesota" },
  { code: "MS", name: "Mississippi" },
  { code: "MO", name: "Missouri" },
  { code: "MT", name: "Montana" },
  { code: "NE", name: "Nebraska" },
  { code: "NV", name: "Nevada" },
  { code: "NH", name: "New Hampshire" },
  { code: "NJ", name: "New Jersey" },
  { code: "NM", name: "New Mexico" },
  { code: "NY", name: "New York" },
  { code: "NC", name: "North Carolina" },
  { code: "ND", name: "North Dakota" },
  { code: "OH", name: "Ohio" },
  { code: "OK", name: "Oklahoma" },
  { code: "OR", name: "Oregon" },
  { code: "PA", name: "Pennsylvania" },
  { code: "RI", name: "Rhode Island" },
  { code: "SC", name: "South Carolina" },
  { code: "SD", name: "South Dakota" },
  { code: "TN", name: "Tennessee" },
  { code: "TX", name: "Texas" },
  { code: "UT", name: "Utah" },
  { code: "VT", name: "Vermont" },
  { code: "VA", name: "Virginia" },
  { code: "WA", name: "Washington" },
  { code: "WV", name: "West Virginia" },
  { code: "WI", name: "Wisconsin" },
  { code: "WY", name: "Wyoming" },
] as const;

export type UsStateCode = (typeof US_STATE_ENTRIES)[number]["code"];

export type UsStateEntry = (typeof US_STATE_ENTRIES)[number];

export const US_STATES: UsStateCode[] = US_STATE_ENTRIES.map((s) => s.code);

const CODE_SET = new Set<string>(US_STATES);
const NAME_TO_CODE = new Map(
  US_STATE_ENTRIES.map((s) => [s.name.toLowerCase(), s.code]),
);

export function formatUsStateOption(entry: UsStateEntry): string {
  return `${entry.code} — ${entry.name}`;
}

/** 按州码或英文全名过滤；空查询返回全部 50 州 */
export function filterUsStateOptions(query: string): UsStateEntry[] {
  const q = query.trim().toLowerCase();
  if (!q) return [...US_STATE_ENTRIES];
  return US_STATE_ENTRIES.filter(
    (s) =>
      s.code.toLowerCase().startsWith(q) ||
      s.name.toLowerCase().includes(q),
  );
}

/** 将手写州码/州名规范为 2 位州码；无法识别时返回 null */
export function normalizeUsStateCode(input: string): UsStateCode | null {
  const trimmed = input.trim();
  if (!trimmed) return null;
  const upper = trimmed.toUpperCase();
  if (/^[A-Z]{2}$/.test(upper) && CODE_SET.has(upper)) {
    return upper as UsStateCode;
  }
  const byName = NAME_TO_CODE.get(trimmed.toLowerCase());
  return byName ?? null;
}

export function isValidUsStateCode(code: string): code is UsStateCode {
  return CODE_SET.has(code.trim().toUpperCase());
}
