import type { ISeoShippingFromWooCommerce } from '../../service/seo/seo.interface'; /** The shipping fields of the settings form, as the zones fill them. */ export interface ShippingFill { rate: string; free_over: string; countries: string; } /** * The form values the zones give, or null when no zone maps. * * @param found - What the route read of WooCommerce's zones. * @return The fields to set, countries spaced as a person types them. */ export const shippingFill = ( found: ISeoShippingFromWooCommerce ): ShippingFill | null => found.rate === '' ? null : { rate: found.rate, free_over: found.free_over, countries: found.countries.split(',').filter(Boolean).join(', '), }; /** * What was filled, in one sentence, or why nothing was. * * @param found - What the route read of WooCommerce's zones. * @return The sentence shown under the button. */ export const shippingFillSummary = ( found: ISeoShippingFromWooCommerce ): string => { const fill = shippingFill(found); if (!fill) { return 'Nothing filled: no zone charges a fixed rate the schema can state.'; } const zones = found.zones.length === 1 ? 'zone' : `${found.zones.length} zones`; const price = fill.rate === '0' ? 'Free shipping' : `A flat ${fill.rate}${fill.free_over ? `, free from ${fill.free_over},` : ''}`; return `${price} to ${fill.countries}, from the ${zones} ${found.zones.join(', ')}. Check it and save the changes to put it in the schema.`; };