/**
 * Resolve a Directions token into one unambiguous action.
 *
 * Existing designs default to the in-locator route panel. External mode omits
 * the interception hook entirely, so the browser only opens Google Maps.
 */
export const directionsAction = ( value, store = {}, item = {} ) => {
	const loc = store.location || ( Array.isArray( store.locations ) ? store.locations[ 0 ] : null ) || {};
	const lat = loc.latitude ?? loc.lat;
	const lng = loc.longitude ?? loc.lng;
	const coordinates = lat != null && lng != null;
	const address = [ loc.address_line1, loc.city, loc.state, store.city ].filter( Boolean ).join( ', ' );
	const destination = coordinates ? `${ lat },${ lng }` : address;

	if ( ! destination ) return value;

	const external = item.directionsBehavior === 'external';
	const out = {
		...value,
		href: `https://www.google.com/maps/dir/?api=1&destination=${ encodeURIComponent( destination ) }`,
		target: '_blank',
	};

	if ( ! external ) {
		out.data = { 'cliq-action': 'directions' };
		if ( coordinates ) {
			out.data[ 'cliq-lat' ] = lat;
			out.data[ 'cliq-lng' ] = lng;
		}
	}

	return out;
};
