@nuskin/shipping-address-form 1.5.1 → 1.7.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.
Files changed (96) hide show
  1. package/README.md +54 -12
  2. package/dist/addressLookup/AddressLookup.js +74 -0
  3. package/dist/addressLookup/AddressLookup.js.map +1 -0
  4. package/dist/addressLookup/components/LocationPinIcon.js +33 -0
  5. package/dist/addressLookup/components/LocationPinIcon.js.map +1 -0
  6. package/dist/addressLookup/components/SmartyIntlResults.js +1020 -0
  7. package/dist/addressLookup/components/SmartyIntlResults.js.map +1 -0
  8. package/dist/addressLookup/components/SmartyUSResults.js +1001 -0
  9. package/dist/addressLookup/components/SmartyUSResults.js.map +1 -0
  10. package/dist/addressLookup/config/address/countries.js +61 -0
  11. package/dist/addressLookup/config/address/countries.js.map +1 -0
  12. package/dist/addressLookup/config/address/index.js +19 -0
  13. package/dist/addressLookup/config/address/index.js.map +1 -0
  14. package/dist/addressLookup/config/googleMaps.js +12 -0
  15. package/dist/addressLookup/config/googleMaps.js.map +1 -0
  16. package/dist/addressLookup/countries.js +19 -0
  17. package/dist/addressLookup/countries.js.map +1 -0
  18. package/dist/addressLookup/hooks/useAddressFieldLabels.js +41 -0
  19. package/dist/addressLookup/hooks/useAddressFieldLabels.js.map +1 -0
  20. package/dist/addressLookup/hooks/useAddressForm.js +36 -0
  21. package/dist/addressLookup/hooks/useAddressForm.js.map +1 -0
  22. package/dist/addressLookup/hooks/useAddressMapModal.js +136 -0
  23. package/dist/addressLookup/hooks/useAddressMapModal.js.map +1 -0
  24. package/dist/addressLookup/hooks/useAddressValidationBase.js +143 -0
  25. package/dist/addressLookup/hooks/useAddressValidationBase.js.map +1 -0
  26. package/dist/addressLookup/hooks/useInitialAddressVerification.js +57 -0
  27. package/dist/addressLookup/hooks/useInitialAddressVerification.js.map +1 -0
  28. package/dist/addressLookup/hooks/useIntlAddressValidation.js +122 -0
  29. package/dist/addressLookup/hooks/useIntlAddressValidation.js.map +1 -0
  30. package/dist/addressLookup/hooks/useSmartyInternationalLookup.js +74 -0
  31. package/dist/addressLookup/hooks/useSmartyInternationalLookup.js.map +1 -0
  32. package/dist/addressLookup/hooks/useSmartyLookupBase.js +152 -0
  33. package/dist/addressLookup/hooks/useSmartyLookupBase.js.map +1 -0
  34. package/dist/addressLookup/hooks/useSmartyUSLookup.js +79 -0
  35. package/dist/addressLookup/hooks/useSmartyUSLookup.js.map +1 -0
  36. package/dist/addressLookup/hooks/useUSAddressValidation.js +166 -0
  37. package/dist/addressLookup/hooks/useUSAddressValidation.js.map +1 -0
  38. package/dist/addressLookup/index.js +41 -0
  39. package/dist/addressLookup/index.js.map +1 -0
  40. package/dist/addressLookup/styles/addressLookup.css +383 -0
  41. package/dist/addressLookup/utils/addressForm.js +150 -0
  42. package/dist/addressLookup/utils/addressForm.js.map +1 -0
  43. package/dist/addressLookup/utils/addressMapping.js +190 -0
  44. package/dist/addressLookup/utils/addressMapping.js.map +1 -0
  45. package/dist/addressLookup/utils/addressStatus.js +173 -0
  46. package/dist/addressLookup/utils/addressStatus.js.map +1 -0
  47. package/dist/addressLookup/utils/addressStrings.js +159 -0
  48. package/dist/addressLookup/utils/addressStrings.js.map +1 -0
  49. package/dist/addressLookup/utils/addressVerification.js +66 -0
  50. package/dist/addressLookup/utils/addressVerification.js.map +1 -0
  51. package/dist/addressLookup/utils/addressWrapper.js +40 -0
  52. package/dist/addressLookup/utils/addressWrapper.js.map +1 -0
  53. package/dist/addressLookup/utils/componentPropTypes.js +118 -0
  54. package/dist/addressLookup/utils/componentPropTypes.js.map +1 -0
  55. package/dist/addressLookup/utils/googleMapsLoader.js +68 -0
  56. package/dist/addressLookup/utils/googleMapsLoader.js.map +1 -0
  57. package/dist/addressLookup/utils/inFlightRequests.js +50 -0
  58. package/dist/addressLookup/utils/inFlightRequests.js.map +1 -0
  59. package/dist/addressLookup/utils/intlAutocompleteBusinessRules.js +62 -0
  60. package/dist/addressLookup/utils/intlAutocompleteBusinessRules.js.map +1 -0
  61. package/dist/addressLookup/utils/requestCache.js +44 -0
  62. package/dist/addressLookup/utils/requestCache.js.map +1 -0
  63. package/dist/addressLookup/utils/requestRateLimiter.js +53 -0
  64. package/dist/addressLookup/utils/requestRateLimiter.js.map +1 -0
  65. package/dist/addressLookup/utils/smartyConfig.js +11 -0
  66. package/dist/addressLookup/utils/smartyConfig.js.map +1 -0
  67. package/dist/addressLookup/utils/validators.js +45 -0
  68. package/dist/addressLookup/utils/validators.js.map +1 -0
  69. package/dist/index.js +28 -1
  70. package/dist/index.js.map +1 -1
  71. package/dist/{ShippingAddressForm.js → shippingAddressForm/ShippingAddressForm.js} +10 -2
  72. package/dist/shippingAddressForm/ShippingAddressForm.js.map +1 -0
  73. package/dist/{components → shippingAddressForm/components}/ShippingField.js +32 -22
  74. package/dist/shippingAddressForm/components/ShippingField.js.map +1 -0
  75. package/dist/shippingAddressForm/hooks/useAddressFieldLabels.js.map +1 -0
  76. package/dist/shippingAddressForm/hooks/useShippingAddressForm.js.map +1 -0
  77. package/dist/shippingAddressForm/hooks/useShippingFormState.js.map +1 -0
  78. package/dist/shippingAddressForm/utils/emailVerify.js.map +1 -0
  79. package/dist/{utils → shippingAddressForm/utils}/shippingAddressForm.js +3 -3
  80. package/dist/shippingAddressForm/utils/shippingAddressForm.js.map +1 -0
  81. package/dist/shippingAddressForm/utils/shippingStrings.js.map +1 -0
  82. package/package.json +2 -3
  83. package/dist/ShippingAddressForm.js.map +0 -1
  84. package/dist/components/ShippingField.js.map +0 -1
  85. package/dist/hooks/useAddressFieldLabels.js.map +0 -1
  86. package/dist/hooks/useShippingAddressForm.js.map +0 -1
  87. package/dist/hooks/useShippingFormState.js.map +0 -1
  88. package/dist/utils/emailVerify.js.map +0 -1
  89. package/dist/utils/shippingAddressForm.js.map +0 -1
  90. package/dist/utils/shippingStrings.js.map +0 -1
  91. /package/dist/{hooks → shippingAddressForm/hooks}/useAddressFieldLabels.js +0 -0
  92. /package/dist/{hooks → shippingAddressForm/hooks}/useShippingAddressForm.js +0 -0
  93. /package/dist/{hooks → shippingAddressForm/hooks}/useShippingFormState.js +0 -0
  94. /package/dist/{styles → shippingAddressForm/styles}/shippingAddressForm.css +0 -0
  95. /package/dist/{utils → shippingAddressForm/utils}/emailVerify.js +0 -0
  96. /package/dist/{utils → shippingAddressForm/utils}/shippingStrings.js +0 -0
