Identifying delivery options chosen by customer

Hi, I’m into Shopify application development for the past 3 months and I’m build an application now. I have written code for the discount function creation using Shopify Functions. To make that function apply discounts to shipping at checkout, I have written a logic and it is getting failed in one scenario. I found that it is because of the delivery option index which I’m using. By default my code takes 0th index deliveryGroup’s first delivery option alone.

  const shippingCost = cart.deliveryGroups[0]?.deliveryOptions[0]?.cost?.amount || 0;

this code line will take only the first deliveryOption. Whereas, if a user has multiple deliveryOptions?, this will not be suitable. So to avoid such case, I need to find out which deliveryOption does the user chooses in checkout and I have to use corresponding shipping cost of that deliveryOption. Could someone help me on how this can be done…