Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

18.2 KB
Raw Blame
// The area codes a new number can be bought in, per country: every geographic area code in service in the US (the
// 50 states and DC), Canada and Puerto Rico, with a main city and the state, province or territory. Taken from
// NANPA's public NPA report (reports.nanpa.com/public/npa_report.csv): General Purpose Codes, use G, in service;
// US codes from the report of 13 Nov 2026 (country US, Puerto Rico and the other territories left out), Canadian
// codes (country CANADA) and Puerto Rico's (country US, location PR) from the report of 15 Nov 2026. Toll-free
// (8XX), N11, 555, the 5XX personal codes, Canada's non-geographic 600/622/633 and codes not in service yet are not
// in it, nor the Caribbean (Jamaica, the Dominican Republic, the Bahamas …) and the other US territories. An
// overlay code has the city of the code it overlays. NANP area codes are unique across countries, so a code alone
// names its country. To update: take the codes from a newer report the same way and give a new code the city of
// its overlay complex.

/** The countries numbers can be bought in (ISO 3166-1 alpha-2). */
export const NUMBER_BUY_COUNTRIES = ['US', 'CA', 'PR'] as const
export type NumberBuyCountry = (typeof NUMBER_BUY_COUNTRIES)[number]

/**
 * `state`: the two letters of the US state, Canadian province or territory, or PR; empty for a German area code (one
 * of a buy offer, number-buy.ts). `code`: without the trunk 0 (Heidelberg 06221: `6221`).
 */
export type AreaCode = { code: string; city: string; state: string; country: string }

type Row = readonly [code: string, city: string, state: string]

