@usertour/helpers 0.0.11 → 0.0.13

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.
@@ -0,0 +1,615 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/conditions.ts
31
+ var conditions_exports = {};
32
+ __export(conditions_exports, {
33
+ PRIORITIES: () => PRIORITIES,
34
+ activedContentCondition: () => activedContentCondition,
35
+ activedContentRulesConditions: () => activedContentRulesConditions,
36
+ activedRulesConditions: () => activedRulesConditions,
37
+ checklistIsDimissed: () => checklistIsDimissed,
38
+ checklistIsSeen: () => checklistIsSeen,
39
+ completeEventMapping: () => completeEventMapping,
40
+ filterAutoStartContent: () => filterAutoStartContent,
41
+ findLatestEvent: () => findLatestEvent,
42
+ flowIsDismissed: () => flowIsDismissed,
43
+ flowIsSeen: () => flowIsSeen,
44
+ isActive: () => isActive,
45
+ isActiveContent: () => isActiveContent,
46
+ isActiveRulesByCurrentPage: () => isActiveRulesByCurrentPage,
47
+ isActiveRulesByCurrentTime: () => isActiveRulesByCurrentTime,
48
+ isDismissedEventMapping: () => isDismissedEventMapping,
49
+ isHasActivedContents: () => isHasActivedContents,
50
+ isSameContents: () => isSameContents,
51
+ isValidContent: () => isValidContent,
52
+ parseUrlParams: () => parseUrlParams,
53
+ rulesTypes: () => rulesTypes,
54
+ wait: () => wait
55
+ });
56
+ module.exports = __toCommonJS(conditions_exports);
57
+
58
+ // src/condition.ts
59
+ var import_fast_deep_equal = __toESM(require("fast-deep-equal"), 1);
60
+ var parseUrl = (url) => {
61
+ const urlPatterns = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
62
+ if (!urlPatterns) {
63
+ return null;
64
+ }
65
+ const [, , scheme = "", domain = "", path = "", , query = "", fragment = ""] = urlPatterns;
66
+ return { scheme, domain, path, query, fragment };
67
+ };
68
+ var replaceWildcard = (input, s1, s2) => {
69
+ const withSpecialWords = replaceSpecialWords(input);
70
+ const withWildcard = withSpecialWords.replace(/\\\*/g, `${s1}*`);
71
+ if (!s2) {
72
+ return withWildcard;
73
+ }
74
+ return withWildcard.replace(/:[a-z0-9_]+/g, `[^${s2}]+`);
75
+ };
76
+ var replaceSpecialWords = (str) => {
77
+ return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
78
+ };
79
+ var parsePattern = (pattern) => {
80
+ if (!pattern || !pattern.trim()) {
81
+ return null;
82
+ }
83
+ const _pattern = parseUrl(pattern);
84
+ if (!_pattern) {
85
+ console.error("Invalid URL pattern:", pattern);
86
+ return null;
87
+ }
88
+ const { scheme, domain, path, query, fragment } = _pattern;
89
+ const _scheme = scheme ? replaceSpecialWords(scheme) : "[a-z\\d]+";
90
+ const _domain = domain ? replaceWildcard(domain, "[^/]", ".") : "[^/]*";
91
+ const _fragment = fragment ? replaceWildcard(fragment, ".", "/") : "(#.*)?";
92
+ const _path = path ? replaceWildcard(path, "[^?#]", "/") : "/[^?#]*";
93
+ let _query = "(\\?[^#]*)?";
94
+ if (query) {
95
+ new URLSearchParams(query).forEach((value, key) => {
96
+ const _str = value === "" ? "=?" : value === "*" ? "(=[^&#]*)?" : `=${replaceWildcard(value, "[^#]")}`;
97
+ _query += `(?=.*[?&]${replaceSpecialWords(key)}${_str}([&#]|$))`;
98
+ });
99
+ _query += "\\?[^#]*";
100
+ }
101
+ return new RegExp(`^${_scheme}://${_domain}(:\\d+)?${_path}${_query}${_fragment}$`);
102
+ };
103
+ var isMatchUrlPattern = (_url, includes, excludes) => {
104
+ const isMatchIncludesConditions = includes.length > 0 ? includes.some((_include) => {
105
+ const reg = parsePattern(_include);
106
+ if (reg) {
107
+ return reg.test(_url);
108
+ }
109
+ return false;
110
+ }) : true;
111
+ const isMatchExcludesConditions = excludes.length > 0 ? excludes.some((_exclude) => {
112
+ const reg = parsePattern(_exclude);
113
+ if (reg) {
114
+ return reg.test(_url);
115
+ }
116
+ return false;
117
+ }) : false;
118
+ return isMatchIncludesConditions && !isMatchExcludesConditions;
119
+ };
120
+ var compareConditionsItem = (item1, item2) => {
121
+ const { data = {}, ...others1 } = item1;
122
+ const { data: data2 = {}, ...others2 } = item2;
123
+ if (!(0, import_fast_deep_equal.default)(others2, others1)) {
124
+ return false;
125
+ }
126
+ for (const key in data) {
127
+ if (!(0, import_fast_deep_equal.default)(data[key], data2[key])) {
128
+ return false;
129
+ }
130
+ }
131
+ return true;
132
+ };
133
+ var conditionsIsSame = (rr1, rr2) => {
134
+ const r1 = [...rr1];
135
+ const r2 = [...rr2];
136
+ if (r1.length === 0 && r2.length === 0) {
137
+ return true;
138
+ }
139
+ if (r1.length !== r2.length) {
140
+ return false;
141
+ }
142
+ const group1 = r1.filter((item) => item.type === "group");
143
+ const group2 = r2.filter((item) => item.type === "group");
144
+ if (group1.length !== group2.length) {
145
+ return false;
146
+ }
147
+ for (let index = 0; index < r1.length; index++) {
148
+ const item1 = r1[index];
149
+ const item2 = r2[index];
150
+ if (!item1 || !item2) {
151
+ return false;
152
+ }
153
+ if (item1.type === "group") {
154
+ if (!item2.conditions) {
155
+ return false;
156
+ }
157
+ const c1 = item1.conditions;
158
+ const c2 = item2.conditions;
159
+ if (item1.operators !== item2.operators) {
160
+ return false;
161
+ }
162
+ if (!conditionsIsSame(c1, c2)) {
163
+ return false;
164
+ }
165
+ } else {
166
+ if (!compareConditionsItem(item1, item2)) {
167
+ return false;
168
+ }
169
+ }
170
+ }
171
+ return true;
172
+ };
173
+
174
+ // src/conditions.ts
175
+ var import_types = require("@usertour/types");
176
+ var import_date_fns = require("date-fns");
177
+ var PRIORITIES = [
178
+ import_types.ContentPriority.HIGHEST,
179
+ import_types.ContentPriority.HIGH,
180
+ import_types.ContentPriority.MEDIUM,
181
+ import_types.ContentPriority.LOW,
182
+ import_types.ContentPriority.LOWEST
183
+ ];
184
+ var rulesTypes = Object.values(import_types.RulesType);
185
+ var isActiveRulesByCurrentPage = (rules) => {
186
+ const { excludes, includes } = rules.data;
187
+ if (location) {
188
+ const href = location.href;
189
+ return isMatchUrlPattern(href, includes, excludes);
190
+ }
191
+ return false;
192
+ };
193
+ var isActiveRulesByCurrentTime = (rules) => {
194
+ const { endDate, endDateHour, endDateMinute, startDate, startDateHour, startDateMinute } = rules.data;
195
+ const startTime = /* @__PURE__ */ new Date(`${startDate} ${startDateHour}:${startDateMinute}:00`);
196
+ const endTime = /* @__PURE__ */ new Date(`${endDate} ${endDateHour}:${endDateMinute}:00`);
197
+ const now = /* @__PURE__ */ new Date();
198
+ if (!endDate) {
199
+ return (0, import_date_fns.isAfter)(now, startTime);
200
+ }
201
+ return (0, import_date_fns.isAfter)(now, startTime) && (0, import_date_fns.isBefore)(now, endTime);
202
+ };
203
+ var isActivedContentRulesCondition = (rules, contentSession) => {
204
+ const { contentId, logic } = rules.data;
205
+ const { latestSession, seenSessions, completedSessions } = contentSession;
206
+ if (!contentId || !logic || contentId !== contentSession.contentId) {
207
+ return false;
208
+ }
209
+ if (logic === import_types.ContentConditionLogic.ACTIVED || logic === import_types.ContentConditionLogic.UNACTIVED) {
210
+ if (!latestSession) {
211
+ return logic === import_types.ContentConditionLogic.UNACTIVED;
212
+ }
213
+ const isActived = !(flowIsDismissed(latestSession) || checklistIsDimissed(latestSession));
214
+ return logic === import_types.ContentConditionLogic.ACTIVED ? isActived : !isActived;
215
+ }
216
+ const isSeen = seenSessions > 0;
217
+ const isCompleted = completedSessions > 0;
218
+ if (logic === import_types.ContentConditionLogic.SEEN || logic === import_types.ContentConditionLogic.UNSEEN) {
219
+ return logic === import_types.ContentConditionLogic.SEEN ? isSeen : !isSeen;
220
+ }
221
+ if (logic === import_types.ContentConditionLogic.COMPLETED || logic === import_types.ContentConditionLogic.UNCOMPLETED) {
222
+ return logic === import_types.ContentConditionLogic.COMPLETED ? isCompleted : !isCompleted;
223
+ }
224
+ return false;
225
+ };
226
+ var isValidRulesType = (type) => {
227
+ return rulesTypes.includes(type);
228
+ };
229
+ var isActiveRules = async (rules) => {
230
+ if (!isValidRulesType(rules.type)) {
231
+ return true;
232
+ }
233
+ switch (rules.type) {
234
+ case import_types.RulesType.CURRENT_PAGE:
235
+ return isActiveRulesByCurrentPage(rules);
236
+ case import_types.RulesType.TIME:
237
+ return isActiveRulesByCurrentTime(rules);
238
+ default:
239
+ return rules.actived;
240
+ }
241
+ };
242
+ var activedRulesConditions = async (conditions, rewrite) => {
243
+ const rulesCondition = [...conditions];
244
+ for (let j = 0; j < rulesCondition.length; j++) {
245
+ const rules = rulesCondition[j];
246
+ if (rules.type !== "group") {
247
+ if (rewrite == null ? void 0 : rewrite[rules.type]) {
248
+ rulesCondition[j].actived = true;
249
+ } else {
250
+ rulesCondition[j].actived = await isActiveRules(rules);
251
+ }
252
+ } else if (rules.conditions) {
253
+ rulesCondition[j].conditions = await activedRulesConditions(rules.conditions);
254
+ }
255
+ }
256
+ return rulesCondition;
257
+ };
258
+ var activedContentRulesConditions = async (conditions, contents) => {
259
+ const rulesCondition = [...conditions];
260
+ for (let j = 0; j < rulesCondition.length; j++) {
261
+ const rules = rulesCondition[j];
262
+ if (rules.type !== "group") {
263
+ if (rules.type === import_types.RulesType.CONTENT) {
264
+ const content = contents.find((c) => c.contentId === rules.data.contentId);
265
+ if (content) {
266
+ const contentSession = {
267
+ contentId: content.contentId,
268
+ latestSession: content.latestSession,
269
+ totalSessions: content.totalSessions,
270
+ dismissedSessions: content.dismissedSessions,
271
+ completedSessions: content.completedSessions,
272
+ seenSessions: content.seenSessions
273
+ };
274
+ rulesCondition[j].actived = isActivedContentRulesCondition(rules, contentSession);
275
+ }
276
+ }
277
+ } else if (rules.conditions) {
278
+ rulesCondition[j].conditions = await activedContentRulesConditions(
279
+ rules.conditions,
280
+ contents
281
+ );
282
+ }
283
+ }
284
+ return rulesCondition;
285
+ };
286
+ var activedContentCondition = async (contents) => {
287
+ const _contents = JSON.parse(JSON.stringify(contents));
288
+ for (let index = 0; index < _contents.length; index++) {
289
+ const content = _contents[index];
290
+ const { enabledAutoStartRules, autoStartRules, hideRules, enabledHideRules } = content.config;
291
+ if (enabledAutoStartRules && autoStartRules && autoStartRules.length > 0) {
292
+ content.config.autoStartRules = await activedRulesConditions(autoStartRules);
293
+ }
294
+ if (enabledHideRules && hideRules && hideRules.length > 0) {
295
+ content.config.hideRules = await activedRulesConditions(hideRules);
296
+ }
297
+ }
298
+ return _contents;
299
+ };
300
+ var isActive = (autoStartRules) => {
301
+ if (!autoStartRules || autoStartRules.length === 0) {
302
+ return false;
303
+ }
304
+ const operator = autoStartRules[0].operators;
305
+ const actives = autoStartRules.filter((rule) => {
306
+ if (!rule.conditions) {
307
+ return rule.actived;
308
+ }
309
+ return isActive(rule.conditions);
310
+ });
311
+ return operator === "and" ? actives.length === autoStartRules.length : actives.length > 0;
312
+ };
313
+ var isActiveContent = (content) => {
314
+ const { enabledAutoStartRules, autoStartRules } = content.config;
315
+ if (!enabledAutoStartRules || !isActive(autoStartRules)) {
316
+ return false;
317
+ }
318
+ return true;
319
+ };
320
+ var priorityCompare = (a, b) => {
321
+ var _a, _b, _c, _d;
322
+ const a1 = (_b = (_a = a == null ? void 0 : a.config) == null ? void 0 : _a.autoStartRulesSetting) == null ? void 0 : _b.priority;
323
+ const a2 = (_d = (_c = b == null ? void 0 : b.config) == null ? void 0 : _c.autoStartRulesSetting) == null ? void 0 : _d.priority;
324
+ if (!a1 || !a2) {
325
+ return 0;
326
+ }
327
+ const index1 = PRIORITIES.indexOf(a1);
328
+ const index2 = PRIORITIES.indexOf(a2);
329
+ if (index1 > index2) {
330
+ return 1;
331
+ }
332
+ if (index1 < index2) {
333
+ return -1;
334
+ }
335
+ return 0;
336
+ };
337
+ var filterAutoStartContent = (contents, type) => {
338
+ return contents.filter((content) => {
339
+ const isActive2 = isActiveContent(content);
340
+ const isValid = isValidContent(content, contents);
341
+ return content.type === type && isActive2 && isValid;
342
+ }).sort(priorityCompare);
343
+ };
344
+ var isHasActivedContents = (source, dest) => {
345
+ for (let index = 0; index < source.length; index++) {
346
+ const content1 = source[index];
347
+ const content2 = dest.find((c) => c.id === content1.id);
348
+ if (!content2) {
349
+ return true;
350
+ }
351
+ if (isActiveContent(content1) !== isActiveContent(content2)) {
352
+ return true;
353
+ }
354
+ }
355
+ return false;
356
+ };
357
+ var isSameContents = (source, dest) => {
358
+ if (!source || !dest || source.length !== dest.length) {
359
+ return false;
360
+ }
361
+ for (let index = 0; index < source.length; index++) {
362
+ const content1 = source[index];
363
+ const content2 = dest.find((c) => c.id === content1.id);
364
+ if (!content2) {
365
+ return false;
366
+ }
367
+ if (!conditionsIsSame(content1.config.autoStartRules, content2.config.autoStartRules)) {
368
+ return false;
369
+ }
370
+ }
371
+ return true;
372
+ };
373
+ var getLatestEvent = (currentContent, contents, eventCodeName) => {
374
+ var _a;
375
+ const bizEvents = [];
376
+ const contentId = currentContent.id;
377
+ const contentType = currentContent.type;
378
+ for (let index = 0; index < contents.length; index++) {
379
+ const content = contents[index];
380
+ if (content.id === contentId || content.type !== contentType) {
381
+ continue;
382
+ }
383
+ const sessionBizEvents = (_a = content.latestSession) == null ? void 0 : _a.bizEvent;
384
+ if (sessionBizEvents && sessionBizEvents.length > 0) {
385
+ bizEvents.push(...sessionBizEvents.filter((e) => {
386
+ var _a2;
387
+ return ((_a2 = e == null ? void 0 : e.event) == null ? void 0 : _a2.codeName) === eventCodeName;
388
+ }));
389
+ }
390
+ }
391
+ return findLatestEvent(bizEvents);
392
+ };
393
+ var findLatestEvent = (bizEvents) => {
394
+ const initialValue = bizEvents[0];
395
+ const lastEvent = bizEvents.reduce(
396
+ (accumulator, currentValue) => {
397
+ if ((0, import_date_fns.isAfter)(new Date(currentValue.createdAt), new Date(accumulator.createdAt))) {
398
+ return currentValue;
399
+ }
400
+ return accumulator;
401
+ },
402
+ initialValue
403
+ );
404
+ return lastEvent;
405
+ };
406
+ var completeEventMapping = {
407
+ [import_types.ContentDataType.FLOW]: import_types.BizEvents.FLOW_COMPLETED,
408
+ [import_types.ContentDataType.LAUNCHER]: import_types.BizEvents.LAUNCHER_ACTIVATED,
409
+ [import_types.ContentDataType.CHECKLIST]: import_types.BizEvents.CHECKLIST_COMPLETED
410
+ };
411
+ var showEventMapping = {
412
+ [import_types.ContentDataType.FLOW]: import_types.BizEvents.FLOW_STEP_SEEN,
413
+ [import_types.ContentDataType.LAUNCHER]: import_types.BizEvents.LAUNCHER_SEEN,
414
+ [import_types.ContentDataType.CHECKLIST]: import_types.BizEvents.CHECKLIST_SEEN
415
+ };
416
+ var isDismissedEventMapping = {
417
+ [import_types.ContentDataType.FLOW]: import_types.BizEvents.FLOW_ENDED,
418
+ [import_types.ContentDataType.LAUNCHER]: import_types.BizEvents.LAUNCHER_DISMISSED,
419
+ [import_types.ContentDataType.CHECKLIST]: import_types.BizEvents.CHECKLIST_DISMISSED
420
+ };
421
+ var isGreaterThenDuration = (dateLeft, dateRight, unit, duration) => {
422
+ switch (unit) {
423
+ case import_types.FrequencyUnits.SECONDS: {
424
+ if ((0, import_date_fns.differenceInSeconds)(dateLeft, dateRight) >= duration) {
425
+ return true;
426
+ }
427
+ return false;
428
+ }
429
+ case import_types.FrequencyUnits.MINUTES:
430
+ if ((0, import_date_fns.differenceInMinutes)(dateLeft, dateRight) >= duration) {
431
+ return true;
432
+ }
433
+ return false;
434
+ case import_types.FrequencyUnits.HOURS:
435
+ if ((0, import_date_fns.differenceInHours)(dateLeft, dateRight) >= duration) {
436
+ return true;
437
+ }
438
+ return false;
439
+ case import_types.FrequencyUnits.DAYES:
440
+ if ((0, import_date_fns.differenceInDays)(dateLeft, dateRight) >= duration) {
441
+ return true;
442
+ }
443
+ return false;
444
+ default:
445
+ return false;
446
+ }
447
+ };
448
+ var checklistIsDimissed = (latestSession) => {
449
+ var _a;
450
+ return (_a = latestSession == null ? void 0 : latestSession.bizEvent) == null ? void 0 : _a.find(
451
+ (event) => {
452
+ var _a2;
453
+ return ((_a2 = event == null ? void 0 : event.event) == null ? void 0 : _a2.codeName) === import_types.BizEvents.CHECKLIST_DISMISSED;
454
+ }
455
+ );
456
+ };
457
+ var flowIsDismissed = (latestSession) => {
458
+ var _a;
459
+ return (_a = latestSession == null ? void 0 : latestSession.bizEvent) == null ? void 0 : _a.find((event) => {
460
+ var _a2;
461
+ return ((_a2 = event == null ? void 0 : event.event) == null ? void 0 : _a2.codeName) === import_types.BizEvents.FLOW_ENDED;
462
+ });
463
+ };
464
+ var flowIsSeen = (latestSession) => {
465
+ var _a;
466
+ return (_a = latestSession == null ? void 0 : latestSession.bizEvent) == null ? void 0 : _a.find(
467
+ (event) => {
468
+ var _a2;
469
+ return ((_a2 = event == null ? void 0 : event.event) == null ? void 0 : _a2.codeName) === import_types.BizEvents.FLOW_STEP_SEEN;
470
+ }
471
+ );
472
+ };
473
+ var checklistIsSeen = (latestSession) => {
474
+ var _a;
475
+ return (_a = latestSession == null ? void 0 : latestSession.bizEvent) == null ? void 0 : _a.find(
476
+ (event) => {
477
+ var _a2;
478
+ return ((_a2 = event == null ? void 0 : event.event) == null ? void 0 : _a2.codeName) === import_types.BizEvents.CHECKLIST_SEEN;
479
+ }
480
+ );
481
+ };
482
+ var isValidContent = (content, contents) => {
483
+ var _a;
484
+ const now = /* @__PURE__ */ new Date();
485
+ if (content.type === import_types.ContentDataType.FLOW) {
486
+ if (!content.steps || content.steps.length === 0) {
487
+ return false;
488
+ }
489
+ } else {
490
+ if (!content.data) {
491
+ return false;
492
+ }
493
+ }
494
+ if (!content.config.autoStartRulesSetting) {
495
+ return true;
496
+ }
497
+ const { frequency, startIfNotComplete } = content.config.autoStartRulesSetting;
498
+ const completedSessions = content.completedSessions;
499
+ const dismissedSessions = content.dismissedSessions;
500
+ if (startIfNotComplete && completedSessions > 0) {
501
+ return false;
502
+ }
503
+ if (!frequency) {
504
+ return true;
505
+ }
506
+ const contentType = content.type;
507
+ const lastEventName = showEventMapping[contentType];
508
+ const lastEvent = getLatestEvent(content, contents, lastEventName);
509
+ const contentEvents = (_a = content.latestSession) == null ? void 0 : _a.bizEvent;
510
+ if (lastEvent && frequency && frequency.atLeast && !isGreaterThenDuration(
511
+ now,
512
+ new Date(lastEvent.createdAt),
513
+ frequency.atLeast.unit,
514
+ frequency.atLeast.duration
515
+ )) {
516
+ return false;
517
+ }
518
+ if (frequency.frequency === import_types.Frequency.ONCE) {
519
+ if (dismissedSessions > 0) {
520
+ return false;
521
+ }
522
+ return true;
523
+ }
524
+ const showEventName = showEventMapping[contentType];
525
+ const showEvents = contentEvents == null ? void 0 : contentEvents.filter(
526
+ (e) => {
527
+ var _a2, _b;
528
+ return ((_a2 = e == null ? void 0 : e.event) == null ? void 0 : _a2.codeName) === showEventName && (contentType === import_types.ContentDataType.FLOW ? ((_b = e == null ? void 0 : e.data) == null ? void 0 : _b.flow_step_number) === 0 : true);
529
+ }
530
+ );
531
+ if (!showEvents || showEvents.length === 0) {
532
+ return true;
533
+ }
534
+ const lastShowEvent = findLatestEvent(showEvents);
535
+ const lastShowEventDate = new Date(lastShowEvent.createdAt);
536
+ if (frequency.frequency === import_types.Frequency.MULTIPLE) {
537
+ if (frequency.every.times && dismissedSessions >= frequency.every.times) {
538
+ return false;
539
+ }
540
+ }
541
+ if (frequency.frequency === import_types.Frequency.MULTIPLE || frequency.frequency === import_types.Frequency.UNLIMITED) {
542
+ if (!isGreaterThenDuration(now, lastShowEventDate, frequency.every.unit, frequency.every.duration)) {
543
+ return false;
544
+ }
545
+ }
546
+ return true;
547
+ };
548
+ var parseUrlParams = (url, paramName) => {
549
+ if (!url || !paramName) {
550
+ return null;
551
+ }
552
+ try {
553
+ const urlObj = new URL(url);
554
+ const searchParams = new URLSearchParams(urlObj.search);
555
+ if (searchParams.has(paramName)) {
556
+ return searchParams.get(paramName);
557
+ }
558
+ if (urlObj.hash) {
559
+ const hashSearch = urlObj.hash.split("?")[1];
560
+ if (hashSearch) {
561
+ const hashParams = new URLSearchParams(hashSearch);
562
+ if (hashParams.has(paramName)) {
563
+ return hashParams.get(paramName);
564
+ }
565
+ }
566
+ }
567
+ return null;
568
+ } catch (error) {
569
+ console.error("Error parsing URL:", error);
570
+ return null;
571
+ }
572
+ };
573
+ var wait = (seconds) => {
574
+ if (typeof seconds !== "number" || Number.isNaN(seconds)) {
575
+ return Promise.reject(new Error("Invalid wait time: must be a number"));
576
+ }
577
+ if (seconds < 0) {
578
+ return Promise.reject(new Error("Invalid wait time: cannot be negative"));
579
+ }
580
+ if (seconds === 0) {
581
+ return Promise.resolve();
582
+ }
583
+ return new Promise((resolve, reject) => {
584
+ try {
585
+ setTimeout(resolve, seconds * 1e3);
586
+ } catch (error) {
587
+ reject(error);
588
+ }
589
+ });
590
+ };
591
+ // Annotate the CommonJS export names for ESM import in node:
592
+ 0 && (module.exports = {
593
+ PRIORITIES,
594
+ activedContentCondition,
595
+ activedContentRulesConditions,
596
+ activedRulesConditions,
597
+ checklistIsDimissed,
598
+ checklistIsSeen,
599
+ completeEventMapping,
600
+ filterAutoStartContent,
601
+ findLatestEvent,
602
+ flowIsDismissed,
603
+ flowIsSeen,
604
+ isActive,
605
+ isActiveContent,
606
+ isActiveRulesByCurrentPage,
607
+ isActiveRulesByCurrentTime,
608
+ isDismissedEventMapping,
609
+ isHasActivedContents,
610
+ isSameContents,
611
+ isValidContent,
612
+ parseUrlParams,
613
+ rulesTypes,
614
+ wait
615
+ });
@@ -0,0 +1,35 @@
1
+ import { ContentPriority, RulesType, RulesCondition, SDKContent, BizEvent, BizEvents, BizSession } from '@usertour/types';
2
+
3
+ declare const PRIORITIES: ContentPriority[];
4
+ declare const rulesTypes: RulesType[];
5
+ declare const isActiveRulesByCurrentPage: (rules: RulesCondition) => boolean;
6
+ declare const isActiveRulesByCurrentTime: (rules: RulesCondition) => boolean;
7
+ type RewriteRulesCondition = Partial<Record<RulesType, boolean>>;
8
+ declare const activedRulesConditions: (conditions: RulesCondition[], rewrite?: RewriteRulesCondition) => Promise<RulesCondition[]>;
9
+ declare const activedContentRulesConditions: (conditions: RulesCondition[], contents: SDKContent[]) => Promise<RulesCondition[]>;
10
+ declare const activedContentCondition: (contents: SDKContent[]) => Promise<SDKContent[]>;
11
+ declare const isActive: (autoStartRules: RulesCondition[]) => boolean;
12
+ declare const isActiveContent: (content: SDKContent) => boolean;
13
+ declare const filterAutoStartContent: (contents: SDKContent[], type: string) => SDKContent[];
14
+ declare const isHasActivedContents: (source: SDKContent[], dest: SDKContent[]) => boolean;
15
+ declare const isSameContents: (source: SDKContent[], dest: SDKContent[]) => boolean;
16
+ declare const findLatestEvent: (bizEvents: BizEvent[]) => BizEvent;
17
+ declare const completeEventMapping: {
18
+ flow: BizEvents;
19
+ launcher: BizEvents;
20
+ checklist: BizEvents;
21
+ };
22
+ declare const isDismissedEventMapping: {
23
+ flow: BizEvents;
24
+ launcher: BizEvents;
25
+ checklist: BizEvents;
26
+ };
27
+ declare const checklistIsDimissed: (latestSession?: BizSession) => BizEvent | undefined;
28
+ declare const flowIsDismissed: (latestSession?: BizSession) => BizEvent | undefined;
29
+ declare const flowIsSeen: (latestSession?: BizSession) => BizEvent | undefined;
30
+ declare const checklistIsSeen: (latestSession?: BizSession) => BizEvent | undefined;
31
+ declare const isValidContent: (content: SDKContent, contents: SDKContent[]) => boolean;
32
+ declare const parseUrlParams: (url: string, paramName: string) => string | null;
33
+ declare const wait: (seconds: number) => Promise<void>;
34
+
35
+ export { PRIORITIES, activedContentCondition, activedContentRulesConditions, activedRulesConditions, checklistIsDimissed, checklistIsSeen, completeEventMapping, filterAutoStartContent, findLatestEvent, flowIsDismissed, flowIsSeen, isActive, isActiveContent, isActiveRulesByCurrentPage, isActiveRulesByCurrentTime, isDismissedEventMapping, isHasActivedContents, isSameContents, isValidContent, parseUrlParams, rulesTypes, wait };
@@ -0,0 +1,35 @@
1
+ import { ContentPriority, RulesType, RulesCondition, SDKContent, BizEvent, BizEvents, BizSession } from '@usertour/types';
2
+
3
+ declare const PRIORITIES: ContentPriority[];
4
+ declare const rulesTypes: RulesType[];
5
+ declare const isActiveRulesByCurrentPage: (rules: RulesCondition) => boolean;
6
+ declare const isActiveRulesByCurrentTime: (rules: RulesCondition) => boolean;
7
+ type RewriteRulesCondition = Partial<Record<RulesType, boolean>>;
8
+ declare const activedRulesConditions: (conditions: RulesCondition[], rewrite?: RewriteRulesCondition) => Promise<RulesCondition[]>;
9
+ declare const activedContentRulesConditions: (conditions: RulesCondition[], contents: SDKContent[]) => Promise<RulesCondition[]>;
10
+ declare const activedContentCondition: (contents: SDKContent[]) => Promise<SDKContent[]>;
11
+ declare const isActive: (autoStartRules: RulesCondition[]) => boolean;
12
+ declare const isActiveContent: (content: SDKContent) => boolean;
13
+ declare const filterAutoStartContent: (contents: SDKContent[], type: string) => SDKContent[];
14
+ declare const isHasActivedContents: (source: SDKContent[], dest: SDKContent[]) => boolean;
15
+ declare const isSameContents: (source: SDKContent[], dest: SDKContent[]) => boolean;
16
+ declare const findLatestEvent: (bizEvents: BizEvent[]) => BizEvent;
17
+ declare const completeEventMapping: {
18
+ flow: BizEvents;
19
+ launcher: BizEvents;
20
+ checklist: BizEvents;
21
+ };
22
+ declare const isDismissedEventMapping: {
23
+ flow: BizEvents;
24
+ launcher: BizEvents;
25
+ checklist: BizEvents;
26
+ };
27
+ declare const checklistIsDimissed: (latestSession?: BizSession) => BizEvent | undefined;
28
+ declare const flowIsDismissed: (latestSession?: BizSession) => BizEvent | undefined;
29
+ declare const flowIsSeen: (latestSession?: BizSession) => BizEvent | undefined;
30
+ declare const checklistIsSeen: (latestSession?: BizSession) => BizEvent | undefined;
31
+ declare const isValidContent: (content: SDKContent, contents: SDKContent[]) => boolean;
32
+ declare const parseUrlParams: (url: string, paramName: string) => string | null;
33
+ declare const wait: (seconds: number) => Promise<void>;
34
+
35
+ export { PRIORITIES, activedContentCondition, activedContentRulesConditions, activedRulesConditions, checklistIsDimissed, checklistIsSeen, completeEventMapping, filterAutoStartContent, findLatestEvent, flowIsDismissed, flowIsSeen, isActive, isActiveContent, isActiveRulesByCurrentPage, isActiveRulesByCurrentTime, isDismissedEventMapping, isHasActivedContents, isSameContents, isValidContent, parseUrlParams, rulesTypes, wait };