import { jsx as _jsx } from "react/jsx-runtime";
import { StandaloneSearchBox } from "@react-google-maps/api";
import { useState } from "react";
import { Input } from "./input";
/**
 * SearchPlaces component - A Google Places autocomplete input field.
 *
 * This component integrates with Google Maps API to provide address autocomplete functionality.
 * When a user selects a place from the suggestions, it extracts and returns structured address data
 * including street, city, state, neighborhood, postal code, and geographic coordinates.
 *
 * @component
 * @example
 * ```tsx
 * <SearchPlaces
 *   placeholder="Digite o endereço"
 *   onChange={(value) => console.log('Input:', value)}
 *   onPlaceChanged={(place) => {
 *     console.log('Selected place:', place);
 *     // place = {
 *     //   street: "Rua exemplo",
 *     //   city: "São Paulo",
 *     //   state: "São Paulo",
 *     //   neighborhood: "Centro",
 *     //   postalCode: "01310-100",
 *     //   stateShortName: "SP",
 *     //   streetNumber: "123",
 *     //   coordinates: { lat: -23.5505, lng: -46.6333 }
 *     // }
 *   }}
 *   options={{
 *     componentRestrictions: { country: "br" }
 *   }}
 * />
 * ```
 *
 * @param {SearchPlacesProps} props - Component props
 * @param {StandaloneSearchBoxProps["options"]} [props.options] - Google Maps API options for filtering/customizing search results
 * @param {(value: string) => void} [props.onChange] - Handler called when the input text changes
 * @param {(place: PlaceData) => void} [props.onPlaceChanged] - Handler called when a place is selected, receives structured address data
 * @returns {JSX.Element} An input field with Google Places autocomplete functionality
 *
 * @requires Google Maps JavaScript API with Places library loaded
 * @requires @react-google-maps/api package
 */
function SearchPlaces(props) {
    const { onChange, onPlaceChanged, options, ...rest } = props;
    const [searchBox, setSearchBox] = useState(null);
    const handleLoad = (ref) => setSearchBox(ref);
    const handlePlacesChanged = () => {
        const places = searchBox?.getPlaces();
        const place = places ? places[0] : null;
        const address_components = place?.address_components;
        function findData(key) {
            const data = address_components.find((item) => item.types[0] === key);
            if (data)
                return data.long_name;
            return "";
        }
        function findDataShort(key) {
            const data = address_components.find((item) => item.types[0] === key);
            if (data)
                return data.short_name;
            return "";
        }
        function findDataByMultipleTypes(keys) {
            for (const key of keys) {
                const data = address_components.find((item) => item.types.includes(key));
                if (data)
                    return data.long_name;
            }
            return "";
        }
        if (place) {
            const street = findData("route");
            const streetNumber = findData("street_number");
            const neighborhood = findDataByMultipleTypes([
                "sublocality_level_1",
                "sublocality",
                "neighborhood",
            ]);
            const city = findData("administrative_area_level_2");
            const state = findData("administrative_area_level_1");
            const stateShortName = findDataShort("administrative_area_level_1");
            const postalCode = findDataByMultipleTypes([
                "postal_code",
                "postal_code_prefix",
            ]);
            const lat = place.geometry?.location?.lat();
            const lng = place.geometry?.location?.lng();
            const sendPlace = {
                street,
                city,
                state,
                neighborhood,
                postalCode,
                streetNumber,
                stateShortName,
                coordinates: { lat, lng },
            };
            onPlaceChanged && onPlaceChanged(sendPlace);
        }
    };
    return (_jsx(StandaloneSearchBox, { onLoad: handleLoad, onPlacesChanged: handlePlacesChanged, options: options, children: _jsx(Input, { type: "text", autoComplete: "off", onChange: (e) => onChange && onChange(e.target.value), ...rest }) }));
}
export { SearchPlaces };