const US_ROWS: readonly Row[] = [
  ['201', 'Jersey City', 'NJ'],
  ['202', 'Washington', 'DC'],
  ['203', 'New Haven', 'CT'],
  ['205', 'Birmingham', 'AL'],
  ['206', 'Seattle', 'WA'],
  ['207', 'Portland', 'ME'],
  ['208', 'Boise', 'ID'],
  ['209', 'Stockton', 'CA'],
  ['210', 'San Antonio', 'TX'],
  ['212', 'New York', 'NY'],
  ['213', 'Los Angeles', 'CA'],
  ['214', 'Dallas', 'TX'],
  ['215', 'Philadelphia', 'PA'],
  ['216', 'Cleveland', 'OH'],
  ['217', 'Springfield', 'IL'],
  ['218', 'Duluth', 'MN'],
  ['219', 'Gary', 'IN'],
  ['220', 'Zanesville', 'OH'],
  ['223', 'Harrisburg', 'PA'],
  ['224', 'Evanston', 'IL'],
  ['225', 'Baton Rouge', 'LA'],
  ['227', 'Rockville', 'MD'],
  ['228', 'Gulfport', 'MS'],
  ['229', 'Albany', 'GA'],
  ['231', 'Muskegon', 'MI'],
  ['234', 'Akron', 'OH'],
  ['235', 'Columbia', 'MO'],
  ['239', 'Fort Myers', 'FL'],
  ['240', 'Rockville', 'MD'],
  ['248', 'Troy', 'MI'],
  ['251', 'Mobile', 'AL'],
  ['252', 'Greenville', 'NC'],
  ['253', 'Tacoma', 'WA'],
  ['254', 'Waco', 'TX'],
  ['256', 'Huntsville', 'AL'],
  ['260', 'Fort Wayne', 'IN'],
  ['262', 'Waukesha', 'WI'],
  ['267', 'Philadelphia', 'PA'],
  ['269', 'Kalamazoo', 'MI'],
  ['270', 'Bowling Green', 'KY'],
  ['272', 'Scranton', 'PA'],
  ['274', 'Green Bay', 'WI'],
  ['276', 'Bristol', 'VA'],
  ['279', 'Sacramento', 'CA'],
  ['281', 'Houston', 'TX'],
  ['283', 'Cincinnati', 'OH'],
  ['301', 'Rockville', 'MD'],
  ['302', 'Wilmington', 'DE'],
  ['303', 'Denver', 'CO'],
  ['304', 'Charleston', 'WV'],
  ['305', 'Miami', 'FL'],
  ['307', 'Cheyenne', 'WY'],
  ['308', 'Grand Island', 'NE'],
  ['309', 'Peoria', 'IL'],
  ['310', 'West Los Angeles', 'CA'],
  ['312', 'Chicago', 'IL'],
  ['313', 'Detroit', 'MI'],
  ['314', 'St. Louis', 'MO'],
  ['315', 'Syracuse', 'NY'],
  ['316', 'Wichita', 'KS'],
  ['317', 'Indianapolis', 'IN'],
  ['318', 'Shreveport', 'LA'],
  ['319', 'Cedar Rapids', 'IA'],
  ['320', 'St. Cloud', 'MN'],
  ['321', 'Orlando', 'FL'],
  ['323', 'Los Angeles', 'CA'],
  ['324', 'Jacksonville', 'FL'],
  ['325', 'Abilene', 'TX'],
  ['326', 'Dayton', 'OH'],
  ['327', 'Jonesboro', 'AR'],
  ['329', 'Poughkeepsie', 'NY'],
  ['330', 'Akron', 'OH'],
  ['331', 'Aurora', 'IL'],
  ['332', 'New York', 'NY'],
  ['334', 'Montgomery', 'AL'],
  ['336', 'Greensboro', 'NC'],
  ['337', 'Lafayette', 'LA'],
  ['339', 'Waltham', 'MA'],
  ['341', 'Oakland', 'CA'],
  ['346', 'Houston', 'TX'],
  ['347', 'Brooklyn', 'NY'],
  ['350', 'Stockton', 'CA'],
  ['351', 'Lowell', 'MA'],
  ['352', 'Gainesville', 'FL'],
  ['353', 'Madison', 'WI'],
  ['357', 'Fresno', 'CA'],
  ['360', 'Vancouver', 'WA'],
  ['361', 'Corpus Christi', 'TX'],
  ['363', 'Hempstead', 'NY'],
  ['364', 'Bowling Green', 'KY'],
  ['369', 'Santa Rosa', 'CA'],
  ['380', 'Columbus', 'OH'],
  ['385', 'Salt Lake City', 'UT'],
  ['386', 'Daytona Beach', 'FL'],
  ['401', 'Providence', 'RI'],
  ['402', 'Omaha', 'NE'],
  ['404', 'Atlanta', 'GA'],
  ['405', 'Oklahoma City', 'OK'],
  ['406', 'Billings', 'MT'],
  ['407', 'Orlando', 'FL'],
  ['408', 'San Jose', 'CA'],
  ['409', 'Beaumont', 'TX'],
  ['410', 'Baltimore', 'MD'],
  ['412', 'Pittsburgh', 'PA'],
  ['413', 'Springfield', 'MA'],
  ['414', 'Milwaukee', 'WI'],
  ['415', 'San Francisco', 'CA'],
  ['417', 'Springfield', 'MO'],
  ['419', 'Toledo', 'OH'],
  ['423', 'Chattanooga', 'TN'],
  ['424', 'West Los Angeles', 'CA'],
  ['425', 'Bellevue', 'WA'],
  ['430', 'Tyler', 'TX'],
  ['432', 'Midland', 'TX'],
  ['434', 'Lynchburg', 'VA'],
  ['435', 'St. George', 'UT'],
  ['436', 'Parma', 'OH'],
  ['440', 'Parma', 'OH'],
  ['442', 'Oceanside', 'CA'],
  ['443', 'Baltimore', 'MD'],
  ['445', 'Philadelphia', 'PA'],
  ['447', 'Springfield', 'IL'],
  ['448', 'Tallahassee', 'FL'],
  ['457', 'Shreveport', 'LA'],
  ['458', 'Eugene', 'OR'],
  ['463', 'Indianapolis', 'IN'],
  ['464', 'Cicero', 'IL'],
  ['465', 'Brooklyn', 'NY'],
  ['469', 'Dallas', 'TX'],
  ['470', 'Atlanta', 'GA'],
  ['471', 'Tupelo', 'MS'],
  ['472', 'Fayetteville', 'NC'],
  ['475', 'New Haven', 'CT'],
  ['478', 'Macon', 'GA'],
  ['479', 'Fort Smith', 'AR'],
  ['480', 'Mesa', 'AZ'],
  ['483', 'Montgomery', 'AL'],
  ['484', 'Allentown', 'PA'],
  ['501', 'Little Rock', 'AR'],
  ['502', 'Louisville', 'KY'],
  ['503', 'Portland', 'OR'],
  ['504', 'New Orleans', 'LA'],
  ['505', 'Albuquerque', 'NM'],
  ['507', 'Rochester', 'MN'],
  ['508', 'Worcester', 'MA'],
  ['509', 'Spokane', 'WA'],
  ['510', 'Oakland', 'CA'],
  ['512', 'Austin', 'TX'],
  ['513', 'Cincinnati', 'OH'],
  ['515', 'Des Moines', 'IA'],
  ['516', 'Hempstead', 'NY'],
  ['517', 'Lansing', 'MI'],
  ['518', 'Albany', 'NY'],
  ['520', 'Tucson', 'AZ'],
  ['530', 'Chico', 'CA'],
  ['531', 'Omaha', 'NE'],
  ['534', 'Eau Claire', 'WI'],
  ['539', 'Tulsa', 'OK'],
  ['540', 'Roanoke', 'VA'],
  ['541', 'Eugene', 'OR'],
  ['551', 'Jersey City', 'NJ'],
  ['557', 'St. Louis', 'MO'],
  ['559', 'Fresno', 'CA'],
  ['561', 'West Palm Beach', 'FL'],
  ['562', 'Long Beach', 'CA'],
  ['563', 'Davenport', 'IA'],
  ['564', 'Seattle', 'WA'],
  ['567', 'Toledo', 'OH'],
  ['570', 'Scranton', 'PA'],
  ['571', 'Arlington', 'VA'],
  ['572', 'Oklahoma City', 'OK'],
  ['573', 'Columbia', 'MO'],
  ['574', 'South Bend', 'IN'],
  ['575', 'Las Cruces', 'NM'],
  ['580', 'Lawton', 'OK'],
  ['582', 'Erie', 'PA'],
  ['585', 'Rochester', 'NY'],
  ['586', 'Warren', 'MI'],
  ['601', 'Jackson', 'MS'],
  ['602', 'Phoenix', 'AZ'],
  ['603', 'Manchester', 'NH'],
  ['605', 'Sioux Falls', 'SD'],
  ['606', 'Ashland', 'KY'],
  ['607', 'Binghamton', 'NY'],
  ['608', 'Madison', 'WI'],
  ['609', 'Trenton', 'NJ'],
  ['610', 'Allentown', 'PA'],
  ['612', 'Minneapolis', 'MN'],
  ['614', 'Columbus', 'OH'],
  ['615', 'Nashville', 'TN'],
  ['616', 'Grand Rapids', 'MI'],
  ['617', 'Boston', 'MA'],
  ['618', 'Belleville', 'IL'],
  ['619', 'San Diego', 'CA'],
  ['620', 'Hutchinson', 'KS'],
  ['621', 'Houston', 'TX'],
  ['623', 'Glendale', 'AZ'],
  ['624', 'Buffalo', 'NY'],
  ['626', 'Pasadena', 'CA'],
  ['628', 'San Francisco', 'CA'],
  ['629', 'Nashville', 'TN'],
  ['630', 'Aurora', 'IL'],
  ['631', 'Islip', 'NY'],
  ['636', 'St. Charles', 'MO'],
  ['640', 'Trenton', 'NJ'],
  ['641', 'Mason City', 'IA'],
  ['645', 'Miami', 'FL'],
  ['646', 'New York', 'NY'],
  ['650', 'San Mateo', 'CA'],
  ['651', 'St. Paul', 'MN'],
  ['656', 'Tampa', 'FL'],
  ['657', 'Anaheim', 'CA'],
  ['659', 'Birmingham', 'AL'],
  ['660', 'Sedalia', 'MO'],
  ['661', 'Bakersfield', 'CA'],
  ['662', 'Tupelo', 'MS'],
  ['667', 'Baltimore', 'MD'],
  ['669', 'San Jose', 'CA'],
  ['678', 'Atlanta', 'GA'],
  ['679', 'Detroit', 'MI'],
  ['680', 'Syracuse', 'NY'],
  ['681', 'Charleston', 'WV'],
  ['682', 'Fort Worth', 'TX'],
  ['686', 'Richmond', 'VA'],
  ['689', 'Orlando', 'FL'],
  ['701', 'Fargo', 'ND'],
  ['702', 'Las Vegas', 'NV'],
  ['703', 'Arlington', 'VA'],
  ['704', 'Charlotte', 'NC'],
  ['706', 'Augusta', 'GA'],
  ['707', 'Santa Rosa', 'CA'],
  ['708', 'Cicero', 'IL'],
  ['712', 'Sioux City', 'IA'],
  ['713', 'Houston', 'TX'],
  ['714', 'Anaheim', 'CA'],
  ['715', 'Eau Claire', 'WI'],
  ['716', 'Buffalo', 'NY'],
  ['717', 'Harrisburg', 'PA'],
  ['718', 'Brooklyn', 'NY'],
  ['719', 'Colorado Springs', 'CO'],
  ['720', 'Denver', 'CO'],
  ['724', 'Greensburg', 'PA'],
  ['725', 'Las Vegas', 'NV'],
  ['726', 'San Antonio', 'TX'],
  ['727', 'St. Petersburg', 'FL'],
  ['728', 'West Palm Beach', 'FL'],
  ['729', 'Chattanooga', 'TN'],
  ['730', 'Belleville', 'IL'],
  ['731', 'Jackson', 'TN'],
  ['732', 'New Brunswick', 'NJ'],
  ['734', 'Ann Arbor', 'MI'],
  ['737', 'Austin', 'TX'],
  ['738', 'Los Angeles', 'CA'],
  ['740', 'Zanesville', 'OH'],
  ['743', 'Greensboro', 'NC'],
  ['747', 'Burbank', 'CA'],
  ['748', 'Fort Collins', 'CO'],
  ['754', 'Fort Lauderdale', 'FL'],
  ['757', 'Virginia Beach', 'VA'],
  ['760', 'Oceanside', 'CA'],
  ['762', 'Augusta', 'GA'],
  ['763', 'Plymouth', 'MN'],
  ['765', 'Lafayette', 'IN'],
  ['769', 'Jackson', 'MS'],
  ['770', 'Atlanta', 'GA'],
  ['771', 'Washington', 'DC'],
  ['772', 'Port St. Lucie', 'FL'],
  ['773', 'Chicago', 'IL'],
  ['774', 'Worcester', 'MA'],
  ['775', 'Reno', 'NV'],
  ['779', 'Rockford', 'IL'],
  ['781', 'Waltham', 'MA'],
  ['785', 'Topeka', 'KS'],
  ['786', 'Miami', 'FL'],
  ['801', 'Salt Lake City', 'UT'],
  ['802', 'Burlington', 'VT'],
  ['803', 'Columbia', 'SC'],
  ['804', 'Richmond', 'VA'],
  ['805', 'Santa Barbara', 'CA'],
  ['806', 'Lubbock', 'TX'],
  ['808', 'Honolulu', 'HI'],
  ['810', 'Flint', 'MI'],
  ['812', 'Evansville', 'IN'],
  ['813', 'Tampa', 'FL'],
  ['814', 'Erie', 'PA'],
  ['815', 'Rockford', 'IL'],
  ['816', 'Kansas City', 'MO'],
  ['817', 'Fort Worth', 'TX'],
  ['818', 'Burbank', 'CA'],
  ['820', 'Santa Barbara', 'CA'],
  ['821', 'Greenville', 'SC'],
  ['826', 'Roanoke', 'VA'],
  ['828', 'Asheville', 'NC'],
  ['830', 'New Braunfels', 'TX'],
  ['831', 'Salinas', 'CA'],
  ['832', 'Houston', 'TX'],
  ['835', 'Allentown', 'PA'],
  ['837', 'Chico', 'CA'],
  ['838', 'Albany', 'NY'],
  ['839', 'Columbia', 'SC'],
  ['840', 'San Bernardino', 'CA'],
  ['843', 'Charleston', 'SC'],
  ['845', 'Poughkeepsie', 'NY'],
  ['847', 'Evanston', 'IL'],
  ['848', 'New Brunswick', 'NJ'],
  ['850', 'Tallahassee', 'FL'],
  ['854', 'Charleston', 'SC'],
  ['856', 'Camden', 'NJ'],
  ['857', 'Boston', 'MA'],
  ['858', 'San Diego', 'CA'],
  ['859', 'Lexington', 'KY'],
  ['860', 'Hartford', 'CT'],
  ['861', 'Peoria', 'IL'],
  ['862', 'Newark', 'NJ'],
  ['863', 'Lakeland', 'FL'],
  ['864', 'Greenville', 'SC'],
  ['865', 'Knoxville', 'TN'],
  ['870', 'Jonesboro', 'AR'],
  ['872', 'Chicago', 'IL'],
  ['878', 'Pittsburgh', 'PA'],
  ['901', 'Memphis', 'TN'],
  ['903', 'Tyler', 'TX'],
  ['904', 'Jacksonville', 'FL'],
  ['906', 'Marquette', 'MI'],
  ['907', 'Anchorage', 'AK'],
  ['908', 'Elizabeth', 'NJ'],
  ['909', 'San Bernardino', 'CA'],
  ['910', 'Fayetteville', 'NC'],
  ['912', 'Savannah', 'GA'],
  ['913', 'Kansas City', 'KS'],
  ['914', 'Yonkers', 'NY'],
  ['915', 'El Paso', 'TX'],
  ['916', 'Sacramento', 'CA'],
  ['917', 'New York', 'NY'],
  ['918', 'Tulsa', 'OK'],
  ['919', 'Raleigh', 'NC'],
  ['920', 'Green Bay', 'WI'],
  ['924', 'Rochester', 'MN'],
  ['925', 'Concord', 'CA'],
  ['928', 'Flagstaff', 'AZ'],
  ['929', 'Brooklyn', 'NY'],
  ['930', 'Evansville', 'IN'],
  ['931', 'Clarksville', 'TN'],
  ['934', 'Islip', 'NY'],
  ['936', 'Conroe', 'TX'],
  ['937', 'Dayton', 'OH'],
  ['938', 'Huntsville', 'AL'],
  ['940', 'Denton', 'TX'],
  ['941', 'Sarasota', 'FL'],
  ['943', 'Atlanta', 'GA'],
  ['945', 'Dallas', 'TX'],
  ['947', 'Troy', 'MI'],
  ['948', 'Virginia Beach', 'VA'],
  ['949', 'Irvine', 'CA'],
  ['951', 'Riverside', 'CA'],
  ['952', 'Bloomington', 'MN'],
  ['954', 'Fort Lauderdale', 'FL'],
  ['956', 'McAllen', 'TX'],
  ['959', 'Hartford', 'CT'],
  ['970', 'Fort Collins', 'CO'],
  ['971', 'Portland', 'OR'],
  ['972', 'Dallas', 'TX'],
  ['973', 'Newark', 'NJ'],
  ['975', 'Kansas City', 'MO'],
  ['978', 'Lowell', 'MA'],
  ['979', 'College Station', 'TX'],
  ['980', 'Charlotte', 'NC'],
  ['983', 'Denver', 'CO'],
  ['984', 'Raleigh', 'NC'],
  ['985', 'Houma', 'LA'],
  ['986', 'Boise', 'ID'],
  ['989', 'Saginaw', 'MI'],
]

