Apply Shipping Discount to Total Shipping Cost

So I’m developing a shopify shipping function to apply discounts based on product type and shipping location.

Currently, we have 3 locations and 3 product types.

Each location has $50 shipping fee.

So let’s say, I have 3 products with different locations in my cart and product type 1 subtotal exceeds $60, $20 will be deducted from its location shipping fee. Total shipping fee should be $130, but currently $20 off is being applied to every shipping location.

Is there a way to deduct the discount from the total shipping fee, instead of targeting each deliveryOptions? Cos product types can be present in each location so I can’t think of a way to just target specific delivery ID.