/** * Shared (free-eligible) accessible-name construction for swatches. * * Out-of-stock swatches are conveyed visually by the cross / blur treatments, * which assistive technology cannot perceive. `aria-disabled` alone says the * control is inoperable, not WHY. Suffixing the accessible name keeps the * reason in the name so every renderer announces it identically. * * Strings arrive translated from PHP via `b3WvsData.i18n` (the frontend bundle * deliberately does not depend on the `wp-i18n` script), with an English * fallback for the untranslated / unlocalized case. * * @package B3VariationSwatches */ const FALLBACK_OUT_OF_STOCK = "Out of stock"; /** * Read a localized frontend string, falling back to English. */ export const getSwatchI18n = ( key: string, fallback: string, data: { i18n?: Record } | undefined = typeof b3WvsData !== "undefined" ? b3WvsData : undefined, ): string => { const value = data?.i18n?.[key]; return typeof value === "string" && value.trim() !== "" ? value : fallback; }; /** * Build the accessible name for one swatch. * * @param label Visible/term label for the option. * @param outOfStock Whether the option resolved to out of stock. */ export const buildSwatchAccessibleName = ( label: string, outOfStock: boolean, ): string => { const name = String(label ?? "").trim(); if (!outOfStock) { return name; } const suffix = getSwatchI18n("out_of_stock", FALLBACK_OUT_OF_STOCK); return name ? `${name} (${suffix})` : suffix; };