export const isTrustedDirectionsOrigin = ( origin ) => (
	origin?.source === 'geolocation'
	&& Number.isFinite( Number( origin.lat ) )
	&& Number.isFinite( Number( origin.lng ) )
);

export const resolveAddressOrigin = async ( selected, address, geocode ) => {
	if ( selected && Number.isFinite( Number( selected.lat ) ) && Number.isFinite( Number( selected.lng ) ) ) {
		return { lat: Number( selected.lat ), lng: Number( selected.lng ), source: 'address' };
	}
	const term = String( address || '' ).trim();
	if ( ! term ) return null;
	const resolved = await geocode( term );
	if ( ! resolved || ! Number.isFinite( Number( resolved.lat ) ) || ! Number.isFinite( Number( resolved.lng ) ) ) return null;
	return { lat: Number( resolved.lat ), lng: Number( resolved.lng ), source: 'address' };
};
