@saar-ai/web-widget 0.11.0 → 0.13.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 (58) hide show
  1. package/dist/capture-card/capture-card-dom.d.ts +52 -0
  2. package/dist/capture-card/capture-card-dom.d.ts.map +1 -0
  3. package/dist/capture-card/capture-card-host.d.ts +9 -0
  4. package/dist/capture-card/capture-card-host.d.ts.map +1 -0
  5. package/dist/chunks/capture-card-BlJ6EkWn.js +156 -0
  6. package/dist/chunks/capture-card-dom-BqDqOvQF.js +383 -0
  7. package/dist/chunks/{chat-CU9VPqVO.js → chat-R7z4DtMM.js} +110 -8
  8. package/dist/chunks/form-fill-B-h6Uw1t.js +45 -0
  9. package/dist/chunks/form-fill-dom-skU-ZwLB.js +191 -0
  10. package/dist/chunks/{host-widget-BZKCb7yR.js → host-widget-BZAYL5tQ.js} +1370 -425
  11. package/dist/chunks/{schemas-YN3wGSGZ.js → schemas-B2B3k74u.js} +2427 -1167
  12. package/dist/chunks/{telemetry-B1xBTk1s.js → telemetry-3f--vvdX.js} +18 -12
  13. package/dist/concierge-shell.css +1 -1
  14. package/dist/consent/consent-prompt-dom.d.ts +57 -0
  15. package/dist/consent/consent-prompt-dom.d.ts.map +1 -0
  16. package/dist/consent/consent-prompt-preview.d.ts +16 -0
  17. package/dist/consent/consent-prompt-preview.d.ts.map +1 -0
  18. package/dist/dock/dock-renderer.d.ts +1 -1
  19. package/dist/dock/dock-renderer.d.ts.map +1 -1
  20. package/dist/dock/dock-row.d.ts +3 -23
  21. package/dist/dock/dock-row.d.ts.map +1 -1
  22. package/dist/dock/dock.d.ts +11 -0
  23. package/dist/dock/dock.d.ts.map +1 -1
  24. package/dist/flow-link.d.ts +13 -0
  25. package/dist/flow-link.d.ts.map +1 -0
  26. package/dist/form-fill/form-fill-dom.d.ts +39 -0
  27. package/dist/form-fill/form-fill-dom.d.ts.map +1 -0
  28. package/dist/format-money.d.ts +3 -2
  29. package/dist/format-money.d.ts.map +1 -1
  30. package/dist/generated/concierge-shell-css.d.ts.map +1 -1
  31. package/dist/handoff-initials.d.ts +34 -0
  32. package/dist/handoff-initials.d.ts.map +1 -0
  33. package/dist/host-widget.d.ts +8 -5
  34. package/dist/host-widget.d.ts.map +1 -1
  35. package/dist/index.d.ts +5 -1
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +139 -173
  38. package/dist/next/index.js +1 -1
  39. package/dist/use-chat-session.d.ts +23 -3
  40. package/dist/use-chat-session.d.ts.map +1 -1
  41. package/dist/use-voice-session.d.ts +39 -4
  42. package/dist/use-voice-session.d.ts.map +1 -1
  43. package/dist/voice-widget.d.ts +11 -5
  44. package/dist/voice-widget.d.ts.map +1 -1
  45. package/dist/widget/concierge-launcher.d.ts +16 -1
  46. package/dist/widget/concierge-launcher.d.ts.map +1 -1
  47. package/dist/widget/concierge-shell.d.ts +48 -2
  48. package/dist/widget/concierge-shell.d.ts.map +1 -1
  49. package/dist/widget/shell-renderer.d.ts +1 -1
  50. package/dist/widget/shell-renderer.d.ts.map +1 -1
  51. package/dist/widget/widget-view.d.ts +66 -2
  52. package/dist/widget/widget-view.d.ts.map +1 -1
  53. package/dist/widget-cart-summary.d.ts +4 -3
  54. package/dist/widget-cart-summary.d.ts.map +1 -1
  55. package/package.json +16 -16
  56. package/src/styles/brand.css +51 -0
  57. package/src/styles/concierge-shell-entry.css +13 -0
  58. package/src/styles/concierge.css +9 -38
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The capture card, plain DOM, in its own lazy chunk (`capture-card.mjs`).
3
+ * Both UI builds emit this chunk and it must be byte-identical between them,
4
+ * so it imports ONLY the zod-free `@saar-ai/widget-protocol/capture-card`
5
+ * rules: no React, no other module of this package (a test pins this).
6
+ * Tenant text is set with `textContent`, never HTML.
7
+ */
8
+ import { type CaptureCardConsentChannel, type CaptureCardValueError } from '@saar-ai/widget-protocol/capture-card';
9
+ export interface CaptureCardSpec {
10
+ readonly field: string;
11
+ readonly input: string;
12
+ readonly label: string;
13
+ readonly purpose?: string;
14
+ readonly choices?: readonly string[];
15
+ readonly maxLength?: number;
16
+ /** A phone card's default region: the country code is then optional. */
17
+ readonly region?: string;
18
+ /** Tenant wording beside an unticked box (ADR 0019). */
19
+ readonly consent?: {
20
+ readonly channel: CaptureCardConsentChannel;
21
+ readonly text: string;
22
+ };
23
+ }
24
+ export type CaptureCardAnswer = {
25
+ readonly field: string;
26
+ readonly value: string;
27
+ readonly consent?: boolean;
28
+ } | {
29
+ readonly field: string;
30
+ readonly refused: true;
31
+ };
32
+ export interface CaptureCardOptions {
33
+ readonly card: CaptureCardSpec;
34
+ readonly locale: string;
35
+ /** Off during a call: focus would pop a keyboard nobody asked for. */
36
+ readonly autoFocus: boolean;
37
+ readonly submit: (answer: CaptureCardAnswer) => Promise<number | null>;
38
+ readonly onAnswered: (outcome: 'saved' | 'declined') => void;
39
+ /** Asked for by a refused checkout: `'last'` is the final card before checkout opens. */
40
+ readonly checkout?: 'more' | 'last';
41
+ }
42
+ type CopyKey = 'checkoutIntro' | 'checkoutReady' | 'phoneLocal' | 'send' | 'refuse' | 'choose' | 'saved' | 'declined' | 'invalid' | 'closed' | 'failed' | CaptureCardValueError;
43
+ /** Lives here, not in `messages.ts`, so the copy ships in this chunk and not in `ui`. */
44
+ export declare const CAPTURE_CARD_COPY: Readonly<Record<'en' | 'ar' | 'hi', Readonly<Record<CopyKey, string>>>>;
45
+ /**
46
+ * Returns the teardown.
47
+ * @param root an empty element the host owns
48
+ * @param options the card, the locale and the two callbacks
49
+ */
50
+ export declare function mountCaptureCard(root: HTMLElement, options: CaptureCardOptions): () => void;
51
+ export {};
52
+ //# sourceMappingURL=capture-card-dom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"capture-card-dom.d.ts","sourceRoot":"","sources":["../../src/capture-card/capture-card-dom.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAGL,KAAK,yBAAyB,EAC9B,KAAK,qBAAqB,EAC3B,MAAM,uCAAuC,CAAC;AAE/C,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,wEAAwE;IACxE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,wDAAwD;IACxD,QAAQ,CAAC,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,yBAAyB,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;CAC3F;AAED,MAAM,MAAM,iBAAiB,GACzB;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,GAC9E;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAA;CAAE,CAAC;AAEvD,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,sEAAsE;IACtE,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,iBAAiB,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACvE,QAAQ,CAAC,UAAU,EAAE,CAAC,OAAO,EAAE,OAAO,GAAG,UAAU,KAAK,IAAI,CAAC;IAC7D,yFAAyF;IACzF,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACrC;AAED,KAAK,OAAO,GACR,eAAe,GACf,eAAe,GACf,YAAY,GACZ,MAAM,GACN,QAAQ,GACR,QAAQ,GACR,OAAO,GACP,UAAU,GACV,SAAS,GACT,QAAQ,GACR,QAAQ,GACR,qBAAqB,CAAC;AAE1B,yFAAyF;AACzF,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CACtC,MAAM,CAAC,IAAI,GAAG,IAAI,GAAG,IAAI,EAAE,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,CAuE9D,CAAC;AAuCF;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,kBAAkB,GAAG,MAAM,IAAI,CAuP3F"}
@@ -0,0 +1,9 @@
1
+ import type { CaptureCardOptions } from './capture-card-dom.js';
2
+ /**
3
+ * Loads the card chunk into an empty element. Keyed by the options object
4
+ * (one per card), so a re-render never redraws a card being typed into.
5
+ */
6
+ export declare function CaptureCardHost({ options }: {
7
+ readonly options: CaptureCardOptions;
8
+ }): import("react").JSX.Element;
9
+ //# sourceMappingURL=capture-card-host.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"capture-card-host.d.ts","sourceRoot":"","sources":["../../src/capture-card/capture-card-host.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhE;;;GAGG;AACH,wBAAgB,eAAe,CAAC,EAAE,OAAO,EAAE,EAAE;IAAE,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAA;CAAE,+BAgBpF"}
@@ -0,0 +1,156 @@
1
+ //#region ../widget-protocol/dist/capture-card.js
2
+ /**
3
+ * Capture card rules shared by the server and the widget's lazy card chunk.
4
+ * Must stay zod-free and import-free: the chunk may not pull zod. The check
5
+ * here must never refuse a value the server accepts (pinned in `apps/api`).
6
+ */
7
+ /** The `type` of the widget event that carries a card. */
8
+ var CAPTURE_CARD_EVENT_TYPE = "capture-card";
9
+ /** An unknown kind is drawn as a text box; the server re-validates. */
10
+ var CAPTURE_CARD_INPUTS = [
11
+ "email",
12
+ "phone",
13
+ "choice",
14
+ "text",
15
+ "number",
16
+ "date",
17
+ "url",
18
+ "country"
19
+ ];
20
+ /** Same as `@wesence/agent-core`'s `CAPTURE_FIELD_KEY_PATTERN`. */
21
+ var CAPTURE_CARD_FIELD_PATTERN = /^[a-z][a-z0-9_]{0,63}$/;
22
+ var CAPTURE_CARD_VALUE_MAX = 1e3;
23
+ /** ISO 3166-1 alpha-2, as the server stores a tenant's default phone region. */
24
+ var CAPTURE_CARD_REGION_PATTERN = /^[A-Z]{2}$/;
25
+ /** The outreach channels a card can ask consent for; the server's list is pinned to this. */
26
+ var CAPTURE_CARD_CONSENT_CHANNELS = [
27
+ "call",
28
+ "whatsapp",
29
+ "sms",
30
+ "email"
31
+ ];
32
+ /** Same cap as a tenant's published consent wording. */
33
+ var CAPTURE_CARD_CONSENT_MAX = 1e3;
34
+ /** zod's email pattern (the server's rule), copied verbatim; a test pins it to `z.regexes.email`. */
35
+ var CAPTURE_CARD_EMAIL_PATTERN = /^(?:[A-Za-z0-9_'+\-]+\.)*[A-Za-z0-9_'+\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$/;
36
+ /** A subset of the server's unsafe set, so the card never blocks a value the server takes. */
37
+ var CERTAINLY_UNSAFE = /[\p{Cc}\p{Zl}\p{Zp}]/u;
38
+ /** Any script's digits: libphonenumber reads Arabic-Indic digits too. */
39
+ var ANY_DIGIT = /\p{Nd}/gu;
40
+ /** Deliberately low: catches a missing number, never judges a real one. */
41
+ var PHONE_MIN_DIGITS = 4;
42
+ /** The server's number shape, once commas and spaces are dropped. */
43
+ var NUMBER = /^[-+]?\d+(\.\d+)?$/;
44
+ /** `YYYY-MM-DD`, what a date input sends; the server judges the calendar. */
45
+ var DATE = /^\d{4}-\d{2}-\d{2}$/;
46
+ var COUNTRY = /^[A-Za-z]{2}$/;
47
+ function isWebAddress(value) {
48
+ try {
49
+ const { protocol } = new URL(value);
50
+ return protocol === "http:" || protocol === "https:";
51
+ } catch {
52
+ return false;
53
+ }
54
+ }
55
+ /**
56
+ * Returns the trimmed value to send, or why it would be refused. Only trims:
57
+ * the server normalises.
58
+ * @param rules the card's input kind, cap and choices
59
+ * @param raw what the shopper typed or picked
60
+ */
61
+ function checkCaptureCardValue(rules, raw) {
62
+ const value = raw.trim();
63
+ if (value === "") return {
64
+ ok: false,
65
+ error: "empty"
66
+ };
67
+ if (CERTAINLY_UNSAFE.test(value)) return {
68
+ ok: false,
69
+ error: "characters"
70
+ };
71
+ if (rules.input === "email") {
72
+ if (value.length > 255) return {
73
+ ok: false,
74
+ error: "length"
75
+ };
76
+ return CAPTURE_CARD_EMAIL_PATTERN.test(value) ? {
77
+ ok: true,
78
+ value
79
+ } : {
80
+ ok: false,
81
+ error: "email"
82
+ };
83
+ }
84
+ if (rules.input === "phone") {
85
+ const digits = value.match(ANY_DIGIT)?.length ?? 0;
86
+ return (value.startsWith("+") || rules.region !== void 0) && digits >= PHONE_MIN_DIGITS && value.length <= 64 ? {
87
+ ok: true,
88
+ value
89
+ } : {
90
+ ok: false,
91
+ error: "phone"
92
+ };
93
+ }
94
+ if (rules.input === "number") return NUMBER.test(value.replace(/[,\s]/g, "")) ? {
95
+ ok: true,
96
+ value
97
+ } : {
98
+ ok: false,
99
+ error: "number"
100
+ };
101
+ if (rules.input === "date") return DATE.test(value) ? {
102
+ ok: true,
103
+ value
104
+ } : {
105
+ ok: false,
106
+ error: "date"
107
+ };
108
+ if (rules.input === "country") return COUNTRY.test(value) ? {
109
+ ok: true,
110
+ value
111
+ } : {
112
+ ok: false,
113
+ error: "country"
114
+ };
115
+ if (rules.input === "url") {
116
+ if (value.length > 1e3) return {
117
+ ok: false,
118
+ error: "length"
119
+ };
120
+ return isWebAddress(value) ? {
121
+ ok: true,
122
+ value
123
+ } : {
124
+ ok: false,
125
+ error: "url"
126
+ };
127
+ }
128
+ if (rules.input === "choice" && rules.choices !== void 0 && rules.choices.length > 0) {
129
+ const wanted = value.toLocaleLowerCase();
130
+ const match = rules.choices.find((choice) => choice.toLocaleLowerCase() === wanted);
131
+ return match === void 0 ? {
132
+ ok: false,
133
+ error: "choice"
134
+ } : {
135
+ ok: true,
136
+ value: match
137
+ };
138
+ }
139
+ const max = Math.min(rules.maxLength ?? 1e3, CAPTURE_CARD_VALUE_MAX);
140
+ return value.length > max ? {
141
+ ok: false,
142
+ error: "length"
143
+ } : {
144
+ ok: true,
145
+ value
146
+ };
147
+ }
148
+ /** `POST /widget/capture` status: 200 done, 400 value refused, 404 card closed, else retry. */
149
+ function captureCardOutcomeOf(status) {
150
+ if (status === 200) return "done";
151
+ if (status === 400) return "invalid";
152
+ if (status === 404) return "closed";
153
+ return "failed";
154
+ }
155
+ //#endregion
156
+ export { CAPTURE_CARD_INPUTS as a, captureCardOutcomeOf as c, CAPTURE_CARD_FIELD_PATTERN as i, checkCaptureCardValue as l, CAPTURE_CARD_CONSENT_MAX as n, CAPTURE_CARD_REGION_PATTERN as o, CAPTURE_CARD_EVENT_TYPE as r, CAPTURE_CARD_VALUE_MAX as s, CAPTURE_CARD_CONSENT_CHANNELS as t };
@@ -0,0 +1,383 @@
1
+ import { c as captureCardOutcomeOf, l as checkCaptureCardValue } from "./capture-card-BlJ6EkWn.js";
2
+ //#region src/capture-card/capture-card-dom.ts
3
+ /**
4
+ * The capture card, plain DOM, in its own lazy chunk (`capture-card.mjs`).
5
+ * Both UI builds emit this chunk and it must be byte-identical between them,
6
+ * so it imports ONLY the zod-free `@saar-ai/widget-protocol/capture-card`
7
+ * rules: no React, no other module of this package (a test pins this).
8
+ * Tenant text is set with `textContent`, never HTML.
9
+ */
10
+ /** Lives here, not in `messages.ts`, so the copy ships in this chunk and not in `ui`. */
11
+ var CAPTURE_CARD_COPY = {
12
+ en: {
13
+ checkoutIntro: "Needed before checkout.",
14
+ checkoutReady: "Thanks — you can check out now.",
15
+ phoneLocal: "Enter a phone number.",
16
+ send: "Send",
17
+ refuse: "I’d rather not say",
18
+ choose: "Choose one",
19
+ saved: "Thanks — sent.",
20
+ declined: "No problem — skipped.",
21
+ empty: "Please fill this in.",
22
+ length: "That is too long.",
23
+ characters: "That contains characters that can’t be used.",
24
+ email: "Enter an email address, like name@example.com.",
25
+ phone: "Enter the number with its country code, starting with +.",
26
+ choice: "Choose one of the options.",
27
+ number: "Enter a number, like 12.",
28
+ date: "Enter a date.",
29
+ url: "Enter a web address starting with https://.",
30
+ country: "Choose a country.",
31
+ invalid: "That doesn’t look right. Check it and try again.",
32
+ closed: "This form has closed. Tell the assistant instead.",
33
+ failed: "That couldn’t be sent. Try again."
34
+ },
35
+ ar: {
36
+ checkoutIntro: "مطلوب قبل إتمام الشراء.",
37
+ checkoutReady: "شكرًا — يمكنك إتمام الشراء الآن.",
38
+ phoneLocal: "يُرجى إدخال رقم هاتف.",
39
+ send: "إرسال",
40
+ refuse: "أفضّل عدم الإجابة",
41
+ choose: "يُرجى الاختيار",
42
+ saved: "شكرًا — تم الإرسال.",
43
+ declined: "لا بأس — تم التخطي.",
44
+ empty: "يُرجى ملء هذا الحقل.",
45
+ length: "النص طويل جدًا.",
46
+ characters: "يحتوي على أحرف لا يمكن استخدامها.",
47
+ email: "يُرجى إدخال بريد إلكتروني، مثل name@example.com.",
48
+ phone: "يُرجى إدخال الرقم مع رمز الدولة، بدءًا بـ +.",
49
+ choice: "يُرجى اختيار أحد الخيارات.",
50
+ number: "يُرجى إدخال رقم، مثل 12.",
51
+ date: "يُرجى إدخال تاريخ.",
52
+ url: "يُرجى إدخال عنوان موقع يبدأ بـ https://.",
53
+ country: "يُرجى اختيار دولة.",
54
+ invalid: "يبدو أن هناك خطأ. يُرجى التحقق منه والمحاولة مرة أخرى.",
55
+ closed: "تم إغلاق هذا النموذج. يمكنك إخبار المساعد بدلًا من ذلك.",
56
+ failed: "تعذّر الإرسال. يُرجى المحاولة مرة أخرى."
57
+ },
58
+ hi: {
59
+ checkoutIntro: "चेकआउट से पहले ज़रूरी।",
60
+ checkoutReady: "धन्यवाद — अब आप चेकआउट कर सकते हैं।",
61
+ phoneLocal: "फ़ोन नंबर दर्ज करें।",
62
+ send: "भेजें",
63
+ refuse: "मुझे नहीं बताना",
64
+ choose: "एक चुनें",
65
+ saved: "धन्यवाद — भेज दिया गया।",
66
+ declined: "कोई बात नहीं — छोड़ दिया गया।",
67
+ empty: "कृपया इसे भरें।",
68
+ length: "यह बहुत लंबा है।",
69
+ characters: "इसमें ऐसे अक्षर हैं जिनका उपयोग नहीं हो सकता।",
70
+ email: "ईमेल पता दर्ज करें, जैसे name@example.com।",
71
+ phone: "देश कोड के साथ नंबर दर्ज करें, जो + से शुरू हो।",
72
+ choice: "विकल्पों में से एक चुनें।",
73
+ number: "कोई संख्या दर्ज करें, जैसे 12।",
74
+ date: "कोई तारीख दर्ज करें।",
75
+ url: "https:// से शुरू होने वाला वेब पता दर्ज करें।",
76
+ country: "कोई देश चुनें।",
77
+ invalid: "यह सही नहीं लग रहा। कृपया जाँचकर फिर से कोशिश करें।",
78
+ closed: "यह फ़ॉर्म बंद हो गया है। इसके बजाय सहायक को बताएँ।",
79
+ failed: "भेजा नहीं जा सका। कृपया फिर से कोशिश करें।"
80
+ }
81
+ };
82
+ var RTL = /^(ar|fa|he|ur)\b/i;
83
+ /** Input type, input mode and autocomplete per kind; each is typed left to right. */
84
+ var INPUT_KINDS = {
85
+ email: [
86
+ "email",
87
+ "email",
88
+ "email"
89
+ ],
90
+ phone: [
91
+ "tel",
92
+ "tel",
93
+ "tel"
94
+ ],
95
+ url: [
96
+ "url",
97
+ "url",
98
+ "url"
99
+ ],
100
+ number: [
101
+ "text",
102
+ "decimal",
103
+ ""
104
+ ],
105
+ date: [
106
+ "date",
107
+ "",
108
+ ""
109
+ ],
110
+ country: [
111
+ "text",
112
+ "",
113
+ "country"
114
+ ]
115
+ };
116
+ /** Region codes a browser names that are not countries the server stores. */
117
+ var NOT_COUNTRIES = /^(?:A[ACN]|BU|C[PQS]|D[DGY]|E[AUZ]|FX|HV|IC|NH|Q[M-Z]|RH|SU|T[AP]|U[KN]|VD|X.|Y[DU]|Z[RZ])$/;
118
+ /** Every country the browser can name, by name in the shopper's language; `null` without `Intl.DisplayNames`. */
119
+ function countryChoices(locale) {
120
+ let names;
121
+ try {
122
+ names = new Intl.DisplayNames([locale, "en"], {
123
+ type: "region",
124
+ fallback: "none"
125
+ });
126
+ } catch {
127
+ return null;
128
+ }
129
+ const out = [];
130
+ for (let a = 65; a < 91; a++) for (let b = 65; b < 91; b++) {
131
+ const code = String.fromCharCode(a, b);
132
+ const name = NOT_COUNTRIES.test(code) ? void 0 : names.of(code);
133
+ if (name) out.push([code, name]);
134
+ }
135
+ return out.sort((x, y) => x[1].localeCompare(y[1], locale));
136
+ }
137
+ var nextId = 0;
138
+ /**
139
+ * Returns the teardown.
140
+ * @param root an empty element the host owns
141
+ * @param options the card, the locale and the two callbacks
142
+ */
143
+ function mountCaptureCard(root, options) {
144
+ const { card } = options;
145
+ const lang = options.locale.slice(0, 2).toLowerCase();
146
+ const copy = CAPTURE_CARD_COPY[lang === "ar" || lang === "hi" ? lang : "en"];
147
+ const doc = root.ownerDocument;
148
+ const el = (tag, text) => {
149
+ const node = doc.createElement(tag);
150
+ if (text !== void 0) node.textContent = text;
151
+ return node;
152
+ };
153
+ const id = `wgt-capture-${++nextId}`;
154
+ let live = true;
155
+ let busy = false;
156
+ const form = el("form");
157
+ form.noValidate = true;
158
+ form.dir = RTL.test(options.locale) ? "rtl" : "ltr";
159
+ form.dataset["captureField"] = card.field;
160
+ Object.assign(form.style, {
161
+ display: "grid",
162
+ gap: "8px",
163
+ margin: "8px 0",
164
+ padding: "12px",
165
+ borderRadius: "12px",
166
+ background: "var(--widget-card)",
167
+ color: "var(--widget-foreground)",
168
+ border: "1px solid var(--widget-chip)",
169
+ font: "inherit",
170
+ fontSize: "14px"
171
+ });
172
+ if (options.checkout) {
173
+ const intro = el("p", copy.checkoutIntro);
174
+ Object.assign(intro.style, {
175
+ margin: "0",
176
+ color: "var(--widget-foreground-muted)"
177
+ });
178
+ form.append(intro);
179
+ }
180
+ const label = el("label", card.label);
181
+ label.htmlFor = `${id}-input`;
182
+ label.style.fontWeight = "600";
183
+ form.append(label);
184
+ const describedBy = [];
185
+ if (card.purpose !== void 0) {
186
+ const purpose = el("p", card.purpose);
187
+ purpose.id = `${id}-purpose`;
188
+ Object.assign(purpose.style, {
189
+ margin: "0",
190
+ color: "var(--widget-foreground-muted)"
191
+ });
192
+ form.append(purpose);
193
+ describedBy.push(purpose.id);
194
+ }
195
+ const choices = card.input === "choice" && card.choices && card.choices.length > 0 ? card.choices : null;
196
+ const listed = choices !== null ? choices.map((choice) => [choice, choice]) : card.input === "country" ? countryChoices(options.locale) : null;
197
+ let field;
198
+ if (listed !== null) {
199
+ const select = el("select");
200
+ const placeholder = el("option", copy.choose);
201
+ placeholder.value = "";
202
+ select.append(placeholder);
203
+ for (const [value, text] of listed) {
204
+ const option = el("option", text);
205
+ option.value = value;
206
+ select.append(option);
207
+ }
208
+ field = select;
209
+ } else {
210
+ const input = el("input");
211
+ const kind = INPUT_KINDS[card.input];
212
+ input.type = kind?.[0] ?? "text";
213
+ if (kind) {
214
+ input.dir = "ltr";
215
+ if (kind[1]) input.inputMode = kind[1];
216
+ if (kind[2]) input.autocomplete = kind[2];
217
+ }
218
+ if (card.maxLength !== void 0) input.maxLength = card.maxLength;
219
+ field = input;
220
+ }
221
+ field.id = `${id}-input`;
222
+ field.name = card.field;
223
+ Object.assign(field.style, {
224
+ width: "100%",
225
+ boxSizing: "border-box",
226
+ padding: "8px 10px",
227
+ borderRadius: "8px",
228
+ border: "1px solid var(--widget-foreground-muted)",
229
+ background: "transparent",
230
+ color: "inherit",
231
+ font: "inherit"
232
+ });
233
+ form.append(field);
234
+ const error = el("p");
235
+ error.id = `${id}-error`;
236
+ error.setAttribute("aria-live", "polite");
237
+ Object.assign(error.style, {
238
+ margin: "0",
239
+ minHeight: "0",
240
+ color: "var(--widget-foreground)"
241
+ });
242
+ form.append(error);
243
+ describedBy.push(error.id);
244
+ field.setAttribute("aria-describedby", describedBy.join(" "));
245
+ let consent = null;
246
+ if (card.consent) {
247
+ const row = el("div");
248
+ Object.assign(row.style, {
249
+ display: "flex",
250
+ gap: "8px",
251
+ alignItems: "flex-start"
252
+ });
253
+ consent = el("input");
254
+ consent.type = "checkbox";
255
+ consent.id = `${id}-consent`;
256
+ Object.assign(consent.style, {
257
+ flex: "none",
258
+ width: "16px",
259
+ height: "16px",
260
+ margin: "2px 0 0",
261
+ accentColor: "var(--widget-accent)"
262
+ });
263
+ const wording = el("label", card.consent.text);
264
+ wording.htmlFor = consent.id;
265
+ Object.assign(wording.style, {
266
+ minWidth: "0",
267
+ overflowWrap: "anywhere"
268
+ });
269
+ row.append(consent, wording);
270
+ form.append(row);
271
+ }
272
+ const actions = el("div");
273
+ Object.assign(actions.style, {
274
+ display: "flex",
275
+ flexWrap: "wrap",
276
+ gap: "8px"
277
+ });
278
+ const send = el("button", copy.send);
279
+ send.type = "submit";
280
+ Object.assign(send.style, {
281
+ padding: "6px 14px",
282
+ borderRadius: "999px",
283
+ border: "0",
284
+ background: "var(--widget-accent)",
285
+ color: "var(--widget-accent-foreground)",
286
+ font: "inherit",
287
+ cursor: "pointer"
288
+ });
289
+ const refuse = el("button", copy.refuse);
290
+ refuse.type = "button";
291
+ Object.assign(refuse.style, {
292
+ padding: "6px 14px",
293
+ borderRadius: "999px",
294
+ border: "1px solid var(--widget-chip)",
295
+ background: "transparent",
296
+ color: "inherit",
297
+ font: "inherit",
298
+ cursor: "pointer"
299
+ });
300
+ actions.append(send, refuse);
301
+ form.append(actions);
302
+ const showError = (key) => {
303
+ error.textContent = key === null ? "" : copy[key === "phone" && card.region ? "phoneLocal" : key];
304
+ if (key === null) field.removeAttribute("aria-invalid");
305
+ else field.setAttribute("aria-invalid", "true");
306
+ };
307
+ const finish = (outcome) => {
308
+ const done = el("p", outcome === "declined" ? copy.declined : options.checkout === "last" ? copy.checkoutReady : copy.saved);
309
+ done.setAttribute("role", "status");
310
+ done.dir = form.dir;
311
+ Object.assign(done.style, {
312
+ margin: "8px 0",
313
+ color: "var(--widget-foreground-muted)"
314
+ });
315
+ form.replaceWith(done);
316
+ options.onAnswered(outcome);
317
+ };
318
+ const answer = async (body, outcome) => {
319
+ if (busy) return;
320
+ busy = true;
321
+ send.disabled = true;
322
+ refuse.disabled = true;
323
+ let status;
324
+ try {
325
+ status = await options.submit(body);
326
+ } catch {
327
+ status = null;
328
+ }
329
+ if (!live) return;
330
+ busy = false;
331
+ send.disabled = false;
332
+ refuse.disabled = false;
333
+ const next = captureCardOutcomeOf(status);
334
+ if (next === "done") {
335
+ finish(outcome);
336
+ return;
337
+ }
338
+ showError(next);
339
+ if (next === "closed") {
340
+ send.disabled = true;
341
+ refuse.disabled = true;
342
+ } else field.focus();
343
+ };
344
+ const onSubmit = (event) => {
345
+ event.preventDefault();
346
+ const checked = checkCaptureCardValue({
347
+ input: choices !== null ? "choice" : card.input,
348
+ ...card.maxLength !== void 0 ? { maxLength: card.maxLength } : {},
349
+ ...choices !== null ? { choices } : {},
350
+ ...card.region ? { region: card.region } : {}
351
+ }, field.value);
352
+ if (!checked.ok) {
353
+ showError(checked.error);
354
+ field.focus();
355
+ return;
356
+ }
357
+ showError(null);
358
+ answer({
359
+ field: card.field,
360
+ value: checked.value,
361
+ ...consent !== null ? { consent: consent.checked } : {}
362
+ }, "saved");
363
+ };
364
+ const onRefuse = () => {
365
+ showError(null);
366
+ answer({
367
+ field: card.field,
368
+ refused: true
369
+ }, "declined");
370
+ };
371
+ form.addEventListener("submit", onSubmit);
372
+ refuse.addEventListener("click", onRefuse);
373
+ root.replaceChildren(form);
374
+ if (options.autoFocus) field.focus();
375
+ return () => {
376
+ live = false;
377
+ form.removeEventListener("submit", onSubmit);
378
+ refuse.removeEventListener("click", onRefuse);
379
+ root.replaceChildren();
380
+ };
381
+ }
382
+ //#endregion
383
+ export { mountCaptureCard };