@hellotext/hellotext 2.5.7 → 2.5.8

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 (102) hide show
  1. package/README.md +0 -25
  2. package/dist/hellotext.js +1 -1
  3. package/index.d.ts +0 -6
  4. package/lib/api/acks.cjs +17 -31
  5. package/lib/api/acks.js +17 -42
  6. package/lib/api/businesses.cjs +14 -28
  7. package/lib/api/businesses.js +13 -36
  8. package/lib/api/events.cjs +26 -40
  9. package/lib/api/events.js +28 -52
  10. package/lib/api/forms.cjs +24 -40
  11. package/lib/api/forms.js +24 -57
  12. package/lib/api/identifications.cjs +15 -29
  13. package/lib/api/identifications.js +15 -40
  14. package/lib/api/index.cjs +25 -51
  15. package/lib/api/index.js +27 -54
  16. package/lib/api/push/identities.cjs +30 -46
  17. package/lib/api/push/identities.js +30 -63
  18. package/lib/api/response.cjs +30 -52
  19. package/lib/api/response.js +31 -60
  20. package/lib/api/submissions.cjs +18 -34
  21. package/lib/api/submissions.js +18 -48
  22. package/lib/api/webchat/messages.cjs +44 -64
  23. package/lib/api/webchat/messages.js +44 -80
  24. package/lib/api/webchats.cjs +37 -56
  25. package/lib/api/webchats.js +37 -65
  26. package/lib/api/whatsapp_widgets.cjs +49 -72
  27. package/lib/api/whatsapp_widgets.js +48 -91
  28. package/lib/builders/input_builder.cjs +36 -48
  29. package/lib/builders/input_builder.js +36 -48
  30. package/lib/builders/logo_builder.cjs +10 -29
  31. package/lib/builders/logo_builder.js +20 -28
  32. package/lib/channels/application_channel.cjs +136 -178
  33. package/lib/channels/application_channel.js +137 -180
  34. package/lib/channels/webchat_channel.cjs +125 -170
  35. package/lib/channels/webchat_channel.js +125 -170
  36. package/lib/controllers/form_controller.cjs +82 -121
  37. package/lib/controllers/form_controller.js +82 -130
  38. package/lib/controllers/message_controller.cjs +211 -282
  39. package/lib/controllers/message_controller.js +209 -284
  40. package/lib/controllers/mixins/usePopover.cjs +3 -6
  41. package/lib/controllers/mixins/usePopover.js +17 -22
  42. package/lib/controllers/webchat/emoji_picker_controller.cjs +106 -150
  43. package/lib/controllers/webchat/emoji_picker_controller.js +105 -170
  44. package/lib/controllers/webchat/useBehaviour.js +5 -5
  45. package/lib/controllers/webchat/useOpeningSequence.js +6 -6
  46. package/lib/controllers/webchat/useTeaser.js +9 -9
  47. package/lib/controllers/webchat_controller.cjs +916 -1088
  48. package/lib/controllers/webchat_controller.js +926 -1152
  49. package/lib/core/configuration/forms.cjs +21 -35
  50. package/lib/core/configuration/forms.js +20 -35
  51. package/lib/core/configuration/locale.cjs +36 -71
  52. package/lib/core/configuration/locale.js +36 -70
  53. package/lib/core/configuration/push.cjs +9 -21
  54. package/lib/core/configuration/push.js +8 -20
  55. package/lib/core/configuration/webchat.cjs +142 -182
  56. package/lib/core/configuration/webchat.js +145 -189
  57. package/lib/core/configuration/whatsapp.cjs +76 -102
  58. package/lib/core/configuration/whatsapp.js +76 -105
  59. package/lib/core/configuration.cjs +60 -78
  60. package/lib/core/configuration.js +59 -78
  61. package/lib/core/event.cjs +36 -59
  62. package/lib/core/event.js +35 -61
  63. package/lib/core/sanitize_html.js +4 -4
  64. package/lib/errors/invalid_event.cjs +5 -26
  65. package/lib/errors/invalid_event.js +5 -26
  66. package/lib/errors/not_initialized_error.cjs +5 -26
  67. package/lib/errors/not_initialized_error.js +5 -26
  68. package/lib/hellotext.cjs +185 -223
  69. package/lib/hellotext.js +188 -247
  70. package/lib/index.js +1 -1
  71. package/lib/models/business.cjs +114 -152
  72. package/lib/models/business.js +117 -162
  73. package/lib/models/cookies.cjs +34 -51
  74. package/lib/models/cookies.js +34 -51
  75. package/lib/models/fingerprint.cjs +8 -23
  76. package/lib/models/fingerprint.js +25 -58
  77. package/lib/models/form.cjs +106 -142
  78. package/lib/models/form.js +105 -153
  79. package/lib/models/form_collection.cjs +63 -101
  80. package/lib/models/form_collection.js +61 -107
  81. package/lib/models/page.cjs +106 -129
  82. package/lib/models/page.js +106 -131
  83. package/lib/models/push.cjs +278 -322
  84. package/lib/models/push.js +284 -357
  85. package/lib/models/query.cjs +21 -41
  86. package/lib/models/query.js +21 -41
  87. package/lib/models/session.cjs +33 -65
  88. package/lib/models/session.js +30 -63
  89. package/lib/models/user.cjs +29 -51
  90. package/lib/models/user.js +29 -51
  91. package/lib/models/utm.cjs +15 -27
  92. package/lib/models/utm.js +17 -32
  93. package/lib/models/webchat.cjs +52 -76
  94. package/lib/models/webchat.js +52 -90
  95. package/lib/models/whatsapp_widget.cjs +41 -59
  96. package/lib/models/whatsapp_widget.js +41 -73
  97. package/lib/vanilla.cjs +4 -7
  98. package/lib/vanilla.js +2 -1
  99. package/package.json +1 -9
  100. package/src/index.bundle.js +0 -1
  101. package/src/vanilla.js +2 -1
  102. package/styles/index.css +0 -30
