import type { AddressInput } from "@/lib/frontend/types";

export interface AddressSuggestion {
  place_id: string;
  formatted_address: string;
  street: string;
  city: string;
  state: string;
  zip: string;
}

/** 演示用地址联想（宿主内嵌场景下由宿主提供；此处为联调 mock） */
export function searchAddressSuggestions(
  street: string,
  city: string,
): AddressSuggestion[] {
  const query = `${street} ${city}`.toLowerCase().trim();
  if (!query) return [];

  const catalog: AddressSuggestion[] = [
    {
      place_id: "place_la_90001",
      formatted_address: "1234 Warehouse Blvd, Los Angeles, CA",
      street: "1234 Warehouse Blvd",
      city: "Los Angeles",
      state: "CA",
      zip: "90001",
    },
    {
      place_id: "place_dallas_75201",
      formatted_address: "5678 Distribution Dr, Dallas, TX",
      street: "5678 Distribution Dr",
      city: "Dallas",
      state: "TX",
      zip: "75201",
    },
  ];

  return catalog.filter((item) => {
    const hay = `${item.street} ${item.city}`.toLowerCase();
    return (
      hay.includes(street.toLowerCase().trim()) ||
      hay.includes(city.toLowerCase().trim())
    );
  });
}

export function toAddressInput(suggestion: AddressSuggestion): AddressInput {
  return {
    ...suggestion,
    selected_from_suggestions: true,
  };
}