@@ -0,0 +1,1020 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _propTypes = _interopRequireDefault(require("prop-types"));
8
+ var _react = require("react");
9
+ var _useSmartyInternationalLookup = _interopRequireDefault(require("../hooks/useSmartyInternationalLookup"));
10
+ var _useIntlAddressValidation = _interopRequireDefault(require("../hooks/useIntlAddressValidation"));
11
+ var _useAddressFieldLabels = _interopRequireDefault(require("../hooks/useAddressFieldLabels"));
12
+ var _useAddressForm = _interopRequireDefault(require("../hooks/useAddressForm"));
13
+ var _useInitialAddressVerification = _interopRequireDefault(require("../hooks/useInitialAddressVerification"));
14
+ var _useAddressMapModal = _interopRequireDefault(require("../hooks/useAddressMapModal"));
15
+ var _addressMapping = require("../utils/addressMapping");
16
+ var _addressWrapper = require("../utils/addressWrapper");
17
+ var _addressStatus = require("../utils/addressStatus");
18
+ var _addressVerification = require("../utils/addressVerification");
19
+ var _validators = require("../utils/validators");
20
+ var _componentPropTypes = require("../utils/componentPropTypes");
21
+ var _LocationPinIcon = _interopRequireDefault(require("./LocationPinIcon"));
22
+ var _foundationUiComponents = require("@nuskin/foundation-ui-components");
23
+ var _jsxRuntime = require("react/jsx-runtime");
24
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
25
+ const stripGeocode = (address = {}) => {
26
+ const nextAddress = {
27
+ ...address
28
+ };
29
+ delete nextAddress.latitude;
30
+ delete nextAddress.longitude;
31
+ return nextAddress;
32
+ };
33
+ const FIELD_TEST_IDS = {
34
+ address1: 'qa-address-1',
35
+ address2: 'qa-address-2',
36
+ district: 'qa-address-district',
37
+ region: 'qa-address-region',
38
+ city: 'qa-address-city',
39
+ postalCode: 'qa-address-postal-code'
40
+ };
41
+ const FIELD_IDS = {
42
+ address1: 'address-line-1',
43
+ address2: 'address-line-2',
44
+ district: 'address-district',
45
+ region: 'address-region',
46
+ city: 'address-city',
47
+ postalCode: 'address-postal-code'
48
+ };
49
+ const getFieldTestId = field => FIELD_TEST_IDS[field] || `qa-address-${field}`;
50
+ const getFieldId = field => FIELD_IDS[field] || `address-${field}`;
51
+ const LOOKUP_LIST_BUTTON_SELECTOR = '.ns-address-lookup__list-button';
52
+ const SmartyIntlResults = ({
53
+ countryCode,
54
+ onSelect,
55
+ initialAddress = null,
56
+ entryMode = 'both',
57
+ requireValidation = true,
58
+ readOnlyView = false,
59
+ showManualValidateButton = false,
60
+ validationRequestId = 0,
61
+ language = 'en',
62
+ strings = null,
63
+ formDefinition = null,
64
+ deferFormLookup = false
65
+ }) => {
66
+ var _formDefinition$addre;
67
+ const [mode, setMode] = (0, _react.useState)(entryMode === 'manual' ? 'manual' : 'autocomplete');
68
+ const [query, setQuery] = (0, _react.useState)('');
69
+ const [selectedAddress, setSelectedAddress] = (0, _react.useState)(null);
70
+ const [selectedIntlAddressId, setSelectedIntlAddressId] = (0, _react.useState)(null);
71
+ const [formData, setFormData] = (0, _react.useState)(initialAddress || {});
72
+ const [verificationStatus, setVerificationStatus] = (0, _react.useState)((0, _addressVerification.getIncomingVerificationStatus)(initialAddress));
73
+ const [customError, setCustomError] = (0, _react.useState)(null);
74
+ const [validationTrigger, setValidationTrigger] = (0, _react.useState)(0);
75
+ const [validationTriggerSource, setValidationTriggerSource] = (0, _react.useState)(null);
76
+ const [showValidationSuggestions, setShowValidationSuggestions] = (0, _react.useState)(false);
77
+ const [mapAddressLines, setMapAddressLines] = (0, _react.useState)([]);
78
+ const [hasPendingLookupDraft, setHasPendingLookupDraft] = (0, _react.useState)(false);
79
+ const [touchedFields, setTouchedFields] = (0, _react.useState)({});
80
+ const [showAllRequiredErrors, setShowAllRequiredErrors] = (0, _react.useState)(false);
81
+ const [lookupServiceError, setLookupServiceError] = (0, _react.useState)(null);
82
+ const [validationServiceError, setValidationServiceError] = (0, _react.useState)(null);
83
+ const shouldSearchRef = (0, _react.useRef)(true);
84
+ const fieldTimerRef = (0, _react.useRef)(null);
85
+ const componentRef = (0, _react.useRef)(null);
86
+ const suggestionsListRef = (0, _react.useRef)(null);
87
+ const lastCommittedAddressRef = (0, _react.useRef)(initialAddress || {});
88
+ const lookupDraftOriginalRef = (0, _react.useRef)(initialAddress || {});
89
+ const appliedSelectionKeyRef = (0, _react.useRef)(null);
90
+ const mapValidationStartedRef = (0, _react.useRef)(false);
91
+ const autoApplyValidationStartedRef = (0, _react.useRef)(false);
92
+ const hasInitializedAddressRef = (0, _react.useRef)(false);
93
+ (0, _react.useEffect)(() => {
94
+ if (!initialAddress || hasInitializedAddressRef.current) {
95
+ return;
96
+ }
97
+ hasInitializedAddressRef.current = true;
98
+ setFormData(initialAddress);
99
+ setVerificationStatus((0, _addressVerification.getIncomingVerificationStatus)(initialAddress));
100
+ shouldSearchRef.current = false;
101
+ lastCommittedAddressRef.current = initialAddress;
102
+ lookupDraftOriginalRef.current = initialAddress;
103
+ setTouchedFields({});
104
+ setShowAllRequiredErrors(false);
105
+ setLookupServiceError(null);
106
+ setValidationServiceError(null);
107
+ setMapAddressLines([]);
108
+ }, [initialAddress]);
109
+ (0, _react.useEffect)(() => {
110
+ return () => {
111
+ if (fieldTimerRef.current) {
112
+ clearTimeout(fieldTimerRef.current);
113
+ }
114
+ };
115
+ }, []);
116
+ const isAutocompleteMode = mode === 'autocomplete';
117
+ const {
118
+ labels: fieldLabels,
119
+ isLoading: areLabelsLoading
120
+ } = (0, _useAddressFieldLabels.default)(countryCode, language, strings);
121
+ const shouldLoadAddressForm = !formDefinition && !deferFormLookup;
122
+ const {
123
+ addressForm: loadedAddressForm,
124
+ isLoading: isFormLoading
125
+ } = (0, _useAddressForm.default)(countryCode, shouldLoadAddressForm);
126
+ const providedAddressForm = formDefinition !== null && formDefinition !== void 0 && (_formDefinition$addre = formDefinition.addressElements) !== null && _formDefinition$addre !== void 0 && _formDefinition$addre.length ? formDefinition : null;
127
+ const resolvedAddressForm = providedAddressForm || loadedAddressForm;
128
+ const allowValidation = requireValidation && !readOnlyView;
129
+ const lookupField = (resolvedAddressForm === null || resolvedAddressForm === void 0 ? void 0 : resolvedAddressForm.lookupField) || 'address1';
130
+ const requiredFields = (0, _addressStatus.getRequiredFieldsFromAddressForm)(resolvedAddressForm);
131
+ const hasEmailField = ((resolvedAddressForm === null || resolvedAddressForm === void 0 ? void 0 : resolvedAddressForm.addressElements) || []).some(field => field.field === 'email');
132
+ const hasMapCoordinates = (formData === null || formData === void 0 ? void 0 : formData.latitude) !== undefined && (formData === null || formData === void 0 ? void 0 : formData.longitude) !== undefined;
133
+ const hasLookupValue = Boolean(String((formData === null || formData === void 0 ? void 0 : formData[lookupField]) || '').trim());
134
+ const canAttemptMap = Boolean(!isAutocompleteMode && hasLookupValue || isAutocompleteMode && ['autocomplete', 'verified'].includes(verificationStatus) && hasLookupValue);
135
+ const showMapAction = !readOnlyView && (hasMapCoordinates || canAttemptMap);
136
+ const mapInfoLines = (mapAddressLines.length ? mapAddressLines : [formData.address1, formData.address2, formData.district, [formData.postalCode, formData.city].filter(Boolean).join(' '), formData.region]).filter(Boolean);
137
+ const {
138
+ mapContainerRef,
139
+ pendingMapOpen,
140
+ setPendingMapOpen,
141
+ setShowMapModal,
142
+ showMapModal
143
+ } = (0, _useAddressMapModal.default)({
144
+ formData,
145
+ infoLines: mapInfoLines,
146
+ hasMapCoordinates,
147
+ openInMapsLabel: fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.openInMaps,
148
+ shouldPrepareMap: showMapAction
149
+ });
150
+ const {
151
+ suggestions,
152
+ loading,
153
+ error,
154
+ rateLimited
155
+ } = (0, _useSmartyInternationalLookup.default)(!readOnlyView && shouldSearchRef.current && isAutocompleteMode ? query : '', countryCode, selectedIntlAddressId, 500);
156
+ const {
157
+ suggestions: validationSuggestions,
158
+ loading: validationLoading,
159
+ error: validationError,
160
+ rateLimited: validationRateLimited
161
+ } = (0, _useIntlAddressValidation.default)(formData, countryCode, allowValidation ? validationTrigger : 0, lookupField);
162
+ const lookupValue = formData[lookupField];
163
+ const lookupErrorMessage = lookupServiceError !== null && lookupServiceError !== void 0 && lookupServiceError.rateLimited ? (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.rateLimited) || 'Too many requests. Please wait a moment and try again.' : (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.lookupError) || 'We could not look up that address right now.';
164
+ const validationErrorMessage = validationServiceError !== null && validationServiceError !== void 0 && validationServiceError.rateLimited ? (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.rateLimited) || 'Too many requests. Please wait a moment and try again.' : (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.validationError) || 'We could not validate that address right now.';
165
+ (0, _react.useEffect)(() => {
166
+ if (!validationRequestId) {
167
+ return;
168
+ }
169
+ setShowAllRequiredErrors(true);
170
+ onSelect === null || onSelect === void 0 || onSelect({
171
+ type: 'validation-request',
172
+ address: formData,
173
+ status: (0, _addressStatus.buildAddressStatus)({
174
+ countryCode,
175
+ addressForm: resolvedAddressForm,
176
+ formData,
177
+ verificationStatus,
178
+ mode,
179
+ requireValidation: allowValidation
180
+ })
181
+ });
182
+ }, [allowValidation, validationRequestId]);
183
+ (0, _react.useEffect)(() => {
184
+ if (!error) {
185
+ setLookupServiceError(null);
186
+ return;
187
+ }
188
+ setLookupServiceError({
189
+ rateLimited,
190
+ rawMessage: error
191
+ });
192
+ }, [error, rateLimited]);
193
+ (0, _react.useEffect)(() => {
194
+ if (!validationError) {
195
+ setValidationServiceError(null);
196
+ return;
197
+ }
198
+ setValidationServiceError({
199
+ rateLimited: validationRateLimited,
200
+ rawMessage: validationError
201
+ });
202
+ setPendingMapOpen(false);
203
+ mapValidationStartedRef.current = false;
204
+ }, [validationError, validationRateLimited]);
205
+ (0, _react.useEffect)(() => {
206
+ if (!lookupServiceError) {
207
+ return;
208
+ }
209
+ onSelect === null || onSelect === void 0 || onSelect({
210
+ type: 'lookup-error',
211
+ address: formData,
212
+ status: (0, _addressStatus.buildAddressStatus)({
213
+ countryCode,
214
+ addressForm: resolvedAddressForm,
215
+ formData,
216
+ verificationStatus,
217
+ mode,
218
+ requireValidation: allowValidation
219
+ }),
220
+ error: {
221
+ kind: lookupServiceError.rateLimited ? 'rate-limited' : 'api',
222
+ source: 'autocomplete',
223
+ message: lookupErrorMessage
224
+ }
225
+ });
226
+ }, [lookupServiceError]);
227
+ (0, _react.useEffect)(() => {
228
+ if (!validationServiceError) {
229
+ return;
230
+ }
231
+ onSelect === null || onSelect === void 0 || onSelect({
232
+ type: 'validation-error',
233
+ address: formData,
234
+ status: (0, _addressStatus.buildAddressStatus)({
235
+ countryCode,
236
+ addressForm: resolvedAddressForm,
237
+ formData,
238
+ verificationStatus,
239
+ mode,
240
+ requireValidation: allowValidation
241
+ }),
242
+ error: {
243
+ kind: validationServiceError.rateLimited ? 'rate-limited' : 'api',
244
+ source: 'validation',
245
+ message: validationErrorMessage
246
+ }
247
+ });
248
+ }, [validationServiceError]);
249
+ const clearServiceErrors = ({
250
+ lookup = true,
251
+ validation = true,
252
+ custom = false
253
+ } = {}) => {
254
+ if (lookup) {
255
+ setLookupServiceError(null);
256
+ }
257
+ if (validation) {
258
+ setValidationServiceError(null);
259
+ }
260
+ if (custom) {
261
+ setCustomError(null);
262
+ }
263
+ };
264
+ const stripUnusedEmail = (address = {}) => {
265
+ if (hasEmailField) {
266
+ return address;
267
+ }
268
+ const nextAddress = {
269
+ ...address
270
+ };
271
+ delete nextAddress.email;
272
+ return nextAddress;
273
+ };
274
+ const buildTrackedAddress = (address = {}, status = 'unverified', source = null) => stripUnusedEmail((0, _addressVerification.withAddressVerification)(address, status, source));
275
+ const isAcceptableInitialValidationResult = validatedAddress => {
276
+ var _validatedAddress$val;
277
+ return ['verified', 'verified-sub-building-changed'].includes(validatedAddress === null || validatedAddress === void 0 || (_validatedAddress$val = validatedAddress.validationSummary) === null || _validatedAddress$val === void 0 ? void 0 : _validatedAddress$val.code);
278
+ };
279
+ (0, _useInitialAddressVerification.default)({
280
+ initialAddress,
281
+ countryCode,
282
+ lookupField,
283
+ requireValidation: allowValidation,
284
+ resolvedAddressForm,
285
+ setValidationServiceError,
286
+ setCustomError,
287
+ setShowValidationSuggestions,
288
+ setValidationTriggerSource,
289
+ setValidationTrigger
290
+ });
291
+ (0, _react.useEffect)(() => {
292
+ if (selectedIntlAddressId) {
293
+ if (suggestions.length === 1 && suggestions[0].isDetailed) {
294
+ setSelectedAddress(suggestions[0]);
295
+ setSelectedIntlAddressId(null);
296
+ setQuery('');
297
+ setCustomError(null);
298
+ } else if (suggestions.length === 0 && !loading) {
299
+ setCustomError('Unable to retrieve address details. Please try another address or enter manually.');
300
+ setSelectedIntlAddressId(null);
301
+ setQuery('');
302
+ }
303
+ } else if (suggestions.length > 0 && !suggestions[0].isDetailed && !selectedAddress) {
304
+ setSelectedAddress(null);
305
+ setCustomError(null);
306
+ }
307
+ }, [suggestions, selectedIntlAddressId, selectedAddress, loading]);
308
+ (0, _react.useEffect)(() => {
309
+ if (lookupValue !== undefined && shouldSearchRef.current && !selectedAddress) {
310
+ setQuery(lookupValue);
311
+ }
312
+ }, [lookupValue, selectedAddress]);
313
+ (0, _react.useEffect)(() => {
314
+ var _suggestionButtons$;
315
+ const shouldShowValidationResults = allowValidation && showValidationSuggestions && validationSuggestions.length > 0 && !validationLoading && (!isAutocompleteMode || ['map', 'initial'].includes(validationTriggerSource));
316
+ if (!shouldShowValidationResults) {
317
+ return;
318
+ }
319
+ const suggestionButtons = getVisibleSuggestionButtons();
320
+ if (suggestionButtons.length === 0 || suggestionButtons.includes(document.activeElement)) {
321
+ return;
322
+ }
323
+ (_suggestionButtons$ = suggestionButtons[0]) === null || _suggestionButtons$ === void 0 || _suggestionButtons$.focus();
324
+ }, [isAutocompleteMode, allowValidation, showValidationSuggestions, validationLoading, validationSuggestions, validationTriggerSource]);
325
+ (0, _react.useEffect)(() => {
326
+ var _getLookupInputElemen;
327
+ const shouldRefocusLookupField = isAutocompleteMode && selectedIntlAddressId && suggestions.length > 0 && !selectedAddress && !loading;
328
+ if (!shouldRefocusLookupField) {
329
+ return;
330
+ }
331
+ (_getLookupInputElemen = getLookupInputElement()) === null || _getLookupInputElemen === void 0 || _getLookupInputElemen.focus();
332
+ }, [isAutocompleteMode, selectedIntlAddressId, suggestions, selectedAddress, loading]);
333
+ (0, _react.useEffect)(() => {
334
+ if (validationTrigger > 0 && ['autocomplete', 'map', 'initial'].includes(validationTriggerSource)) {
335
+ autoApplyValidationStartedRef.current = false;
336
+ }
337
+ }, [validationTrigger, validationTriggerSource]);
338
+ (0, _react.useEffect)(() => {
339
+ if (validationTriggerSource === 'map' && validationLoading) {
340
+ mapValidationStartedRef.current = true;
341
+ }
342
+ if (validationLoading && ['autocomplete', 'map', 'initial'].includes(validationTriggerSource)) {
343
+ autoApplyValidationStartedRef.current = true;
344
+ }
345
+ }, [validationLoading, validationTriggerSource]);
346
+ (0, _react.useEffect)(() => {
347
+ if (validationTriggerSource === 'map' && mapValidationStartedRef.current && showValidationSuggestions && !validationLoading && validationSuggestions.length === 0) {
348
+ setPendingMapOpen(false);
349
+ mapValidationStartedRef.current = false;
350
+ }
351
+ }, [validationLoading, validationSuggestions, validationTriggerSource, showValidationSuggestions]);
352
+ (0, _react.useEffect)(() => {
353
+ if (!hasPendingLookupDraft || suggestions.length === 0) {
354
+ return undefined;
355
+ }
356
+ const handleDocumentMouseDown = event => {
357
+ const component = componentRef.current;
358
+ if (component !== null && component !== void 0 && component.contains(event.target)) {
359
+ return;
360
+ }
361
+ const suggestionsList = suggestionsListRef.current;
362
+ if ((suggestionsList === null || suggestionsList === void 0 ? void 0 : suggestionsList.contains(event.target)) === false) {
363
+ shouldSearchRef.current = false;
364
+ setQuery('');
365
+ setSelectedAddress(null);
366
+ setSelectedIntlAddressId(null);
367
+ setShowValidationSuggestions(false);
368
+ clearServiceErrors({
369
+ custom: true
370
+ });
371
+ setFormData(lookupDraftOriginalRef.current);
372
+ setHasPendingLookupDraft(false);
373
+ }
374
+ };
375
+ document.addEventListener('pointerdown', handleDocumentMouseDown);
376
+ return () => {
377
+ document.removeEventListener('pointerdown', handleDocumentMouseDown);
378
+ };
379
+ }, [hasPendingLookupDraft, suggestions]);
380
+ (0, _react.useEffect)(() => {
381
+ if (isAutocompleteMode || !showValidationSuggestions || validationLoading || validationSuggestions.length === 0) {
382
+ return undefined;
383
+ }
384
+ const handleDocumentPointerDown = event => {
385
+ var _componentRef$current;
386
+ if ((_componentRef$current = componentRef.current) !== null && _componentRef$current !== void 0 && _componentRef$current.contains(event.target)) {
387
+ return;
388
+ }
389
+ setShowValidationSuggestions(false);
390
+ };
391
+ document.addEventListener('pointerdown', handleDocumentPointerDown);
392
+ return () => {
393
+ document.removeEventListener('pointerdown', handleDocumentPointerDown);
394
+ };
395
+ }, [isAutocompleteMode, showValidationSuggestions, validationLoading, validationSuggestions]);
396
+ (0, _react.useEffect)(() => {
397
+ if (selectedAddress) {
398
+ const selectionKey = selectedAddress.address_id || `${selectedAddress.street || ''}|${selectedAddress.locality || ''}|${selectedAddress.postal_code || ''}`;
399
+ if (appliedSelectionKeyRef.current === selectionKey) {
400
+ return;
401
+ }
402
+ appliedSelectionKeyRef.current = selectionKey;
403
+ const mapped = (0, _addressMapping.buildIntlAutocompleteAddressMapping)(selectedAddress, resolvedAddressForm);
404
+ const updatedFormData = {
405
+ ...stripGeocode(formData),
406
+ ...mapped,
407
+ email: formData.email
408
+ };
409
+ const normalizedFormData = buildTrackedAddress(updatedFormData, 'autocomplete', 'smarty-intl');
410
+ setFormData(normalizedFormData);
411
+ setMapAddressLines([]);
412
+ lastCommittedAddressRef.current = normalizedFormData;
413
+ lookupDraftOriginalRef.current = normalizedFormData;
414
+ setHasPendingLookupDraft(false);
415
+ clearServiceErrors({
416
+ custom: true
417
+ });
418
+ const currentVerificationStatus = 'autocomplete';
419
+ setVerificationStatus(currentVerificationStatus);
420
+ onSelect === null || onSelect === void 0 || onSelect({
421
+ type: 'autocomplete',
422
+ address: normalizedFormData,
423
+ status: (0, _addressStatus.buildAddressStatus)({
424
+ countryCode,
425
+ addressForm: resolvedAddressForm,
426
+ formData: normalizedFormData,
427
+ verificationStatus: currentVerificationStatus,
428
+ mode,
429
+ requireValidation: allowValidation
430
+ })
431
+ });
432
+ if ((0, _addressStatus.shouldVerifyAutocompleteSelection)({
433
+ addressForm: resolvedAddressForm,
434
+ formData: normalizedFormData,
435
+ requireValidation: allowValidation
436
+ })) {
437
+ // Trigger validation instead of directly selecting.
438
+ // Keep suggestions visible in case multiple candidates are returned.
439
+ setShowValidationSuggestions(true);
440
+ setValidationTriggerSource('autocomplete');
441
+ setValidationTrigger(prev => prev + 1);
442
+ }
443
+ }
444
+ }, [selectedAddress]);
445
+ const handleInputChange = e => {
446
+ const value = (0, _validators.normalizeAddressFieldValue)(lookupField, e.target.value);
447
+ shouldSearchRef.current = true;
448
+ appliedSelectionKeyRef.current = null;
449
+ setSelectedAddress(null);
450
+ setSelectedIntlAddressId(null);
451
+ setShowValidationSuggestions(false);
452
+ setShowMapModal(false);
453
+ setPendingMapOpen(false);
454
+ mapValidationStartedRef.current = false;
455
+ setMapAddressLines([]);
456
+ setValidationTriggerSource(null);
457
+ clearServiceErrors({
458
+ custom: true
459
+ });
460
+ if (!hasPendingLookupDraft) {
461
+ lookupDraftOriginalRef.current = formData;
462
+ setHasPendingLookupDraft(true);
463
+ }
464
+ const updatedFormData = stripUnusedEmail({
465
+ ...stripGeocode(formData),
466
+ [lookupField]: value
467
+ });
468
+ setFormData(buildTrackedAddress(updatedFormData, 'unverified'));
469
+ setVerificationStatus('unverified');
470
+ };
471
+ const isFieldRequired = field => requiredFields.includes(field);
472
+ const isFieldBlank = field => String(formData[field] || '').trim() === '';
473
+ const hasInvalidPostalCodeFormat = field => {
474
+ if (field !== 'postalCode') {
475
+ return false;
476
+ }
477
+ const value = String(formData.postalCode || '').trim();
478
+ if (!value || !resolvedAddressForm.postalCodeRegex) {
479
+ return false;
480
+ }
481
+ return !resolvedAddressForm.postalCodeRegex.test((0, _validators.normalizePostalCodeValue)(value));
482
+ };
483
+ const hasInvalidEmailFormat = field => {
484
+ if (field !== 'email') {
485
+ return false;
486
+ }
487
+ const value = String(formData.email || '').trim();
488
+ if (!value) {
489
+ return false;
490
+ }
491
+ return !(0, _validators.isValidEmail)(value);
492
+ };
493
+ const shouldShowFieldError = field => {
494
+ const shouldShowValidation = Boolean(showAllRequiredErrors || touchedFields[field]);
495
+ if (!shouldShowValidation) {
496
+ return false;
497
+ }
498
+ const requiredError = isFieldRequired(field) && isFieldBlank(field);
499
+ return requiredError || hasInvalidPostalCodeFormat(field) || hasInvalidEmailFormat(field);
500
+ };
501
+ const handleFieldBlur = field => {
502
+ if (!isFieldRequired(field) && field !== 'email' && field !== 'postalCode') {
503
+ return;
504
+ }
505
+ setTouchedFields(prev => ({
506
+ ...prev,
507
+ [field]: true
508
+ }));
509
+ };
510
+ const getFieldErrorMessage = field => {
511
+ if (!shouldShowFieldError(field)) {
512
+ return '';
513
+ }
514
+ if (isFieldRequired(field) && isFieldBlank(field)) {
515
+ return areLabelsLoading ? '' : fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.fieldRequired;
516
+ }
517
+ if (hasInvalidPostalCodeFormat(field)) {
518
+ return areLabelsLoading ? '' : fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.invalidPostalCode;
519
+ }
520
+ if (hasInvalidEmailFormat(field)) {
521
+ return areLabelsLoading ? '' : fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.invalidEmail;
522
+ }
523
+ return '';
524
+ };
525
+ const hasVisibleFieldError = field => shouldShowFieldError(field);
526
+ const handleEditableFieldChange = (field, value) => {
527
+ const normalizedValue = (0, _validators.normalizeAddressFieldValue)(field, value);
528
+ if (field === lookupField) {
529
+ setShowValidationSuggestions(false);
530
+ }
531
+ setShowMapModal(false);
532
+ setPendingMapOpen(false);
533
+ mapValidationStartedRef.current = false;
534
+ setMapAddressLines([]);
535
+ clearServiceErrors({
536
+ custom: true
537
+ });
538
+ const requiredFieldsForVerification = (resolvedAddressForm === null || resolvedAddressForm === void 0 ? void 0 : resolvedAddressForm.requiredFields) || requiredFields;
539
+ const nextVerificationStatus = (0, _addressVerification.getManualEditVerificationStatus)(verificationStatus, field, lookupField, requiredFieldsForVerification);
540
+ const updatedFormData = (0, _addressVerification.withManualAddressVerificationEdit)(stripUnusedEmail({
541
+ ...stripGeocode(formData),
542
+ [field]: normalizedValue
543
+ }), field, nextVerificationStatus);
544
+ setFormData(updatedFormData);
545
+ setVerificationStatus(nextVerificationStatus);
546
+ if (fieldTimerRef.current) {
547
+ clearTimeout(fieldTimerRef.current);
548
+ }
549
+ fieldTimerRef.current = setTimeout(() => {
550
+ const payload = {
551
+ type: 'field-change',
552
+ field,
553
+ value: normalizedValue,
554
+ address: updatedFormData,
555
+ status: (0, _addressStatus.buildAddressStatus)({
556
+ countryCode,
557
+ addressForm: resolvedAddressForm,
558
+ formData: updatedFormData,
559
+ verificationStatus: nextVerificationStatus,
560
+ mode,
561
+ requireValidation: allowValidation
562
+ })
563
+ };
564
+ if (hasEmailField && field === 'email' && value) {
565
+ payload.isValidEmail = (0, _validators.isValidEmail)(normalizedValue);
566
+ }
567
+ onSelect === null || onSelect === void 0 || onSelect(payload);
568
+ }, 500);
569
+ };
570
+ const handleSelect = address => {
571
+ clearServiceErrors({
572
+ lookup: true,
573
+ validation: false,
574
+ custom: true
575
+ });
576
+ if (address.entries > 1 || address.entries === 1 && address.isDetailed === false) {
577
+ shouldSearchRef.current = true;
578
+ appliedSelectionKeyRef.current = null;
579
+ setSelectedIntlAddressId(address.address_id);
580
+ setQuery(address.displayText);
581
+ } else {
582
+ shouldSearchRef.current = false;
583
+ appliedSelectionKeyRef.current = null;
584
+ setSelectedIntlAddressId(null);
585
+ setSelectedAddress(address);
586
+ setQuery('');
587
+ }
588
+ };
589
+ const handleValidationSelect = validatedAddress => {
590
+ var _validatedAddress$met, _validatedAddress$met2;
591
+ clearServiceErrors({
592
+ lookup: false,
593
+ validation: true,
594
+ custom: false
595
+ });
596
+ const config = resolvedAddressForm.addressElements || [];
597
+ const {
598
+ mapped: formattedMapped,
599
+ mapAddressLines: nextMapAddressLines
600
+ } = (0, _addressMapping.buildIntlConfiguredVerificationMapping)(validatedAddress, resolvedAddressForm);
601
+ let mapped = (0, _addressMapping.preserveBlankConfiguredMappings)(formattedMapped, formData, config, 'verification');
602
+
603
+ // Apply address wrapping if enabled
604
+ if (resolvedAddressForm.wrapAddress1ToAddress2) {
605
+ const address1Field = config.find(f => f.field === 'address1');
606
+ if (address1Field !== null && address1Field !== void 0 && address1Field.maxLength) {
607
+ const wrapped = (0, _addressWrapper.wrapAddress1ToAddress2)(mapped.address1, mapped.address2, address1Field.maxLength);
608
+ mapped.address1 = wrapped.address1;
609
+ mapped.address2 = wrapped.address2;
610
+ }
611
+ }
612
+ const updatedFormData = {
613
+ ...stripGeocode(formData),
614
+ ...mapped,
615
+ email: formData.email,
616
+ latitude: (_validatedAddress$met = validatedAddress.metadata) === null || _validatedAddress$met === void 0 ? void 0 : _validatedAddress$met.latitude,
617
+ longitude: (_validatedAddress$met2 = validatedAddress.metadata) === null || _validatedAddress$met2 === void 0 ? void 0 : _validatedAddress$met2.longitude
618
+ };
619
+ const normalizedFormData = buildTrackedAddress(updatedFormData, 'verified', 'smarty-intl');
620
+ setShowValidationSuggestions(false);
621
+ setValidationTriggerSource(null);
622
+ setFormData(normalizedFormData);
623
+ setMapAddressLines(nextMapAddressLines);
624
+ lastCommittedAddressRef.current = normalizedFormData;
625
+ lookupDraftOriginalRef.current = normalizedFormData;
626
+ setHasPendingLookupDraft(false);
627
+ const currentVerificationStatus = 'verified';
628
+ setVerificationStatus(currentVerificationStatus);
629
+ autoApplyValidationStartedRef.current = false;
630
+ if (pendingMapOpen) {
631
+ setPendingMapOpen(false);
632
+ setShowMapModal(true);
633
+ }
634
+ mapValidationStartedRef.current = false;
635
+ onSelect === null || onSelect === void 0 || onSelect({
636
+ type: 'validation',
637
+ address: normalizedFormData,
638
+ verification: validatedAddress.analysis,
639
+ status: (0, _addressStatus.buildAddressStatus)({
640
+ countryCode,
641
+ addressForm: resolvedAddressForm,
642
+ formData: normalizedFormData,
643
+ verificationStatus: currentVerificationStatus,
644
+ mode,
645
+ requireValidation: allowValidation
646
+ })
647
+ });
648
+ };
649
+ const handleValidate = () => {
650
+ if (!allowValidation) {
651
+ return;
652
+ }
653
+ clearServiceErrors({
654
+ lookup: false,
655
+ validation: true,
656
+ custom: false
657
+ });
658
+ setShowValidationSuggestions(true);
659
+ setValidationTriggerSource('manual');
660
+ setValidationTrigger(prev => prev + 1);
661
+ };
662
+ const getFieldKey = fieldConfig => fieldConfig.field;
663
+ const getSuggestionKey = suggestion => suggestion.address_id || [suggestion.displayText, suggestion.street, suggestion.address_text, suggestion.locality, suggestion.administrative_area, suggestion.postal_code, suggestion.entries].filter(value => value !== undefined && value !== null).join('|');
664
+ const getValidationSuggestionKey = suggestion => {
665
+ var _suggestion$component, _suggestion$component2, _suggestion$component3, _suggestion$component4, _suggestion$component5;
666
+ return [suggestion.displayText, suggestion.address1, suggestion.address2, suggestion.address3, (_suggestion$component = suggestion.components) === null || _suggestion$component === void 0 ? void 0 : _suggestion$component.thoroughfare, (_suggestion$component2 = suggestion.components) === null || _suggestion$component2 === void 0 ? void 0 : _suggestion$component2.premise, (_suggestion$component3 = suggestion.components) === null || _suggestion$component3 === void 0 ? void 0 : _suggestion$component3.locality, (_suggestion$component4 = suggestion.components) === null || _suggestion$component4 === void 0 ? void 0 : _suggestion$component4.administrative_area, (_suggestion$component5 = suggestion.components) === null || _suggestion$component5 === void 0 ? void 0 : _suggestion$component5.postal_code].filter(value => value !== undefined && value !== null).join('|');
667
+ };
668
+ const getIntlValidationSummaryTitle = suggestion => {
669
+ var _suggestion$validatio, _suggestion$validatio2, _suggestion$validatio3, _suggestion$validatio4, _suggestion$validatio5, _suggestion$validatio6, _suggestion$validatio7;
670
+ const code = (_suggestion$validatio = suggestion.validationSummary) === null || _suggestion$validatio === void 0 ? void 0 : _suggestion$validatio.code;
671
+ switch (code) {
672
+ case 'verified':
673
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlVerifiedTitle) || ((_suggestion$validatio2 = suggestion.validationSummary) === null || _suggestion$validatio2 === void 0 ? void 0 : _suggestion$validatio2.title) || 'Verified';
674
+ case 'verified-sub-building-changed':
675
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlVerifiedSubBuildingChangedTitle) || ((_suggestion$validatio3 = suggestion.validationSummary) === null || _suggestion$validatio3 === void 0 ? void 0 : _suggestion$validatio3.title) || 'Verified with unit changes';
676
+ case 'partial':
677
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlPartialTitle) || ((_suggestion$validatio4 = suggestion.validationSummary) === null || _suggestion$validatio4 === void 0 ? void 0 : _suggestion$validatio4.title) || 'Partially verified';
678
+ case 'ambiguous':
679
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlAmbiguousTitle) || ((_suggestion$validatio5 = suggestion.validationSummary) === null || _suggestion$validatio5 === void 0 ? void 0 : _suggestion$validatio5.title) || 'Multiple matches found';
680
+ case 'not-verified':
681
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlNotVerifiedTitle) || ((_suggestion$validatio6 = suggestion.validationSummary) === null || _suggestion$validatio6 === void 0 ? void 0 : _suggestion$validatio6.title) || 'Not verified';
682
+ default:
683
+ return ((_suggestion$validatio7 = suggestion.validationSummary) === null || _suggestion$validatio7 === void 0 ? void 0 : _suggestion$validatio7.title) || 'Unknown';
684
+ }
685
+ };
686
+ const getIntlValidationSummaryMessage = suggestion => {
687
+ var _suggestion$validatio8, _suggestion$validatio9, _suggestion$validatio0, _suggestion$validatio1, _suggestion$validatio10, _suggestion$validatio11, _suggestion$validatio12;
688
+ const code = (_suggestion$validatio8 = suggestion.validationSummary) === null || _suggestion$validatio8 === void 0 ? void 0 : _suggestion$validatio8.code;
689
+ switch (code) {
690
+ case 'verified':
691
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlVerifiedMessage) || ((_suggestion$validatio9 = suggestion.validationSummary) === null || _suggestion$validatio9 === void 0 ? void 0 : _suggestion$validatio9.message) || '';
692
+ case 'verified-sub-building-changed':
693
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlVerifiedSubBuildingChangedMessage) || ((_suggestion$validatio0 = suggestion.validationSummary) === null || _suggestion$validatio0 === void 0 ? void 0 : _suggestion$validatio0.message) || '';
694
+ case 'partial':
695
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlPartialMessage) || ((_suggestion$validatio1 = suggestion.validationSummary) === null || _suggestion$validatio1 === void 0 ? void 0 : _suggestion$validatio1.message) || '';
696
+ case 'ambiguous':
697
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlAmbiguousMessage) || ((_suggestion$validatio10 = suggestion.validationSummary) === null || _suggestion$validatio10 === void 0 ? void 0 : _suggestion$validatio10.message) || '';
698
+ case 'not-verified':
699
+ return (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlNotVerifiedMessage) || ((_suggestion$validatio11 = suggestion.validationSummary) === null || _suggestion$validatio11 === void 0 ? void 0 : _suggestion$validatio11.message) || '';
700
+ default:
701
+ return ((_suggestion$validatio12 = suggestion.validationSummary) === null || _suggestion$validatio12 === void 0 ? void 0 : _suggestion$validatio12.message) || '';
702
+ }
703
+ };
704
+ const handleOpenMap = () => {
705
+ if (hasMapCoordinates) {
706
+ setShowMapModal(true);
707
+ return;
708
+ }
709
+ if (!canAttemptMap || validationLoading) {
710
+ return;
711
+ }
712
+ clearServiceErrors({
713
+ lookup: false,
714
+ validation: true,
715
+ custom: false
716
+ });
717
+ mapValidationStartedRef.current = false;
718
+ setPendingMapOpen(true);
719
+ setShowValidationSuggestions(true);
720
+ setValidationTriggerSource('map');
721
+ setValidationTrigger(prev => prev + 1);
722
+ };
723
+ const renderLookupActions = () => /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
724
+ className: "ns-address-lookup__actions",
725
+ children: [showMapAction && /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
726
+ type: "button",
727
+ onClick: handleOpenMap,
728
+ className: "ns-address-lookup__toggle ns-address-lookup__toggle--icon",
729
+ title: fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.openInMaps,
730
+ "aria-label": fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.openInMaps,
731
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_LocationPinIcon.default, {
732
+ className: "ns-address-lookup__icon"
733
+ })
734
+ }), !isAutocompleteMode && allowValidation && showManualValidateButton && /*#__PURE__*/(0, _jsxRuntime.jsx)(_foundationUiComponents.NsButton, {
735
+ type: "button",
736
+ onClick: handleValidate,
737
+ disabled: validationLoading || !String(formData[lookupField] || '').trim(),
738
+ variant: "contained",
739
+ color: "primary",
740
+ size: "small",
741
+ children: validationLoading ? 'Validating...' : 'Validate Address'
742
+ }), entryMode === 'both' && /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
743
+ type: "button",
744
+ onClick: () => {
745
+ setShowValidationSuggestions(false);
746
+ setShowMapModal(false);
747
+ setPendingMapOpen(false);
748
+ setValidationTriggerSource(null);
749
+ clearServiceErrors({
750
+ custom: true
751
+ });
752
+ setMode(mode === 'autocomplete' ? 'manual' : 'autocomplete');
753
+ },
754
+ className: "ns-address-lookup__toggle",
755
+ children: mode === 'autocomplete' ? fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.switchToManualEntry : fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.switchToAutocomplete
756
+ })]
757
+ });
758
+ const getLookupInputElement = () => typeof document !== 'undefined' ? document.getElementById(getFieldId(lookupField)) : null;
759
+ const getVisibleSuggestionButtons = () => {
760
+ var _componentRef$current2;
761
+ return Array.from(((_componentRef$current2 = componentRef.current) === null || _componentRef$current2 === void 0 ? void 0 : _componentRef$current2.querySelectorAll(LOOKUP_LIST_BUTTON_SELECTOR)) || []);
762
+ };
763
+ const handleLookupInputKeyDown = event => {
764
+ if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') {
765
+ return;
766
+ }
767
+ const suggestionButtons = getVisibleSuggestionButtons();
768
+ if (suggestionButtons.length === 0) {
769
+ return;
770
+ }
771
+ event.preventDefault();
772
+ const targetButton = event.key === 'ArrowUp' ? suggestionButtons[suggestionButtons.length - 1] : suggestionButtons[0];
773
+ targetButton === null || targetButton === void 0 || targetButton.focus();
774
+ };
775
+ const handleSuggestionButtonKeyDown = (event, index) => {
776
+ const suggestionButtons = getVisibleSuggestionButtons();
777
+ if (suggestionButtons.length === 0) {
778
+ return;
779
+ }
780
+ if (event.key === 'ArrowDown') {
781
+ var _suggestionButtons;
782
+ event.preventDefault();
783
+ if (index === suggestionButtons.length - 1) {
784
+ var _getLookupInputElemen2;
785
+ (_getLookupInputElemen2 = getLookupInputElement()) === null || _getLookupInputElemen2 === void 0 || _getLookupInputElemen2.focus();
786
+ return;
787
+ }
788
+ (_suggestionButtons = suggestionButtons[index + 1]) === null || _suggestionButtons === void 0 || _suggestionButtons.focus();
789
+ return;
790
+ }
791
+ if (event.key === 'ArrowUp') {
792
+ var _suggestionButtons2;
793
+ event.preventDefault();
794
+ if (index === 0) {
795
+ var _getLookupInputElemen3;
796
+ (_getLookupInputElemen3 = getLookupInputElement()) === null || _getLookupInputElemen3 === void 0 || _getLookupInputElemen3.focus();
797
+ return;
798
+ }
799
+ (_suggestionButtons2 = suggestionButtons[index - 1]) === null || _suggestionButtons2 === void 0 || _suggestionButtons2.focus();
800
+ return;
801
+ }
802
+ if (event.key === 'Escape') {
803
+ var _getLookupInputElemen4;
804
+ event.preventDefault();
805
+ (_getLookupInputElemen4 = getLookupInputElement()) === null || _getLookupInputElemen4 === void 0 || _getLookupInputElemen4.focus();
806
+ }
807
+ };
808
+ const renderLookupLists = isLookupField => {
809
+ if (readOnlyView) {
810
+ return null;
811
+ }
812
+ const shouldShowValidationResults = allowValidation && showValidationSuggestions && (!isAutocompleteMode || ['map', 'initial'].includes(validationTriggerSource));
813
+ const shouldShowNoResults = shouldShowValidationResults && !validationLoading && validationSuggestions.length === 0 && !validationServiceError && (validationTriggerSource !== 'map' || mapValidationStartedRef.current);
814
+ if (!isLookupField) {
815
+ return null;
816
+ }
817
+ if (loading && isAutocompleteMode) {
818
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
819
+ className: "ns-address-lookup__spinner"
820
+ });
821
+ }
822
+ if (suggestions.length > 0 && !selectedAddress && isAutocompleteMode) {
823
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
824
+ ref: suggestionsListRef,
825
+ className: "ns-address-lookup__list ns-address-lookup__list--suggestions",
826
+ children: suggestions.map((suggestion, index) => /*#__PURE__*/(0, _jsxRuntime.jsx)("li", {
827
+ className: ['ns-address-lookup__list-item', index < suggestions.length - 1 ? 'ns-address-lookup__list-item--divided' : ''].filter(Boolean).join(' '),
828
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
829
+ type: "button",
830
+ onClick: () => handleSelect(suggestion),
831
+ onKeyDown: event => handleSuggestionButtonKeyDown(event, index),
832
+ className: "ns-address-lookup__list-button",
833
+ children: suggestion.displayText
834
+ })
835
+ }, getSuggestionKey(suggestion)))
836
+ });
837
+ }
838
+ if (shouldShowValidationResults && validationSuggestions.length > 0) {
839
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
840
+ className: "ns-address-lookup__list ns-address-lookup__list--validation-inline",
841
+ children: validationSuggestions.map((suggestion, index) => {
842
+ var _suggestion$validatio13;
843
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("li", {
844
+ className: ['ns-address-lookup__list-item', ((_suggestion$validatio13 = suggestion.validationSummary) === null || _suggestion$validatio13 === void 0 ? void 0 : _suggestion$validatio13.code) === 'not-verified' ? 'ns-address-lookup__list-item--not-verified' : '', index < validationSuggestions.length - 1 ? 'ns-address-lookup__list-item--divided' : ''].filter(Boolean).join(' '),
845
+ children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("button", {
846
+ type: "button",
847
+ onClick: () => handleValidationSelect(suggestion),
848
+ onKeyDown: event => handleSuggestionButtonKeyDown(event, index),
849
+ className: "ns-address-lookup__list-button",
850
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
851
+ className: "ns-address-lookup__list-title",
852
+ children: getIntlValidationSummaryTitle(suggestion)
853
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
854
+ className: "ns-address-lookup__list-subtitle",
855
+ children: suggestion.displayText
856
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
857
+ className: "ns-address-lookup__list-subtitle",
858
+ children: getIntlValidationSummaryMessage(suggestion)
859
+ })]
860
+ })
861
+ }, getValidationSuggestionKey(suggestion));
862
+ })
863
+ });
864
+ }
865
+ if (shouldShowNoResults) {
866
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
867
+ className: "ns-address-lookup__message",
868
+ children: fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.intlNoResults
869
+ });
870
+ }
871
+ return null;
872
+ };
873
+ (0, _react.useEffect)(() => {
874
+ if (['autocomplete', 'map'].includes(validationTriggerSource) && validationSuggestions.length === 1 && autoApplyValidationStartedRef.current) {
875
+ handleValidationSelect(validationSuggestions[0]);
876
+ }
877
+ if (validationTriggerSource === 'initial' && validationSuggestions.length === 1 && autoApplyValidationStartedRef.current && isAcceptableInitialValidationResult(validationSuggestions[0])) {
878
+ handleValidationSelect(validationSuggestions[0]);
879
+ }
880
+ }, [validationSuggestions, validationTriggerSource]);
881
+ const renderField = fieldConfig => {
882
+ const isLookupField = fieldConfig.field === lookupField;
883
+ const fieldId = getFieldId(fieldConfig.field);
884
+ const fieldLabel = areLabelsLoading ? '' : (fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels[fieldConfig.field]) || fieldConfig.label;
885
+ const fieldError = getFieldErrorMessage(fieldConfig.field);
886
+ const isInvalid = hasVisibleFieldError(fieldConfig.field);
887
+ const isReadOnly = readOnlyView || !fieldConfig.editable && !isLookupField && isAutocompleteMode;
888
+ const fieldWidth = fieldConfig.width === 'full' ? '100%' : fieldConfig.width === 'third' ? '33.333333%' : '50%';
889
+ const inputClassName = ['ns-address-lookup__input', isReadOnly ? 'ns-address-lookup__input--readonly' : '', isLookupField && loading ? 'ns-address-lookup__input--loading' : ''].filter(Boolean).join(' ');
890
+ const labelRowClassName = ['ns-address-lookup__label-row', isLookupField ? '' : 'ns-address-lookup__label-row--field'].filter(Boolean).join(' ');
891
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
892
+ className: "ns-address-lookup__field",
893
+ style: {
894
+ width: fieldWidth
895
+ },
896
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
897
+ className: labelRowClassName,
898
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("label", {
899
+ htmlFor: fieldId,
900
+ className: "MuiFormLabel-root MuiFormLabel-colorPrimary css-1ef1ceb-MuiFormLabel-root",
901
+ children: [fieldLabel, fieldConfig.required && !areLabelsLoading && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
902
+ "aria-hidden": "true",
903
+ className: "MuiFormLabel-asterisk",
904
+ children: ' *'
905
+ })]
906
+ }), isLookupField && !readOnlyView && renderLookupActions()]
907
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_foundationUiComponents.NsInputField, {
908
+ id: fieldId,
909
+ type: "text",
910
+ value: formData[fieldConfig.field] || '',
911
+ "aria-invalid": shouldShowFieldError(fieldConfig.field) ? 'true' : undefined,
912
+ fieldState: isInvalid ? 'error' : undefined,
913
+ helperText: fieldError || undefined,
914
+ required: fieldConfig.required,
915
+ onBlur: () => handleFieldBlur(fieldConfig.field),
916
+ onKeyDown: isLookupField && !readOnlyView ? handleLookupInputKeyDown : undefined,
917
+ onChange: e => {
918
+ const filtered = (0, _validators.filterAllowedCharacters)(e.target.value, fieldConfig.allowedCharacters);
919
+ if (readOnlyView) {
920
+ return;
921
+ }
922
+ if (isLookupField && isAutocompleteMode) {
923
+ handleInputChange({
924
+ ...e,
925
+ target: {
926
+ ...e.target,
927
+ value: filtered
928
+ }
929
+ });
930
+ } else {
931
+ handleEditableFieldChange(fieldConfig.field, filtered);
932
+ }
933
+ },
934
+ InputProps: {
935
+ readOnly: isReadOnly
936
+ },
937
+ inputProps: {
938
+ maxLength: fieldConfig.maxLength,
939
+ 'data-testid': getFieldTestId(fieldConfig.field)
940
+ },
941
+ className: inputClassName
942
+ }), renderLookupLists(isLookupField)]
943
+ }, getFieldKey(fieldConfig));
944
+ };
945
+ const renderAddressForm = () => {
946
+ const config = resolvedAddressForm === null || resolvedAddressForm === void 0 ? void 0 : resolvedAddressForm.addressElements;
947
+ if (!(config !== null && config !== void 0 && config.length)) {
948
+ if (isFormLoading) {
949
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
950
+ children: fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.loadingAddressForm
951
+ });
952
+ }
953
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
954
+ children: ["No form available for this country: ", countryCode]
955
+ });
956
+ }
957
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
958
+ children: config.map(renderField)
959
+ });
960
+ };
961
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
962
+ ref: componentRef,
963
+ className: "ns-address-lookup__panel",
964
+ children: [lookupServiceError && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
965
+ className: "ns-address-lookup__message",
966
+ children: lookupErrorMessage
967
+ }), customError && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
968
+ className: "ns-address-lookup__message",
969
+ children: customError
970
+ }), allowValidation && validationServiceError && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
971
+ className: "ns-address-lookup__message",
972
+ children: validationErrorMessage
973
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
974
+ className: "ns-address-lookup__form",
975
+ children: renderAddressForm()
976
+ }), showMapModal && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
977
+ className: "ns-address-lookup__modal-backdrop",
978
+ onClick: () => setShowMapModal(false),
979
+ children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
980
+ className: "ns-address-lookup__modal",
981
+ role: "dialog",
982
+ "aria-modal": "true",
983
+ "aria-label": fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.addressLocation,
984
+ onClick: event => event.stopPropagation(),
985
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
986
+ className: "ns-address-lookup__modal-header",
987
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
988
+ className: "ns-address-lookup__modal-title",
989
+ children: fieldLabels === null || fieldLabels === void 0 ? void 0 : fieldLabels.addressLocation
990
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
991
+ type: "button",
992
+ className: "ns-address-lookup__toggle",
993
+ "aria-label": "Close",
994
+ onClick: () => setShowMapModal(false),
995
+ children: "X"
996
+ })]
997
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
998
+ ref: mapContainerRef,
999
+ className: "ns-address-lookup__map"
1000
+ })]
1001
+ })
1002
+ })]
1003
+ });
1004
+ };
1005
+ SmartyIntlResults.propTypes = {
1006
+ countryCode: _propTypes.default.string.isRequired,
1007
+ onSelect: _propTypes.default.func,
1008
+ initialAddress: _componentPropTypes.addressPropType,
1009
+ entryMode: _propTypes.default.oneOf(['both', 'autocomplete', 'manual']),
1010
+ requireValidation: _propTypes.default.bool,
1011
+ readOnlyView: _propTypes.default.bool,
1012
+ showManualValidateButton: _propTypes.default.bool,
1013
+ validationRequestId: _propTypes.default.number,
1014
+ language: _propTypes.default.string,
1015
+ strings: _componentPropTypes.labelStringsPropType,
1016
+ formDefinition: _componentPropTypes.formDefinitionPropType,
1017
+ deferFormLookup: _propTypes.default.bool
1018
+ };
1019
+ var _default = exports.default = SmartyIntlResults;
1020
+ //# sourceMappingURL=SmartyIntlResults.js.map