@@ -7,11 +7,6 @@ exports.Business = void 0;
7
7
  var _locales = _interopRequireDefault(require("../locales"));
8
8
  var _businesses = _interopRequireDefault(require("../api/businesses"));
9
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
11
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
12
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
13
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
14
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
15
10
  const stylesheetAttribute = 'data-hellotext-stylesheet';
16
11
  const stylesheetLoadTimeout = 10000;
17
12
 
@@ -46,12 +41,11 @@ const stylesheetLoadTimeout = 10000;
46
41
  /**
47
42
  * Public business context used by the SDK for tracking, forms, and webchat defaults.
48
43
  */
49
- let Business = /*#__PURE__*/function () {
44
+ class Business {
50
45
  /**
51
46
  * @param {String} id - Public business id.
52
47
  */
53
- function Business(id) {
54
- _classCallCheck(this, Business);
48
+ constructor(id) {
55
49
  this.id = id;
56
50
  this.data = null;
57
51
  this.stylesheet = null;
@@ -66,158 +60,126 @@ let Business = /*#__PURE__*/function () {
66
60
  *
67
61
  * @returns {Promise<BusinessData|null>}
68
62
  */
69
- _createClass(Business, [{
70
- key: "hydrate",
71
- value: async function hydrate() {
72
- try {
73
- const response = await _businesses.default.get(this.id);
74
- if (response.ok === false) {
75
- return null;
76
- }
77
- const business = await response.json();
78
- if (!business) {
79
- return null;
80
- }
81
- this.setData(business);
82
- if (business.locale) {
83
- this.setLocale(business.locale);
84
- }
85
- return business;
86
- } catch (_error) {
63
+ async hydrate() {
64
+ try {
65
+ const response = await _businesses.default.get(this.id);
66
+ if (response.ok === false) {
87
67
  return null;
88
68
  }
89
- }
90
-
91
- /**
92
- * @param {BusinessData} data
93
- * @returns {void}
94
- */
95
- }, {
96
- key: "setData",
97
- value: function setData(data) {
98
- this.data = data;
99
- if (typeof document !== 'undefined' && data.style_url) {
100
- this.stylesheet = this.constructor.ensureStylesheet(data.style_url);
101
- this.stylesheetLoaded = this.constructor.waitForStylesheet(this.stylesheet);
102
- } else {
103
- this.stylesheet = null;
104
- this.stylesheetLoaded = Promise.resolve(false);
105
- }
106
- }
107
- }, {
108
- key: "subscription",
109
- get: function () {
110
- return this.data.subscription;
111
- }
112
- }, {
113
- key: "country",
114
- get: function () {
115
- return this.data.country;
116
- }
117
- }, {
118
- key: "enabledWhitelist",
119
- get: function () {
120
- return this.data.whitelist !== 'disabled';
121
- }
122
-
123
- /**
124
- * @param {String} locale
125
- * @returns {void}
126
- */
127
- }, {
128
- key: "setLocale",
129
- value: function setLocale(locale) {
130
- if (!_locales.default[locale]) {
131
- return console.warn(`Locale ${locale} not found`);
132
- }
133
- if (!this.data) {
134
- this.data = {};
69
+ const business = await response.json();
70
+ if (!business) {
71
+ return null;
135
72
  }
136
- this.data.locale = locale;
137
- }
138
- }, {
139
- key: "locale",
140
- get: function () {
141
- return _locales.default[this.data.locale];
142
- }
143
- }, {
144
- key: "features",
145
- get: function () {
146
- return this.data.features;
147
- }
148
- }], [{
149
- key: "stylesheetSelector",
150
- get: function () {
151
- return `link[rel="stylesheet"][${stylesheetAttribute}]`;
152
- }
153
- }, {
154
- key: "ensureStylesheet",
155
- value: function ensureStylesheet(styleUrl) {
156
- const href = this.normalizedStylesheetUrl(styleUrl);
157
- const existingLink = this.stylesheetLinks.find(link => link.href === href);
158
- if (existingLink) {
159
- existingLink.setAttribute(stylesheetAttribute, 'true');
160
- return existingLink;
73
+ this.setData(business);
74
+ if (business.locale) {
75
+ this.setLocale(business.locale);
161
76
  }
162
- const linkTag = document.createElement('link');
163
- linkTag.rel = 'stylesheet';
164
- linkTag.href = styleUrl;
165
- linkTag.setAttribute(stylesheetAttribute, 'true');
166
- this.waitForStylesheet(linkTag);
167
- document.head.append(linkTag);
168
- return linkTag;
77
+ return business;
78
+ } catch (_error) {
79
+ return null;
169
80
  }
170
- }, {
171
- key: "stylesheetLinks",
172
- get: function () {
173
- if (typeof document === 'undefined') return [];
174
- return Array.from(document.querySelectorAll(this.stylesheetSelector));
81
+ }
82
+
83
+ /**
84
+ * @param {BusinessData} data
85
+ * @returns {void}
86
+ */
87
+ setData(data) {
88
+ this.data = data;
89
+ if (typeof document !== 'undefined' && data.style_url) {
90
+ this.stylesheet = this.constructor.ensureStylesheet(data.style_url);
91
+ this.stylesheetLoaded = this.constructor.waitForStylesheet(this.stylesheet);
92
+ } else {
93
+ this.stylesheet = null;
94
+ this.stylesheetLoaded = Promise.resolve(false);
175
95
  }
176
- }, {
177
- key: "latestStylesheet",
178
- get: function () {
179
- return this.stylesheetLinks[this.stylesheetLinks.length - 1];
96
+ }
97
+ static get stylesheetSelector() {
98
+ return `link[rel="stylesheet"][${stylesheetAttribute}]`;
99
+ }
100
+ static ensureStylesheet(styleUrl) {
101
+ const href = this.normalizedStylesheetUrl(styleUrl);
102
+ const existingLink = this.stylesheetLinks.find(link => link.href === href);
103
+ if (existingLink) {
104
+ existingLink.setAttribute(stylesheetAttribute, 'true');
105
+ return existingLink;
180
106
  }
181
- }, {
182
- key: "normalizedStylesheetUrl",
183
- value: function normalizedStylesheetUrl(styleUrl) {
184
- try {
185
- return new URL(styleUrl, document.baseURI).href;
186
- } catch (_error) {
187
- return styleUrl;
188
- }
107
+ const linkTag = document.createElement('link');
108
+ linkTag.rel = 'stylesheet';
109
+ linkTag.href = styleUrl;
110
+ linkTag.setAttribute(stylesheetAttribute, 'true');
111
+ this.waitForStylesheet(linkTag);
112
+ document.head.append(linkTag);
113
+ return linkTag;
114
+ }
115
+ static get stylesheetLinks() {
116
+ if (typeof document === 'undefined') return [];
117
+ return Array.from(document.querySelectorAll(this.stylesheetSelector));
118
+ }
119
+ static get latestStylesheet() {
120
+ return this.stylesheetLinks[this.stylesheetLinks.length - 1];
121
+ }
122
+ static normalizedStylesheetUrl(styleUrl) {
123
+ try {
124
+ return new URL(styleUrl, document.baseURI).href;
125
+ } catch (_error) {
126
+ return styleUrl;
189
127
  }
190
- }, {
191
- key: "waitForStylesheet",
192
- value: function waitForStylesheet(linkTag) {
193
- if (!linkTag) return Promise.resolve(false);
194
- if (this.stylesheetIsLoaded(linkTag)) return Promise.resolve(true);
195
- if (linkTag.dataset.hellotextStylesheetLoaded === 'false') return Promise.resolve(false);
196
- if (linkTag._hellotextStylesheetLoaded) return linkTag._hellotextStylesheetLoaded;
197
- linkTag._hellotextStylesheetLoaded = new Promise(resolve => {
198
- let timeout;
199
- const finish = loaded => {
200
- clearTimeout(timeout);
201
- linkTag.removeEventListener('load', handleLoad);
202
- linkTag.removeEventListener('error', handleError);
203
- linkTag.dataset.hellotextStylesheetLoaded = loaded ? 'true' : 'false';
204
- resolve(loaded);
205
- };
206
- const handleLoad = () => finish(this.stylesheetIsLoaded(linkTag));
207
- const handleError = () => finish(false);
208
- linkTag.addEventListener('load', handleLoad);
209
- linkTag.addEventListener('error', handleError);
210
- timeout = setTimeout(() => finish(this.stylesheetIsLoaded(linkTag)), stylesheetLoadTimeout);
211
- if (timeout.unref) timeout.unref();
212
- });
213
- return linkTag._hellotextStylesheetLoaded;
128
+ }
129
+ static waitForStylesheet(linkTag) {
130
+ if (!linkTag) return Promise.resolve(false);
131
+ if (this.stylesheetIsLoaded(linkTag)) return Promise.resolve(true);
132
+ if (linkTag.dataset.hellotextStylesheetLoaded === 'false') return Promise.resolve(false);
133
+ if (linkTag._hellotextStylesheetLoaded) return linkTag._hellotextStylesheetLoaded;
134
+ linkTag._hellotextStylesheetLoaded = new Promise(resolve => {
135
+ let timeout;
136
+ const finish = loaded => {
137
+ clearTimeout(timeout);
138
+ linkTag.removeEventListener('load', handleLoad);
139
+ linkTag.removeEventListener('error', handleError);
140
+ linkTag.dataset.hellotextStylesheetLoaded = loaded ? 'true' : 'false';
141
+ resolve(loaded);
142
+ };
143
+ const handleLoad = () => finish(this.stylesheetIsLoaded(linkTag));
144
+ const handleError = () => finish(false);
145
+ linkTag.addEventListener('load', handleLoad);
146
+ linkTag.addEventListener('error', handleError);
147
+ timeout = setTimeout(() => finish(this.stylesheetIsLoaded(linkTag)), stylesheetLoadTimeout);
148
+ if (timeout.unref) timeout.unref();
149
+ });
150
+ return linkTag._hellotextStylesheetLoaded;
151
+ }
152
+ static stylesheetIsLoaded(linkTag) {
153
+ return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
154
+ }
155
+ get subscription() {
156
+ return this.data.subscription;
157
+ }
158
+ get country() {
159
+ return this.data.country;
160
+ }
161
+ get enabledWhitelist() {
162
+ return this.data.whitelist !== 'disabled';
163
+ }
164
+
165
+ /**
166
+ * @param {String} locale
167
+ * @returns {void}
168
+ */
169
+ setLocale(locale) {
170
+ if (!_locales.default[locale]) {
171
+ return console.warn(`Locale ${locale} not found`);
214
172
  }
215
- }, {
216
- key: "stylesheetIsLoaded",
217
- value: function stylesheetIsLoaded(linkTag) {
218
- return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
173
+ if (!this.data) {
174
+ this.data = {};
219
175
  }
220
- }]);
221
- return Business;
222
- }();
176
+ this.data.locale = locale;
177
+ }
178
+ get locale() {
179
+ return _locales.default[this.data.locale];
180
+ }
181
+ get features() {
182
+ return this.data.features;
183
+ }
184
+ }
223
185
  exports.Business = Business;
@@ -1,14 +1,7 @@
1
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
2
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
3
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
5
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
6
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
7
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
8
1
  import locales from '../locales';
9
2
  import BusinessesAPI from '../api/businesses';
10
- var stylesheetAttribute = 'data-hellotext-stylesheet';
11
- var stylesheetLoadTimeout = 10000;
3
+ const stylesheetAttribute = 'data-hellotext-stylesheet';
4
+ const stylesheetLoadTimeout = 10000;
12
5
 
13
6
  /**
14
7
  * @typedef {Object} BusinessCountry
@@ -41,12 +34,11 @@ var stylesheetLoadTimeout = 10000;
41
34
  /**
42
35
  * Public business context used by the SDK for tracking, forms, and webchat defaults.
43
36
  */
44
- var Business = /*#__PURE__*/function () {
37
+ class Business {
45
38
  /**
46
39
  * @param {String} id - Public business id.
47
40
  */
48
- function Business(id) {
49
- _classCallCheck(this, Business);
41
+ constructor(id) {
50
42
  this.id = id;
51
43
  this.data = null;
52
44
  this.stylesheet = null;
@@ -61,163 +53,126 @@ var Business = /*#__PURE__*/function () {
61
53
  *
62
54
  * @returns {Promise<BusinessData|null>}
63
55
  */
64
- _createClass(Business, [{
65
- key: "hydrate",
66
- value: function () {
67
- var _hydrate = _asyncToGenerator(function* () {
68
- try {
69
- var response = yield BusinessesAPI.get(this.id);
70
- if (response.ok === false) {
71
- return null;
72
- }
73
- var business = yield response.json();
74
- if (!business) {
75
- return null;
76
- }
77
- this.setData(business);
78
- if (business.locale) {
79
- this.setLocale(business.locale);
80
- }
81
- return business;
82
- } catch (_error) {
83
- return null;
84
- }
85
- });
86
- function hydrate() {
87
- return _hydrate.apply(this, arguments);
56
+ async hydrate() {
57
+ try {
58
+ const response = await BusinessesAPI.get(this.id);
59
+ if (response.ok === false) {
60
+ return null;
88
61
  }
89
- return hydrate;
90
- }()
91
- /**
92
- * @param {BusinessData} data
93
- * @returns {void}
94
- */
95
- }, {
96
- key: "setData",
97
- value: function setData(data) {
98
- this.data = data;
99
- if (typeof document !== 'undefined' && data.style_url) {
100
- this.stylesheet = this.constructor.ensureStylesheet(data.style_url);
101
- this.stylesheetLoaded = this.constructor.waitForStylesheet(this.stylesheet);
102
- } else {
103
- this.stylesheet = null;
104
- this.stylesheetLoaded = Promise.resolve(false);
62
+ const business = await response.json();
63
+ if (!business) {
64
+ return null;
105
65
  }
106
- }
107
- }, {
108
- key: "subscription",
109
- get: function get() {
110
- return this.data.subscription;
111
- }
112
- }, {
113
- key: "country",
114
- get: function get() {
115
- return this.data.country;
116
- }
117
- }, {
118
- key: "enabledWhitelist",
119
- get: function get() {
120
- return this.data.whitelist !== 'disabled';
121
- }
122
-
123
- /**
124
- * @param {String} locale
125
- * @returns {void}
126
- */
127
- }, {
128
- key: "setLocale",
129
- value: function setLocale(locale) {
130
- if (!locales[locale]) {
131
- return console.warn("Locale ".concat(locale, " not found"));
66
+ this.setData(business);
67
+ if (business.locale) {
68
+ this.setLocale(business.locale);
132
69
  }
133
- if (!this.data) {
134
- this.data = {};
135
- }
136
- this.data.locale = locale;
137
- }
138
- }, {
139
- key: "locale",
140
- get: function get() {
141
- return locales[this.data.locale];
142
- }
143
- }, {
144
- key: "features",
145
- get: function get() {
146
- return this.data.features;
147
- }
148
- }], [{
149
- key: "stylesheetSelector",
150
- get: function get() {
151
- return "link[rel=\"stylesheet\"][".concat(stylesheetAttribute, "]");
70
+ return business;
71
+ } catch (_error) {
72
+ return null;
152
73
  }
153
- }, {
154
- key: "ensureStylesheet",
155
- value: function ensureStylesheet(styleUrl) {
156
- var href = this.normalizedStylesheetUrl(styleUrl);
157
- var existingLink = this.stylesheetLinks.find(link => link.href === href);
158
- if (existingLink) {
159
- existingLink.setAttribute(stylesheetAttribute, 'true');
160
- return existingLink;
161
- }
162
- var linkTag = document.createElement('link');
163
- linkTag.rel = 'stylesheet';
164
- linkTag.href = styleUrl;
165
- linkTag.setAttribute(stylesheetAttribute, 'true');
166
- this.waitForStylesheet(linkTag);
167
- document.head.append(linkTag);
168
- return linkTag;
169
- }
170
- }, {
171
- key: "stylesheetLinks",
172
- get: function get() {
173
- if (typeof document === 'undefined') return [];
174
- return Array.from(document.querySelectorAll(this.stylesheetSelector));
74
+ }
75
+
76
+ /**
77
+ * @param {BusinessData} data
78
+ * @returns {void}
79
+ */
80
+ setData(data) {
81
+ this.data = data;
82
+ if (typeof document !== 'undefined' && data.style_url) {
83
+ this.stylesheet = this.constructor.ensureStylesheet(data.style_url);
84
+ this.stylesheetLoaded = this.constructor.waitForStylesheet(this.stylesheet);
85
+ } else {
86
+ this.stylesheet = null;
87
+ this.stylesheetLoaded = Promise.resolve(false);
175
88
  }
176
- }, {
177
- key: "latestStylesheet",
178
- get: function get() {
179
- return this.stylesheetLinks[this.stylesheetLinks.length - 1];
89
+ }
90
+ static get stylesheetSelector() {
91
+ return `link[rel="stylesheet"][${stylesheetAttribute}]`;
92
+ }
93
+ static ensureStylesheet(styleUrl) {
94
+ const href = this.normalizedStylesheetUrl(styleUrl);
95
+ const existingLink = this.stylesheetLinks.find(link => link.href === href);
96
+ if (existingLink) {
97
+ existingLink.setAttribute(stylesheetAttribute, 'true');
98
+ return existingLink;
180
99
  }
181
- }, {
182
- key: "normalizedStylesheetUrl",
183
- value: function normalizedStylesheetUrl(styleUrl) {
184
- try {
185
- return new URL(styleUrl, document.baseURI).href;
186
- } catch (_error) {
187
- return styleUrl;
188
- }
100
+ const linkTag = document.createElement('link');
101
+ linkTag.rel = 'stylesheet';
102
+ linkTag.href = styleUrl;
103
+ linkTag.setAttribute(stylesheetAttribute, 'true');
104
+ this.waitForStylesheet(linkTag);
105
+ document.head.append(linkTag);
106
+ return linkTag;
107
+ }
108
+ static get stylesheetLinks() {
109
+ if (typeof document === 'undefined') return [];
110
+ return Array.from(document.querySelectorAll(this.stylesheetSelector));
111
+ }
112
+ static get latestStylesheet() {
113
+ return this.stylesheetLinks[this.stylesheetLinks.length - 1];
114
+ }
115
+ static normalizedStylesheetUrl(styleUrl) {
116
+ try {
117
+ return new URL(styleUrl, document.baseURI).href;
118
+ } catch (_error) {
119
+ return styleUrl;
189
120
  }
190
- }, {
191
- key: "waitForStylesheet",
192
- value: function waitForStylesheet(linkTag) {
193
- if (!linkTag) return Promise.resolve(false);
194
- if (this.stylesheetIsLoaded(linkTag)) return Promise.resolve(true);
195
- if (linkTag.dataset.hellotextStylesheetLoaded === 'false') return Promise.resolve(false);
196
- if (linkTag._hellotextStylesheetLoaded) return linkTag._hellotextStylesheetLoaded;
197
- linkTag._hellotextStylesheetLoaded = new Promise(resolve => {
198
- var timeout;
199
- var finish = loaded => {
200
- clearTimeout(timeout);
201
- linkTag.removeEventListener('load', handleLoad);
202
- linkTag.removeEventListener('error', handleError);
203
- linkTag.dataset.hellotextStylesheetLoaded = loaded ? 'true' : 'false';
204
- resolve(loaded);
205
- };
206
- var handleLoad = () => finish(this.stylesheetIsLoaded(linkTag));
207
- var handleError = () => finish(false);
208
- linkTag.addEventListener('load', handleLoad);
209
- linkTag.addEventListener('error', handleError);
210
- timeout = setTimeout(() => finish(this.stylesheetIsLoaded(linkTag)), stylesheetLoadTimeout);
211
- if (timeout.unref) timeout.unref();
212
- });
213
- return linkTag._hellotextStylesheetLoaded;
121
+ }
122
+ static waitForStylesheet(linkTag) {
123
+ if (!linkTag) return Promise.resolve(false);
124
+ if (this.stylesheetIsLoaded(linkTag)) return Promise.resolve(true);
125
+ if (linkTag.dataset.hellotextStylesheetLoaded === 'false') return Promise.resolve(false);
126
+ if (linkTag._hellotextStylesheetLoaded) return linkTag._hellotextStylesheetLoaded;
127
+ linkTag._hellotextStylesheetLoaded = new Promise(resolve => {
128
+ let timeout;
129
+ const finish = loaded => {
130
+ clearTimeout(timeout);
131
+ linkTag.removeEventListener('load', handleLoad);
132
+ linkTag.removeEventListener('error', handleError);
133
+ linkTag.dataset.hellotextStylesheetLoaded = loaded ? 'true' : 'false';
134
+ resolve(loaded);
135
+ };
136
+ const handleLoad = () => finish(this.stylesheetIsLoaded(linkTag));
137
+ const handleError = () => finish(false);
138
+ linkTag.addEventListener('load', handleLoad);
139
+ linkTag.addEventListener('error', handleError);
140
+ timeout = setTimeout(() => finish(this.stylesheetIsLoaded(linkTag)), stylesheetLoadTimeout);
141
+ if (timeout.unref) timeout.unref();
142
+ });
143
+ return linkTag._hellotextStylesheetLoaded;
144
+ }
145
+ static stylesheetIsLoaded(linkTag) {
146
+ return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
147
+ }
148
+ get subscription() {
149
+ return this.data.subscription;
150
+ }
151
+ get country() {
152
+ return this.data.country;
153
+ }
154
+ get enabledWhitelist() {
155
+ return this.data.whitelist !== 'disabled';
156
+ }
157
+
158
+ /**
159
+ * @param {String} locale
160
+ * @returns {void}
161
+ */
162
+ setLocale(locale) {
163
+ if (!locales[locale]) {
164
+ return console.warn(`Locale ${locale} not found`);
214
165
  }
215
- }, {
216
- key: "stylesheetIsLoaded",
217
- value: function stylesheetIsLoaded(linkTag) {
218
- return linkTag.dataset.hellotextStylesheetLoaded === 'true' || !!linkTag.sheet;
166
+ if (!this.data) {
167
+ this.data = {};
219
168
  }
220
- }]);
221
- return Business;
222
- }();
169
+ this.data.locale = locale;
170
+ }
171
+ get locale() {
172
+ return locales[this.data.locale];
173
+ }
174
+ get features() {
175
+ return this.data.features;
176
+ }
177
+ }
223
178
  export { Business };