const CA_ROWS: readonly Row[] = [
  ['204', 'Winnipeg', 'MB'],
  ['226', 'London', 'ON'],
  ['236', 'Vancouver', 'BC'],
  ['249', 'Sudbury', 'ON'],
  ['250', 'Victoria', 'BC'],
  ['257', 'Vancouver', 'BC'],
  ['263', 'Montreal', 'QC'],
  ['289', 'Hamilton', 'ON'],
  ['306', 'Saskatoon', 'SK'],
  ['343', 'Ottawa', 'ON'],
  ['354', 'Laval', 'QC'],
  ['365', 'Hamilton', 'ON'],
  ['367', 'Quebec City', 'QC'],
  ['368', 'Edmonton', 'AB'],
  ['382', 'London', 'ON'],
  ['403', 'Calgary', 'AB'],
  ['416', 'Toronto', 'ON'],
  ['418', 'Quebec City', 'QC'],
  ['428', 'Moncton', 'NB'],
  ['431', 'Winnipeg', 'MB'],
  ['437', 'Toronto', 'ON'],
  ['438', 'Montreal', 'QC'],
  ['450', 'Laval', 'QC'],
  ['468', 'Gatineau', 'QC'],
  ['474', 'Saskatoon', 'SK'],
  ['506', 'Moncton', 'NB'],
  ['514', 'Montreal', 'QC'],
  ['519', 'London', 'ON'],
  ['548', 'London', 'ON'],
  ['579', 'Laval', 'QC'],
  ['581', 'Quebec City', 'QC'],
  ['584', 'Winnipeg', 'MB'],
  ['587', 'Edmonton', 'AB'],
  ['604', 'Vancouver', 'BC'],
  ['613', 'Ottawa', 'ON'],
  ['639', 'Saskatoon', 'SK'],
  ['647', 'Toronto', 'ON'],
  ['672', 'Vancouver', 'BC'],
  ['683', 'Sudbury', 'ON'],
  ['705', 'Sudbury', 'ON'],
  ['709', "St. John's", 'NL'],
  ['742', 'Hamilton', 'ON'],
  ['753', 'Ottawa', 'ON'],
  ['778', 'Vancouver', 'BC'],
  ['780', 'Edmonton', 'AB'],
  ['782', 'Halifax', 'NS'],
  ['807', 'Thunder Bay', 'ON'],
  ['819', 'Gatineau', 'QC'],
  ['825', 'Edmonton', 'AB'],
  ['867', 'Whitehorse', 'YT'],
  ['873', 'Gatineau', 'QC'],
  ['879', "St. John's", 'NL'],
  ['902', 'Halifax', 'NS'],
  ['905', 'Hamilton', 'ON'],
  ['942', 'Toronto', 'ON'],
]

