import { getExampleNumber, type CountryCode } from 'libphonenumber-js/mobile/es6' import examples from 'libphonenumber-js/mobile/examples' const maskCache = new Map() /** IMask pattern with space grouping derived from libphonenumber mobile example numbers. */ export const getPhoneMaskForCountry = (countryIso2: string): string | undefined => { const country = countryIso2.trim().toUpperCase() as CountryCode const cached = maskCache.get(country) if (cached) { return cached } const example = getExampleNumber(country, examples) if (!example) { return undefined } const groups = example .formatNational() .split(/\D+/) .filter(Boolean) .map((part) => part.length) const mask = groups.map((size) => '0'.repeat(size)).join(' ') maskCache.set(country, mask) return mask }