@garagem-ai/site-sdk 0.5.3 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +98 -4
- package/dist/index.d.ts +98 -4
- package/dist/index.js +431 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +421 -12
- package/dist/index.mjs.map +1 -1
- package/dist/react.d.mts +18 -2
- package/dist/react.d.ts +18 -2
- package/dist/react.js +134 -31
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +135 -32
- package/dist/react.mjs.map +1 -1
- package/dist/{search-engine-CkbO77G8.d.mts → search-engine-CtnLzmwW.d.mts} +2 -0
- package/dist/{search-engine-CkbO77G8.d.ts → search-engine-CtnLzmwW.d.ts} +2 -0
- package/package.json +8 -5
- package/src/index.ts +19 -0
- package/src/leads.ts +45 -6
- package/src/listing-card-model.ts +4 -1
- package/src/listing-detail.ts +6 -1
- package/src/phone-countries.ts +97 -0
- package/src/phone.ts +431 -0
- package/src/react/index.ts +1 -0
- package/src/react/public-search-snapshot.ts +165 -0
- package/src/react/types.ts +17 -0
- package/src/react/use-public-search.ts +91 -32
- package/src/studio-contract.ts +31 -4
- package/src/types.ts +2 -0
package/src/leads.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import { callJson, type FetchLike } from "./fetchers";
|
|
7
7
|
import { coerceSiteId, normalizeBaseUrl } from "./internal/url";
|
|
8
|
+
import { isValidPhone, toE164 } from "./phone";
|
|
8
9
|
import { resolvePublicSiteId } from "./resolve-site-id";
|
|
9
10
|
import {
|
|
10
11
|
STUDIO_DEFAULT_VISIT_SCHEDULE,
|
|
@@ -88,6 +89,13 @@ type LeadSubmitBase = LeadContact & {
|
|
|
88
89
|
baseUrl?: string;
|
|
89
90
|
fetch?: FetchLike;
|
|
90
91
|
listingId: string;
|
|
92
|
+
/**
|
|
93
|
+
* Active listing modality (`sale` | `rental` | `rent`). Always pass
|
|
94
|
+
* `listing.businessType`, sale included; the server only fills a missing
|
|
95
|
+
* value from the catalog as a safety net.
|
|
96
|
+
*/
|
|
97
|
+
listingType?: string;
|
|
98
|
+
businessType?: string;
|
|
91
99
|
visitorId?: string;
|
|
92
100
|
brokerRef?: string;
|
|
93
101
|
referrer?: string;
|
|
@@ -216,10 +224,15 @@ function normalizeContact(input: LeadContact): {
|
|
|
216
224
|
code: "invalid_contact",
|
|
217
225
|
});
|
|
218
226
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
227
|
+
let normalizedPhone: string | undefined;
|
|
228
|
+
if (phone) {
|
|
229
|
+
const e164 = toE164(phone);
|
|
230
|
+
if (!e164 || !isValidPhone(e164) || e164.length > 40) {
|
|
231
|
+
throw new LeadSubmitError("Garagem SDK: phone is invalid.", {
|
|
232
|
+
code: "invalid_contact",
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
normalizedPhone = e164;
|
|
223
236
|
}
|
|
224
237
|
const message = input.message?.trim() || undefined;
|
|
225
238
|
if (message && message.length > MESSAGE_MAX) {
|
|
@@ -230,7 +243,7 @@ function normalizeContact(input: LeadContact): {
|
|
|
230
243
|
return {
|
|
231
244
|
name,
|
|
232
245
|
...(email ? { email } : {}),
|
|
233
|
-
...(
|
|
246
|
+
...(normalizedPhone ? { phone: normalizedPhone } : {}),
|
|
234
247
|
...(message ? { message } : {}),
|
|
235
248
|
};
|
|
236
249
|
}
|
|
@@ -246,11 +259,29 @@ type SiteSdkLeadBody = {
|
|
|
246
259
|
listingId: string;
|
|
247
260
|
visitDate?: string;
|
|
248
261
|
visitTime?: string;
|
|
262
|
+
businessType?: "sale" | "rent";
|
|
263
|
+
listingType?: string;
|
|
249
264
|
visitorId?: string;
|
|
250
265
|
brokerRef?: string;
|
|
251
266
|
referrer?: string;
|
|
252
267
|
};
|
|
253
268
|
|
|
269
|
+
function normalizeSdkBusinessType(
|
|
270
|
+
value: string | undefined
|
|
271
|
+
): "sale" | "rent" | undefined {
|
|
272
|
+
if (!value) return undefined;
|
|
273
|
+
const normalized = value.trim().toLowerCase();
|
|
274
|
+
if (
|
|
275
|
+
normalized === "rent" ||
|
|
276
|
+
normalized === "rental" ||
|
|
277
|
+
normalized === "aluguel"
|
|
278
|
+
) {
|
|
279
|
+
return "rent";
|
|
280
|
+
}
|
|
281
|
+
if (normalized === "sale" || normalized === "venda") return "sale";
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
|
|
254
285
|
async function postSiteLead(input: {
|
|
255
286
|
siteId?: string | Promise<string>;
|
|
256
287
|
baseUrl?: string;
|
|
@@ -309,9 +340,15 @@ async function postSiteLead(input: {
|
|
|
309
340
|
function leadPostContext(input: LeadSubmitBase): {
|
|
310
341
|
listingId: string;
|
|
311
342
|
contact: ReturnType<typeof normalizeContact>;
|
|
312
|
-
extras: Pick<
|
|
343
|
+
extras: Pick<
|
|
344
|
+
SiteSdkLeadBody,
|
|
345
|
+
"visitorId" | "brokerRef" | "referrer" | "businessType" | "listingType"
|
|
346
|
+
>;
|
|
313
347
|
} {
|
|
314
348
|
assertListingId(input.listingId);
|
|
349
|
+
const businessType =
|
|
350
|
+
normalizeSdkBusinessType(input.businessType) ??
|
|
351
|
+
normalizeSdkBusinessType(input.listingType);
|
|
315
352
|
return {
|
|
316
353
|
listingId: input.listingId.trim(),
|
|
317
354
|
contact: normalizeContact(input),
|
|
@@ -319,6 +356,8 @@ function leadPostContext(input: LeadSubmitBase): {
|
|
|
319
356
|
...(input.visitorId ? { visitorId: input.visitorId } : {}),
|
|
320
357
|
...(input.brokerRef ? { brokerRef: input.brokerRef } : {}),
|
|
321
358
|
...(input.referrer ? { referrer: input.referrer } : {}),
|
|
359
|
+
...(businessType ? { businessType } : {}),
|
|
360
|
+
...(input.listingType ? { listingType: input.listingType } : {}),
|
|
322
361
|
},
|
|
323
362
|
};
|
|
324
363
|
}
|
|
@@ -46,6 +46,7 @@ export function toListingCardModel(
|
|
|
46
46
|
| "title"
|
|
47
47
|
| "businessType"
|
|
48
48
|
| "price"
|
|
49
|
+
| "currency"
|
|
49
50
|
| "priceVisibility"
|
|
50
51
|
| "coverImage"
|
|
51
52
|
> & {
|
|
@@ -73,7 +74,9 @@ export function toListingCardModel(
|
|
|
73
74
|
businessLabel: listingBusinessLabel(listing.businessType),
|
|
74
75
|
priceVisibility: listing.priceVisibility,
|
|
75
76
|
price: listing.price,
|
|
76
|
-
priceLabel: isPrivate
|
|
77
|
+
priceLabel: isPrivate
|
|
78
|
+
? "Sob consulta"
|
|
79
|
+
: formatListingPrice(listing.price, "pt-BR", listing.currency ?? "BRL"),
|
|
77
80
|
pricePeriod:
|
|
78
81
|
listing.businessType === "rental" && !isPrivate && listing.price != null
|
|
79
82
|
? "/mês"
|
package/src/listing-detail.ts
CHANGED
|
@@ -171,6 +171,7 @@ function crumbLabel(segment: string): string {
|
|
|
171
171
|
|
|
172
172
|
export function listingValues(listing: {
|
|
173
173
|
price?: number | null;
|
|
174
|
+
currency?: string | null;
|
|
174
175
|
priceVisibility?: string | null;
|
|
175
176
|
propertyTaxLabel?: string | null;
|
|
176
177
|
property?: { condoFee?: number | null; propertyTax?: number | null } | null;
|
|
@@ -182,7 +183,11 @@ export function listingValues(listing: {
|
|
|
182
183
|
text:
|
|
183
184
|
listing.priceVisibility === "private"
|
|
184
185
|
? "Sob consulta"
|
|
185
|
-
: formatListingPrice(
|
|
186
|
+
: formatListingPrice(
|
|
187
|
+
listing.price,
|
|
188
|
+
"pt-BR",
|
|
189
|
+
listing.currency ?? "BRL"
|
|
190
|
+
),
|
|
186
191
|
},
|
|
187
192
|
];
|
|
188
193
|
const condoFee = positiveCount(listing.property?.condoFee);
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Countries for Studio phone pickers (labels pt-BR).
|
|
3
|
+
* Same catalog shape as `@garagem/ui` PhoneInput — generated sites compose
|
|
4
|
+
* the UI; this list is headless data only.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface PhoneCountry {
|
|
8
|
+
code: string;
|
|
9
|
+
name: string;
|
|
10
|
+
dialCode: string;
|
|
11
|
+
flag: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const DEFAULT_PHONE_COUNTRY_CODE = "BR";
|
|
15
|
+
|
|
16
|
+
export const PHONE_COUNTRIES: PhoneCountry[] = [
|
|
17
|
+
{ code: "BR", name: "Brasil", dialCode: "55", flag: "🇧🇷" },
|
|
18
|
+
{ code: "US", name: "Estados Unidos", dialCode: "1", flag: "🇺🇸" },
|
|
19
|
+
{ code: "CA", name: "Canadá", dialCode: "1", flag: "🇨🇦" },
|
|
20
|
+
{ code: "MX", name: "México", dialCode: "52", flag: "🇲🇽" },
|
|
21
|
+
{ code: "GT", name: "Guatemala", dialCode: "502", flag: "🇬🇹" },
|
|
22
|
+
{ code: "BZ", name: "Belize", dialCode: "501", flag: "🇧🇿" },
|
|
23
|
+
{ code: "SV", name: "El Salvador", dialCode: "503", flag: "🇸🇻" },
|
|
24
|
+
{ code: "HN", name: "Honduras", dialCode: "504", flag: "🇭🇳" },
|
|
25
|
+
{ code: "NI", name: "Nicarágua", dialCode: "505", flag: "🇳🇮" },
|
|
26
|
+
{ code: "CR", name: "Costa Rica", dialCode: "506", flag: "🇨🇷" },
|
|
27
|
+
{ code: "PA", name: "Panamá", dialCode: "507", flag: "🇵🇦" },
|
|
28
|
+
{ code: "CU", name: "Cuba", dialCode: "53", flag: "🇨🇺" },
|
|
29
|
+
{ code: "DO", name: "República Dominicana", dialCode: "1", flag: "🇩🇴" },
|
|
30
|
+
{ code: "HT", name: "Haiti", dialCode: "509", flag: "🇭🇹" },
|
|
31
|
+
{ code: "AR", name: "Argentina", dialCode: "54", flag: "🇦🇷" },
|
|
32
|
+
{ code: "CL", name: "Chile", dialCode: "56", flag: "🇨🇱" },
|
|
33
|
+
{ code: "CO", name: "Colômbia", dialCode: "57", flag: "🇨🇴" },
|
|
34
|
+
{ code: "PE", name: "Peru", dialCode: "51", flag: "🇵🇪" },
|
|
35
|
+
{ code: "UY", name: "Uruguai", dialCode: "598", flag: "🇺🇾" },
|
|
36
|
+
{ code: "PY", name: "Paraguai", dialCode: "595", flag: "🇵🇾" },
|
|
37
|
+
{ code: "BO", name: "Bolívia", dialCode: "591", flag: "🇧🇴" },
|
|
38
|
+
{ code: "EC", name: "Equador", dialCode: "593", flag: "🇪🇨" },
|
|
39
|
+
{ code: "VE", name: "Venezuela", dialCode: "58", flag: "🇻🇪" },
|
|
40
|
+
{ code: "GY", name: "Guiana", dialCode: "592", flag: "🇬🇾" },
|
|
41
|
+
{ code: "SR", name: "Suriname", dialCode: "597", flag: "🇸🇷" },
|
|
42
|
+
{ code: "PT", name: "Portugal", dialCode: "351", flag: "🇵🇹" },
|
|
43
|
+
{ code: "ES", name: "Espanha", dialCode: "34", flag: "🇪🇸" },
|
|
44
|
+
{ code: "FR", name: "França", dialCode: "33", flag: "🇫🇷" },
|
|
45
|
+
{ code: "UK", name: "Reino Unido", dialCode: "44", flag: "🇬🇧" },
|
|
46
|
+
{ code: "DE", name: "Alemanha", dialCode: "49", flag: "🇩🇪" },
|
|
47
|
+
{ code: "IT", name: "Itália", dialCode: "39", flag: "🇮🇹" },
|
|
48
|
+
{ code: "NL", name: "Holanda", dialCode: "31", flag: "🇳🇱" },
|
|
49
|
+
{ code: "BE", name: "Bélgica", dialCode: "32", flag: "🇧🇪" },
|
|
50
|
+
{ code: "CH", name: "Suíça", dialCode: "41", flag: "🇨🇭" },
|
|
51
|
+
{ code: "AT", name: "Áustria", dialCode: "43", flag: "🇦🇹" },
|
|
52
|
+
{ code: "SE", name: "Suécia", dialCode: "46", flag: "🇸🇪" },
|
|
53
|
+
{ code: "NO", name: "Noruega", dialCode: "47", flag: "🇳🇴" },
|
|
54
|
+
{ code: "DK", name: "Dinamarca", dialCode: "45", flag: "🇩🇰" },
|
|
55
|
+
{ code: "FI", name: "Finlândia", dialCode: "358", flag: "🇫🇮" },
|
|
56
|
+
{ code: "IE", name: "Irlanda", dialCode: "353", flag: "🇮🇪" },
|
|
57
|
+
{ code: "AU", name: "Austrália", dialCode: "61", flag: "🇦🇺" },
|
|
58
|
+
{ code: "NZ", name: "Nova Zelândia", dialCode: "64", flag: "🇳🇿" },
|
|
59
|
+
{ code: "JP", name: "Japão", dialCode: "81", flag: "🇯🇵" },
|
|
60
|
+
{ code: "KR", name: "Coreia do Sul", dialCode: "82", flag: "🇰🇷" },
|
|
61
|
+
{ code: "CN", name: "China", dialCode: "86", flag: "🇨🇳" },
|
|
62
|
+
{ code: "IN", name: "Índia", dialCode: "91", flag: "🇮🇳" },
|
|
63
|
+
{ code: "RU", name: "Rússia", dialCode: "7", flag: "🇷🇺" },
|
|
64
|
+
{ code: "UA", name: "Ucrânia", dialCode: "380", flag: "🇺🇦" },
|
|
65
|
+
{ code: "AE", name: "Emirados Árabes", dialCode: "971", flag: "🇦🇪" },
|
|
66
|
+
{ code: "SA", name: "Arábia Saudita", dialCode: "966", flag: "🇸🇦" },
|
|
67
|
+
{ code: "QA", name: "Catar", dialCode: "974", flag: "🇶🇦" },
|
|
68
|
+
{ code: "KW", name: "Kuwait", dialCode: "965", flag: "🇰🇼" },
|
|
69
|
+
{ code: "BH", name: "Bahrein", dialCode: "973", flag: "🇧🇭" },
|
|
70
|
+
{ code: "OM", name: "Omã", dialCode: "968", flag: "🇴🇲" },
|
|
71
|
+
{ code: "JO", name: "Jordânia", dialCode: "962", flag: "🇯🇴" },
|
|
72
|
+
{ code: "LB", name: "Líbano", dialCode: "961", flag: "🇱🇧" },
|
|
73
|
+
{ code: "IL", name: "Israel", dialCode: "972", flag: "🇮🇱" },
|
|
74
|
+
{ code: "TR", name: "Turquia", dialCode: "90", flag: "🇹🇷" },
|
|
75
|
+
{ code: "EG", name: "Egito", dialCode: "20", flag: "🇪🇬" },
|
|
76
|
+
{ code: "ZA", name: "África do Sul", dialCode: "27", flag: "🇿🇦" },
|
|
77
|
+
{ code: "AO", name: "Angola", dialCode: "244", flag: "🇦🇴" },
|
|
78
|
+
{ code: "MZ", name: "Moçambique", dialCode: "258", flag: "🇲🇿" },
|
|
79
|
+
{ code: "CV", name: "Cabo Verde", dialCode: "238", flag: "🇨🇻" },
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
/** cmdk / filter value: name, ISO, DDI, and +DDI so "59" / "+598" both match. */
|
|
83
|
+
export function phoneCountrySearchValue(country: PhoneCountry): string {
|
|
84
|
+
return `${country.name} ${country.code} ${country.dialCode} +${country.dialCode}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function findPhoneCountry(
|
|
88
|
+
code: string | null | undefined
|
|
89
|
+
): PhoneCountry | undefined {
|
|
90
|
+
if (!code) return undefined;
|
|
91
|
+
const normalized = code.trim().toUpperCase();
|
|
92
|
+
return PHONE_COUNTRIES.find((country) => country.code === normalized);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function defaultPhoneCountry(): PhoneCountry {
|
|
96
|
+
return findPhoneCountry(DEFAULT_PHONE_COUNTRY_CODE) ?? PHONE_COUNTRIES[0]!;
|
|
97
|
+
}
|
package/src/phone.ts
ADDED
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headless phone mask / E.164 helpers for Studio-generated lead forms.
|
|
3
|
+
* UI (country picker, input) stays in the app — same contract as CRM
|
|
4
|
+
* `@garagem/ui` PhoneInput without shipping markup.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
AsYouType,
|
|
9
|
+
isValidPhoneNumber,
|
|
10
|
+
parsePhoneNumber,
|
|
11
|
+
type CountryCode,
|
|
12
|
+
} from "libphonenumber-js";
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
DEFAULT_PHONE_COUNTRY_CODE,
|
|
16
|
+
PHONE_COUNTRIES,
|
|
17
|
+
defaultPhoneCountry,
|
|
18
|
+
findPhoneCountry,
|
|
19
|
+
type PhoneCountry,
|
|
20
|
+
} from "./phone-countries";
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
DEFAULT_PHONE_COUNTRY_CODE,
|
|
24
|
+
PHONE_COUNTRIES,
|
|
25
|
+
defaultPhoneCountry,
|
|
26
|
+
findPhoneCountry,
|
|
27
|
+
phoneCountrySearchValue,
|
|
28
|
+
type PhoneCountry,
|
|
29
|
+
} from "./phone-countries";
|
|
30
|
+
|
|
31
|
+
/** ITU-T E.164 national number upper bound. */
|
|
32
|
+
export const MAX_NATIONAL_PHONE_DIGITS = 15;
|
|
33
|
+
|
|
34
|
+
/** Brazilian area codes — required before collapsing a duplicated mobile `9`. */
|
|
35
|
+
const VALID_BR_DDDS = new Set([
|
|
36
|
+
"11",
|
|
37
|
+
"12",
|
|
38
|
+
"13",
|
|
39
|
+
"14",
|
|
40
|
+
"15",
|
|
41
|
+
"16",
|
|
42
|
+
"17",
|
|
43
|
+
"18",
|
|
44
|
+
"19",
|
|
45
|
+
"21",
|
|
46
|
+
"22",
|
|
47
|
+
"24",
|
|
48
|
+
"27",
|
|
49
|
+
"28",
|
|
50
|
+
"31",
|
|
51
|
+
"32",
|
|
52
|
+
"33",
|
|
53
|
+
"34",
|
|
54
|
+
"35",
|
|
55
|
+
"37",
|
|
56
|
+
"38",
|
|
57
|
+
"41",
|
|
58
|
+
"42",
|
|
59
|
+
"43",
|
|
60
|
+
"44",
|
|
61
|
+
"45",
|
|
62
|
+
"46",
|
|
63
|
+
"47",
|
|
64
|
+
"48",
|
|
65
|
+
"49",
|
|
66
|
+
"51",
|
|
67
|
+
"53",
|
|
68
|
+
"54",
|
|
69
|
+
"55",
|
|
70
|
+
"61",
|
|
71
|
+
"62",
|
|
72
|
+
"63",
|
|
73
|
+
"64",
|
|
74
|
+
"65",
|
|
75
|
+
"66",
|
|
76
|
+
"67",
|
|
77
|
+
"68",
|
|
78
|
+
"69",
|
|
79
|
+
"71",
|
|
80
|
+
"73",
|
|
81
|
+
"74",
|
|
82
|
+
"75",
|
|
83
|
+
"77",
|
|
84
|
+
"79",
|
|
85
|
+
"81",
|
|
86
|
+
"82",
|
|
87
|
+
"83",
|
|
88
|
+
"84",
|
|
89
|
+
"85",
|
|
90
|
+
"86",
|
|
91
|
+
"87",
|
|
92
|
+
"88",
|
|
93
|
+
"89",
|
|
94
|
+
"91",
|
|
95
|
+
"92",
|
|
96
|
+
"93",
|
|
97
|
+
"94",
|
|
98
|
+
"95",
|
|
99
|
+
"96",
|
|
100
|
+
"97",
|
|
101
|
+
"98",
|
|
102
|
+
"99",
|
|
103
|
+
]);
|
|
104
|
+
|
|
105
|
+
const PHONE_INPUT_COUNTRY_CODES = new Set(
|
|
106
|
+
PHONE_COUNTRIES.map((country) => country.code)
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const SORTED_PHONE_INPUT_DIAL_CODES = [
|
|
110
|
+
...new Set(PHONE_COUNTRIES.map((country) => country.dialCode)),
|
|
111
|
+
].sort((a, b) => b.length - a.length);
|
|
112
|
+
|
|
113
|
+
function toLibCountryCode(code: string): CountryCode {
|
|
114
|
+
if (code === "UK") return "GB";
|
|
115
|
+
return code as CountryCode;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function fromLibCountryCode(code: CountryCode): string {
|
|
119
|
+
if (code === "GB") return "UK";
|
|
120
|
+
return code;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function nationalPhoneDigits(value: string | null | undefined): string {
|
|
124
|
+
return String(value ?? "").replace(/\D/g, "");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function truncateNationalPhoneDigits(
|
|
128
|
+
digits: string,
|
|
129
|
+
max = MAX_NATIONAL_PHONE_DIGITS
|
|
130
|
+
): string {
|
|
131
|
+
return digits.slice(0, max);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Collapse DDD + extra `9` + 9-digit BR mobile (12 national digits).
|
|
136
|
+
* Matches CRM `collapseDuplicatedBrazilianMobileNine` for common typos.
|
|
137
|
+
*/
|
|
138
|
+
export function collapseDuplicatedBrazilianMobileNine(digits: string): string {
|
|
139
|
+
if (digits.length === 14 && digits.startsWith("55")) {
|
|
140
|
+
const national = collapseDuplicatedBrazilianMobileNine(digits.slice(2));
|
|
141
|
+
return national === digits.slice(2) ? digits : `55${national}`;
|
|
142
|
+
}
|
|
143
|
+
if (digits.length !== 12) return digits;
|
|
144
|
+
const ddd = digits.slice(0, 2);
|
|
145
|
+
if (!VALID_BR_DDDS.has(ddd)) return digits;
|
|
146
|
+
if (digits.charAt(2) !== "9") return digits;
|
|
147
|
+
const rest = digits.slice(3);
|
|
148
|
+
if (rest.length !== 9 || rest.charAt(0) !== "9") return digits;
|
|
149
|
+
// `5599…`: Pelotas extra-9 mobile vs CC55+DDD99 landline (CRM heuristic).
|
|
150
|
+
if (ddd === "55") {
|
|
151
|
+
const subscriberFirst = digits.charAt(4);
|
|
152
|
+
if (subscriberFirst >= "0" && subscriberFirst <= "5") {
|
|
153
|
+
return digits;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return `${ddd}${rest}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const BR_DIAL_CODE = "55";
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Strip an accidental dial-code prefix from national input without eating
|
|
163
|
+
* legitimate nationals that share those digits (e.g. Cuban mobiles 53…).
|
|
164
|
+
* Matches CRM PhoneInput `stripAccidentalDialCodePrefix` so a paste like
|
|
165
|
+
* `5511966906900` into a BR field does not become `+555511966906900`.
|
|
166
|
+
*/
|
|
167
|
+
function stripAccidentalDialCodePrefix(
|
|
168
|
+
numbers: string,
|
|
169
|
+
country: PhoneCountry
|
|
170
|
+
): string {
|
|
171
|
+
const dial = country.dialCode;
|
|
172
|
+
if (!numbers.startsWith(dial) || numbers.length <= dial.length) {
|
|
173
|
+
return numbers;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const withoutDial = numbers.slice(dial.length);
|
|
177
|
+
if (!withoutDial) return numbers;
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
const parsed = parsePhoneNumber(numbers, toLibCountryCode(country.code));
|
|
181
|
+
// Digits were kept as the national number — do not strip.
|
|
182
|
+
if (parsed.nationalNumber === numbers) {
|
|
183
|
+
return numbers;
|
|
184
|
+
}
|
|
185
|
+
// libphonenumber consumed the dial code from the digit string (paste/type
|
|
186
|
+
// of DDI + national into the national field).
|
|
187
|
+
if (
|
|
188
|
+
parsed.countryCallingCode === dial &&
|
|
189
|
+
parsed.nationalNumber === withoutDial
|
|
190
|
+
) {
|
|
191
|
+
return withoutDial;
|
|
192
|
+
}
|
|
193
|
+
} catch {
|
|
194
|
+
/* incomplete / ambiguous — keep digits */
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return numbers;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* National digits for the country the draft belongs to. Strip an accidental
|
|
202
|
+
* country-code prefix first, then run the Brazilian extra-9 collapse only
|
|
203
|
+
* for dial code 55. Any other country keeps its (stripped) digits — a
|
|
204
|
+
* 12-digit foreign national number is not a Brazilian mobile with a
|
|
205
|
+
* duplicated 9.
|
|
206
|
+
*/
|
|
207
|
+
function prepareNationalDigits(
|
|
208
|
+
value: string | null | undefined,
|
|
209
|
+
country: PhoneCountry
|
|
210
|
+
): string {
|
|
211
|
+
const raw = nationalPhoneDigits(value);
|
|
212
|
+
const stripped = stripAccidentalDialCodePrefix(raw, country);
|
|
213
|
+
const digits =
|
|
214
|
+
country.dialCode === BR_DIAL_CODE
|
|
215
|
+
? collapseDuplicatedBrazilianMobileNine(stripped)
|
|
216
|
+
: stripped;
|
|
217
|
+
return truncateNationalPhoneDigits(digits);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Digits of a `+`-prefixed draft, where the number carries its own country.
|
|
222
|
+
* Mirrors CRM `phoneDigits`: a valid non-`55` international number (India
|
|
223
|
+
* `+91 99…`) is kept instead of being rewritten by the Brazilian heuristic.
|
|
224
|
+
*/
|
|
225
|
+
function prepareInternationalDigits(value: string): string {
|
|
226
|
+
const digits = nationalPhoneDigits(value);
|
|
227
|
+
const collapsed = collapseDuplicatedBrazilianMobileNine(digits);
|
|
228
|
+
if (collapsed === digits) return digits;
|
|
229
|
+
try {
|
|
230
|
+
const original = parsePhoneNumber(`+${digits}`);
|
|
231
|
+
if (original.isValid() && original.countryCallingCode !== BR_DIAL_CODE) {
|
|
232
|
+
return digits;
|
|
233
|
+
}
|
|
234
|
+
} catch {
|
|
235
|
+
/* collapse mistaken +BR extras */
|
|
236
|
+
}
|
|
237
|
+
return collapsed;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Pure national mask over already-normalized digits. No prefix strip and no
|
|
242
|
+
* extra-9 collapse here — callers decide whether the digits are fresh input
|
|
243
|
+
* (`formatPhoneNational`) or an existing draft being re-masked.
|
|
244
|
+
*
|
|
245
|
+
* The mask never drops a digit: `formatNational()` re-derives the number and
|
|
246
|
+
* can swallow a leading national prefix (`+1 1966…` → `1966…`), which would
|
|
247
|
+
* lose data once the display is read back as the draft. Fall back to the
|
|
248
|
+
* as-typed national string, then to the raw digits.
|
|
249
|
+
*/
|
|
250
|
+
function formatNationalDigits(digits: string, country: PhoneCountry): string {
|
|
251
|
+
if (!digits) return "";
|
|
252
|
+
const libCode = toLibCountryCode(country.code);
|
|
253
|
+
const formatter = new AsYouType(libCode);
|
|
254
|
+
formatter.input(`+${country.dialCode}${digits}`);
|
|
255
|
+
const national = formatter.getNumber()?.formatNational();
|
|
256
|
+
if (national && nationalPhoneDigits(national) === digits) return national;
|
|
257
|
+
|
|
258
|
+
const asTyped = new AsYouType(libCode).input(digits);
|
|
259
|
+
if (nationalPhoneDigits(asTyped) === digits) return asTyped;
|
|
260
|
+
return digits;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* National display mask for the selected country (`(11) 96690-6900` for BR).
|
|
265
|
+
* Pass national digits or a messy draft; non-digits are stripped.
|
|
266
|
+
*/
|
|
267
|
+
export function formatPhoneNational(
|
|
268
|
+
value: string | null | undefined,
|
|
269
|
+
countryCode: string = DEFAULT_PHONE_COUNTRY_CODE
|
|
270
|
+
): string {
|
|
271
|
+
const country = findPhoneCountry(countryCode) ?? defaultPhoneCountry();
|
|
272
|
+
return formatNationalDigits(prepareNationalDigits(value, country), country);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export type ParsedPhoneInput = {
|
|
276
|
+
ddi: string | null;
|
|
277
|
+
national: string;
|
|
278
|
+
countryCode: string | null;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Split E.164 / `+DDI…` paste vs national draft. Without `+`, `ddi` is null
|
|
283
|
+
* and `national` is all digits (country comes from the picker).
|
|
284
|
+
*/
|
|
285
|
+
export function parsePhoneInput(
|
|
286
|
+
input: string | null | undefined
|
|
287
|
+
): ParsedPhoneInput {
|
|
288
|
+
const cleaned = String(input ?? "").replace(/[\s\-().]/g, "");
|
|
289
|
+
if (!cleaned) {
|
|
290
|
+
return { ddi: null, national: "", countryCode: null };
|
|
291
|
+
}
|
|
292
|
+
const hasPlus = cleaned.startsWith("+");
|
|
293
|
+
|
|
294
|
+
if (hasPlus) {
|
|
295
|
+
try {
|
|
296
|
+
const parsed = parsePhoneNumber(cleaned);
|
|
297
|
+
if (parsed.country) {
|
|
298
|
+
const ourCode = fromLibCountryCode(parsed.country);
|
|
299
|
+
if (PHONE_INPUT_COUNTRY_CODES.has(ourCode)) {
|
|
300
|
+
return {
|
|
301
|
+
ddi: parsed.countryCallingCode,
|
|
302
|
+
national: parsed.nationalNumber,
|
|
303
|
+
countryCode: ourCode,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
} catch {
|
|
308
|
+
/* fall through */
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const digits = cleaned.slice(1);
|
|
312
|
+
for (const ddi of SORTED_PHONE_INPUT_DIAL_CODES) {
|
|
313
|
+
if (digits.startsWith(ddi)) {
|
|
314
|
+
const national = digits.slice(ddi.length);
|
|
315
|
+
const match = PHONE_COUNTRIES.find((c) => c.dialCode === ddi);
|
|
316
|
+
return {
|
|
317
|
+
ddi,
|
|
318
|
+
national,
|
|
319
|
+
countryCode: match?.code ?? null,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
ddi: null,
|
|
325
|
+
national: digits,
|
|
326
|
+
countryCode: null,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return {
|
|
331
|
+
ddi: null,
|
|
332
|
+
national: cleaned.replace(/\D/g, ""),
|
|
333
|
+
countryCode: null,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function preferValidE164(candidate: string): string {
|
|
338
|
+
try {
|
|
339
|
+
const number = parsePhoneNumber(candidate);
|
|
340
|
+
if (number.isValid()) return number.number;
|
|
341
|
+
} catch {
|
|
342
|
+
/* keep constructed */
|
|
343
|
+
}
|
|
344
|
+
return candidate;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Canonical submit value: `+5511966906900`. Empty draft → `""`.
|
|
349
|
+
* Callers that need a guaranteed-valid number should also run `isValidPhone`.
|
|
350
|
+
*/
|
|
351
|
+
export function toE164(
|
|
352
|
+
value: string | null | undefined,
|
|
353
|
+
countryCode: string = DEFAULT_PHONE_COUNTRY_CODE
|
|
354
|
+
): string {
|
|
355
|
+
const trimmed = String(value ?? "").trim();
|
|
356
|
+
if (!trimmed) return "";
|
|
357
|
+
|
|
358
|
+
const parsed = parsePhoneInput(trimmed);
|
|
359
|
+
if (parsed.ddi && parsed.national) {
|
|
360
|
+
const ddiCountry =
|
|
361
|
+
(parsed.countryCode && findPhoneCountry(parsed.countryCode)) ||
|
|
362
|
+
PHONE_COUNTRIES.find((c) => c.dialCode === parsed.ddi) ||
|
|
363
|
+
defaultPhoneCountry();
|
|
364
|
+
return preferValidE164(
|
|
365
|
+
`+${parsed.ddi}${prepareNationalDigits(parsed.national, ddiCountry)}`
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const country = findPhoneCountry(countryCode) ?? defaultPhoneCountry();
|
|
370
|
+
const national = prepareNationalDigits(parsed.national || trimmed, country);
|
|
371
|
+
if (!national) return "";
|
|
372
|
+
return preferValidE164(`+${country.dialCode}${national}`);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Validate national digits or E.164 for the selected country (CRM-aligned).
|
|
377
|
+
*/
|
|
378
|
+
export function isValidPhone(
|
|
379
|
+
value: string | null | undefined,
|
|
380
|
+
countryCode?: string
|
|
381
|
+
): boolean {
|
|
382
|
+
const cleaned = String(value ?? "").trim();
|
|
383
|
+
if (!cleaned) return false;
|
|
384
|
+
|
|
385
|
+
if (cleaned.startsWith("+")) {
|
|
386
|
+
const digits = prepareInternationalDigits(cleaned);
|
|
387
|
+
return digits ? isValidPhoneNumber(`+${digits}`) : false;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// No hint = the default picker country (BR); an explicit non-BR hint must
|
|
391
|
+
// not get the Brazilian collapse. Always strip an accidental dial-code
|
|
392
|
+
// prefix against that country so `5511…` validates as BR national.
|
|
393
|
+
const country = countryCode
|
|
394
|
+
? findPhoneCountry(countryCode)
|
|
395
|
+
: defaultPhoneCountry();
|
|
396
|
+
if (!country) return false;
|
|
397
|
+
const digits = prepareNationalDigits(cleaned, country);
|
|
398
|
+
if (!digits) return false;
|
|
399
|
+
|
|
400
|
+
if (countryCode) {
|
|
401
|
+
return isValidPhoneNumber(digits, toLibCountryCode(countryCode));
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (isValidPhoneNumber(`+${digits}`)) return true;
|
|
405
|
+
return isValidPhoneNumber(digits, "BR");
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Re-mask national digits after the visitor changes country (PhoneInput flow).
|
|
410
|
+
*
|
|
411
|
+
* Lossless on purpose, like CRM PhoneInput `handleCountryChange`: the digits
|
|
412
|
+
* already in the field are kept as-is (truncated to E.164 length), only the
|
|
413
|
+
* mask and the dial code change. The accidental dial-prefix strip and the
|
|
414
|
+
* Brazilian extra-9 collapse are for freshly typed/pasted input — running
|
|
415
|
+
* them here against the destination country eats legitimate digits (NANP
|
|
416
|
+
* dial `1` would take the first digit of a BR `11…` national on a toggle).
|
|
417
|
+
*/
|
|
418
|
+
export function remaskPhoneForCountry(
|
|
419
|
+
nationalOrDraft: string | null | undefined,
|
|
420
|
+
countryCode: string
|
|
421
|
+
): { display: string; e164: string; country: PhoneCountry } {
|
|
422
|
+
const country = findPhoneCountry(countryCode) ?? defaultPhoneCountry();
|
|
423
|
+
const digits = truncateNationalPhoneDigits(
|
|
424
|
+
nationalPhoneDigits(nationalOrDraft)
|
|
425
|
+
);
|
|
426
|
+
return {
|
|
427
|
+
display: formatNationalDigits(digits, country),
|
|
428
|
+
e164: digits ? `+${country.dialCode}${digits}` : "",
|
|
429
|
+
country,
|
|
430
|
+
};
|
|
431
|
+
}
|