const PR_ROWS: readonly Row[] = [
  ['787', 'San Juan', 'PR'],
  ['939', 'San Juan', 'PR'],
]

const rows = (country: NumberBuyCountry, list: readonly Row[]): readonly AreaCode[] =>
  list.map(([code, city, state]) => ({ code, city, state, country }))

/** Each country's area codes, sorted by code. */
export const AREA_CODES: Readonly<Record<NumberBuyCountry, readonly AreaCode[]>> = {
  US: rows('US', US_ROWS),
  CA: rows('CA', CA_ROWS),
  PR: rows('PR', PR_ROWS),
}

/** Every area code a number can be bought in, sorted by code. */
export const BUYABLE_AREA_CODES: readonly AreaCode[] = Object.values(AREA_CODES)
  .flat()
  .sort((a, b) => a.code.localeCompare(b.code))

const BY_CODE = new Map(BUYABLE_AREA_CODES.map(a => [a.code, a]))

/** The area code from the lists (with its country), or undefined for one a new number can't be bought in. */
export const buyableAreaCode = (code: string): AreaCode | undefined => BY_CODE.get(code)

/** Whether numbers can be bought in `country` (ISO 3166-1 alpha-2, any case): the US, Canada and Puerto Rico. */
export const canBuyNumbersIn = (country: string): boolean =>
  (NUMBER_BUY_COUNTRIES as readonly string[]).includes(country.trim().toUpperCase())

/**
 * The area codes a workspace in `country` buys a number in (workspace_settings.country, resolveWorkspaceCountry):
 * its own country's; none outside NUMBER_BUY_COUNTRIES, where the business connects a number it already has in its
 * carrier account (phoneNumber.setup without purchase), and the server refuses a purchase.
 */
export const areaCodesIn = (country: string): readonly AreaCode[] => {
  const code = country.trim().toUpperCase()
  return canBuyNumbersIn(code) ? AREA_CODES[code as NumberBuyCountry] : []
}

/** "503 · Portland, OR", "416 · Toronto, ON", "787 · San Juan, PR"; "06221 · Heidelberg" in Germany. */
export const areaCodeLabel = (a: AreaCode): string =>
  a.country === 'DE' ? `0${a.code}${a.city ? ` · ${a.city}` : ''}` : `${a.code} · ${a.city}, ${a.state}`

/** The places by their two letters, so a search for "oregon" finds OR and "ontario" ON. */
export const REGION_NAMES: Readonly<Record<string, string>> = {
  AK: 'Alaska',
  AL: 'Alabama',
  AR: 'Arkansas',
  AZ: 'Arizona',
  CA: 'California',
  CO: 'Colorado',
  CT: 'Connecticut',
  DC: 'District of Columbia',
  DE: 'Delaware',
  FL: 'Florida',
  GA: 'Georgia',
  HI: 'Hawaii',
  IA: 'Iowa',
  ID: 'Idaho',
  IL: 'Illinois',
  IN: 'Indiana',
  KS: 'Kansas',
  KY: 'Kentucky',
  LA: 'Louisiana',
  MA: 'Massachusetts',
  MD: 'Maryland',
  ME: 'Maine',
  MI: 'Michigan',
  MN: 'Minnesota',
  MO: 'Missouri',
  MS: 'Mississippi',
  MT: 'Montana',
  NC: 'North Carolina',
  ND: 'North Dakota',
  NE: 'Nebraska',
  NH: 'New Hampshire',
  NJ: 'New Jersey',
  NM: 'New Mexico',
  NV: 'Nevada',
  NY: 'New York',
  OH: 'Ohio',
  OK: 'Oklahoma',
  OR: 'Oregon',
  PA: 'Pennsylvania',
  RI: 'Rhode Island',
  SC: 'South Carolina',
  SD: 'South Dakota',
  TN: 'Tennessee',
  TX: 'Texas',
  UT: 'Utah',
  VA: 'Virginia',
  VT: 'Vermont',
  WA: 'Washington',
  WI: 'Wisconsin',
  WV: 'West Virginia',
  WY: 'Wyoming',
  AB: 'Alberta',
  BC: 'British Columbia',
  MB: 'Manitoba',
  NB: 'New Brunswick',
  NL: 'Newfoundland and Labrador',
  NS: 'Nova Scotia',
  NT: 'Northwest Territories',
  NU: 'Nunavut',
  ON: 'Ontario',
  PE: 'Prince Edward Island',
  QC: 'Quebec',
  SK: 'Saskatchewan',
  YT: 'Yukon',
  PR: 'Puerto Rico',
}

const words = (text: string) =>
  text
    .toLowerCase()
    .split(/[^a-z0-9]+/)
    .filter(Boolean)

/** Does one searched word fit the area code: the start of the code, of a word of the city or state name, or the state's two letters. */
function fits(a: AreaCode, word: string): boolean {
  if (/^\d+$/.test(word)) return a.code.startsWith(word)
  if (word === a.state.toLowerCase()) return true
  return [...words(a.city), ...words(REGION_NAMES[a.state] ?? '')].some(w => w.startsWith(word))
}

/**
 * The area codes that fit every word of the search, in list order: "503", "portland", "OR", "oregon" and
 * "Portland, OR" (a label as shown) all work. All of them while nothing is typed.
 */
export function searchAreaCodes(list: readonly AreaCode[], query: string): AreaCode[] {
  const searched = words(query)
  if (!searched.length) return [...list]
  return list.filter(a => searched.every(w => fits(a, w)))
}