datakeen-session-react 1.1.140-dev.144 → 1.1.140-dev.147

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 (29) hide show
  1. package/dist/cjs/components/id-check/CameraMask.js +6 -2
  2. package/dist/cjs/components/id-check/CameraMask.js.map +1 -1
  3. package/dist/cjs/components/id-check/Photo.js +42 -14
  4. package/dist/cjs/components/id-check/Photo.js.map +1 -1
  5. package/dist/cjs/components/session/StartSession.js +16 -5
  6. package/dist/cjs/components/session/StartSession.js.map +1 -1
  7. package/dist/cjs/components/start-flow/CGU.js +1 -1
  8. package/dist/cjs/components/start-flow/Start.js +1 -1
  9. package/dist/cjs/services/cameraService.js +34 -90
  10. package/dist/cjs/services/cameraService.js.map +1 -1
  11. package/dist/cjs/services/documentEdgeDetectionService.js +182 -0
  12. package/dist/cjs/services/documentEdgeDetectionService.js.map +1 -0
  13. package/dist/esm/components/id-check/CameraMask.js +6 -2
  14. package/dist/esm/components/id-check/CameraMask.js.map +1 -1
  15. package/dist/esm/components/id-check/Photo.js +42 -14
  16. package/dist/esm/components/id-check/Photo.js.map +1 -1
  17. package/dist/esm/components/session/StartSession.js +16 -5
  18. package/dist/esm/components/session/StartSession.js.map +1 -1
  19. package/dist/esm/components/start-flow/CGU.js +1 -1
  20. package/dist/esm/components/start-flow/Start.js +1 -1
  21. package/dist/esm/services/cameraService.js +34 -90
  22. package/dist/esm/services/cameraService.js.map +1 -1
  23. package/dist/esm/services/documentEdgeDetectionService.js +176 -0
  24. package/dist/esm/services/documentEdgeDetectionService.js.map +1 -0
  25. package/package.json +1 -4
  26. package/dist/cjs/services/documentDetectionService.js +0 -156
  27. package/dist/cjs/services/documentDetectionService.js.map +0 -1
  28. package/dist/esm/services/documentDetectionService.js +0 -153
  29. package/dist/esm/services/documentDetectionService.js.map +0 -1
@@ -1,156 +0,0 @@
1
- 'use strict';
2
-
3
- var tslib_es6 = require('../node_modules/tslib/tslib.es6.js');
4
-
5
- var DocumentDetectionService = /** @class */ (function () {
6
- function DocumentDetectionService() {
7
- this.detectionTimeoutId = null;
8
- this.model = null;
9
- this.isDetecting = false;
10
- this.lastResult = null;
11
- this.DETECTION_INTERVAL_MS = 350;
12
- }
13
- DocumentDetectionService.prototype.loadModel = function () {
14
- return tslib_es6.__awaiter(this, void 0, void 0, function () {
15
- var tf, cocoSsd, _a;
16
- return tslib_es6.__generator(this, function (_b) {
17
- switch (_b.label) {
18
- case 0:
19
- if (!!this.model) return [3 /*break*/, 7];
20
- return [4 /*yield*/, import('@tensorflow/tfjs')];
21
- case 1:
22
- tf = _b.sent();
23
- return [4 /*yield*/, import('@tensorflow/tfjs-backend-webgl')];
24
- case 2:
25
- _b.sent();
26
- return [4 /*yield*/, import('@tensorflow-models/coco-ssd')];
27
- case 3:
28
- cocoSsd = _b.sent();
29
- return [4 /*yield*/, tf.setBackend("webgl")];
30
- case 4:
31
- _b.sent();
32
- return [4 /*yield*/, tf.ready()];
33
- case 5:
34
- _b.sent();
35
- _a = this;
36
- return [4 /*yield*/, cocoSsd.load()];
37
- case 6:
38
- _a.model = _b.sent();
39
- _b.label = 7;
40
- case 7: return [2 /*return*/];
41
- }
42
- });
43
- });
44
- };
45
- DocumentDetectionService.prototype.startDocumentDetection = function (videoElement, onDetectionChange) {
46
- return tslib_es6.__awaiter(this, void 0, void 0, function () {
47
- var detect;
48
- var _this = this;
49
- return tslib_es6.__generator(this, function (_a) {
50
- switch (_a.label) {
51
- case 0:
52
- this.stopDocumentDetection();
53
- return [4 /*yield*/, this.loadModel()];
54
- case 1:
55
- _a.sent();
56
- detect = function () { return tslib_es6.__awaiter(_this, void 0, void 0, function () {
57
- var predictions, documentLike, result, err_1;
58
- return tslib_es6.__generator(this, function (_a) {
59
- switch (_a.label) {
60
- case 0:
61
- if (!videoElement || !this.model || !videoElement.videoWidth) {
62
- this.scheduleNextDetection(detect);
63
- return [2 /*return*/];
64
- }
65
- if (this.isDetecting) {
66
- this.scheduleNextDetection(detect);
67
- return [2 /*return*/];
68
- }
69
- this.isDetecting = true;
70
- _a.label = 1;
71
- case 1:
72
- _a.trys.push([1, 3, 4, 5]);
73
- return [4 /*yield*/, this.model.detect(videoElement)];
74
- case 2:
75
- predictions = _a.sent();
76
- documentLike = predictions.find(function (pred) {
77
- return ["book", "cell phone", "tv", "laptop"].includes(pred.class) &&
78
- pred.score > 0.6;
79
- });
80
- result = documentLike
81
- ? {
82
- hasDocument: true,
83
- score: documentLike.score,
84
- label: documentLike.class,
85
- bbox: documentLike.bbox,
86
- timestamp: Date.now(),
87
- }
88
- : {
89
- hasDocument: false,
90
- timestamp: Date.now(),
91
- };
92
- if (this.shouldEmitResult(result)) {
93
- onDetectionChange(result);
94
- this.lastResult = result;
95
- }
96
- return [3 /*break*/, 5];
97
- case 3:
98
- err_1 = _a.sent();
99
- console.error("[DocumentDetection] Error during detection:", err_1);
100
- return [3 /*break*/, 5];
101
- case 4:
102
- this.isDetecting = false;
103
- this.scheduleNextDetection(detect);
104
- return [7 /*endfinally*/];
105
- case 5: return [2 /*return*/];
106
- }
107
- });
108
- }); };
109
- detect();
110
- return [2 /*return*/, {
111
- stop: function () { return _this.stopDocumentDetection(); },
112
- }];
113
- }
114
- });
115
- });
116
- };
117
- DocumentDetectionService.prototype.stopDocumentDetection = function () {
118
- if (this.detectionTimeoutId) {
119
- clearTimeout(this.detectionTimeoutId);
120
- this.detectionTimeoutId = null;
121
- }
122
- this.isDetecting = false;
123
- this.lastResult = null;
124
- };
125
- DocumentDetectionService.prototype.shouldEmitResult = function (result) {
126
- if (!this.lastResult) {
127
- return true;
128
- }
129
- if (this.lastResult.hasDocument !== result.hasDocument) {
130
- return true;
131
- }
132
- if (!result.hasDocument) {
133
- return false;
134
- }
135
- if (!this.lastResult.bbox || !result.bbox) {
136
- return true;
137
- }
138
- var _a = this.lastResult.bbox, x1 = _a[0], y1 = _a[1], w1 = _a[2], h1 = _a[3];
139
- var _b = result.bbox, x2 = _b[0], y2 = _b[1], w2 = _b[2], h2 = _b[3];
140
- var delta = Math.abs(x1 - x2) + Math.abs(y1 - y2);
141
- var sizeDelta = Math.abs(w1 - w2) + Math.abs(h1 - h2);
142
- return delta > 10 || sizeDelta > 10;
143
- };
144
- DocumentDetectionService.prototype.scheduleNextDetection = function (callback) {
145
- if (this.detectionTimeoutId) {
146
- clearTimeout(this.detectionTimeoutId);
147
- }
148
- this.detectionTimeoutId = window.setTimeout(callback, this.DETECTION_INTERVAL_MS);
149
- };
150
- return DocumentDetectionService;
151
- }());
152
- var documentDetectionService = new DocumentDetectionService();
153
-
154
- exports.DocumentDetectionService = DocumentDetectionService;
155
- exports.documentDetectionService = documentDetectionService;
156
- //# sourceMappingURL=documentDetectionService.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"documentDetectionService.js","sources":["../../../../src/services/documentDetectionService.ts"],"sourcesContent":["import type { ObjectDetection } from \"@tensorflow-models/coco-ssd\";\n\nexport interface DocumentDetectionResult {\n hasDocument: boolean;\n score?: number;\n label?: string;\n bbox?: [number, number, number, number];\n timestamp: number;\n}\n\nexport class DocumentDetectionService {\n private detectionTimeoutId: number | null = null;\n private model: ObjectDetection | null = null;\n private isDetecting = false;\n private lastResult: DocumentDetectionResult | null = null;\n private readonly DETECTION_INTERVAL_MS = 350;\n\n async loadModel() {\n if (!this.model) {\n const tf = await import(\"@tensorflow/tfjs\");\n await import(\"@tensorflow/tfjs-backend-webgl\");\n const cocoSsd = await import(\"@tensorflow-models/coco-ssd\");\n\n await tf.setBackend(\"webgl\");\n await tf.ready();\n\n this.model = await cocoSsd.load();\n }\n }\n\n async startDocumentDetection(\n videoElement: HTMLVideoElement | null,\n onDetectionChange: (result: DocumentDetectionResult) => void\n ): Promise<{ stop: () => void }> {\n this.stopDocumentDetection();\n await this.loadModel();\n\n const detect = async () => {\n if (!videoElement || !this.model || !videoElement.videoWidth) {\n this.scheduleNextDetection(detect);\n return;\n }\n\n if (this.isDetecting) {\n this.scheduleNextDetection(detect);\n return;\n }\n\n this.isDetecting = true;\n\n try {\n const predictions = await this.model.detect(videoElement);\n\n const documentLike = predictions.find(\n (pred) =>\n [\"book\", \"cell phone\", \"tv\", \"laptop\"].includes(pred.class) &&\n pred.score > 0.6\n );\n\n const result: DocumentDetectionResult = documentLike\n ? {\n hasDocument: true,\n score: documentLike.score,\n label: documentLike.class,\n bbox: documentLike.bbox,\n timestamp: Date.now(),\n }\n : {\n hasDocument: false,\n timestamp: Date.now(),\n };\n\n if (this.shouldEmitResult(result)) {\n onDetectionChange(result);\n this.lastResult = result;\n }\n } catch (err) {\n console.error(\"[DocumentDetection] Error during detection:\", err);\n } finally {\n this.isDetecting = false;\n this.scheduleNextDetection(detect);\n }\n };\n\n detect();\n\n return {\n stop: () => this.stopDocumentDetection(),\n };\n }\n\n stopDocumentDetection() {\n if (this.detectionTimeoutId) {\n clearTimeout(this.detectionTimeoutId);\n this.detectionTimeoutId = null;\n }\n this.isDetecting = false;\n this.lastResult = null;\n }\n\n private shouldEmitResult(result: DocumentDetectionResult): boolean {\n if (!this.lastResult) {\n return true;\n }\n\n if (this.lastResult.hasDocument !== result.hasDocument) {\n return true;\n }\n\n if (!result.hasDocument) {\n return false;\n }\n\n if (!this.lastResult.bbox || !result.bbox) {\n return true;\n }\n\n const [x1, y1, w1, h1] = this.lastResult.bbox;\n const [x2, y2, w2, h2] = result.bbox;\n\n const delta = Math.abs(x1 - x2) + Math.abs(y1 - y2);\n const sizeDelta = Math.abs(w1 - w2) + Math.abs(h1 - h2);\n\n return delta > 10 || sizeDelta > 10;\n }\n\n private scheduleNextDetection(callback: () => void) {\n if (this.detectionTimeoutId) {\n clearTimeout(this.detectionTimeoutId);\n }\n this.detectionTimeoutId = window.setTimeout(\n callback,\n this.DETECTION_INTERVAL_MS\n );\n }\n}\n\nexport const documentDetectionService = new DocumentDetectionService();\n"],"names":["__awaiter"],"mappings":";;;;AAUA,IAAA,wBAAA,kBAAA,YAAA;AAAA,IAAA,SAAA,wBAAA,GAAA;QACU,IA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C,MAAM,CAAC,IAAI,EAAE;AACzC,YAAA,OAAO,IAAI;QACb;AAEM,QAAA,IAAA,KAAmB,IAAI,CAAC,UAAU,CAAC,IAAI,EAAtC,EAAE,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,EAAE,QAAA,EAAE,EAAE,QAAA,EAAE,EAAE,QAAwB;AACvC,QAAA,IAAA,KAAmB,MAAM,CAAC,IAAI,EAA7B,EAAE,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,EAAE,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,EAAE,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,EAAE,QAAe;AAEpC,QAAA,IAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC;AACnD,QAAA,IAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC;AAEvD,QAAA,OAAO,KAAK,GAAG,EAAE,IAAI,SAAS,GAAG,EAAE;IACrC,CAAC;IAEO,wBAAA,CAAA,SAAA,CAAA,qBAAqB,GAA7B,UAA8B,QAAoB,EAAA;AAChD,QAAA,IAAI,IAAI,CAAC,kBAAkB,EAAE;AAC3B,YAAA,YAAY,CAAC,IAAI,CAAC,kBAAkB,CAAC;QACvC;AACA,QAAA,IAAI,CAAC,kBAAkB,GAAG,MAAM,CAAC,UAAU,CACzC,QAAQ,EACR,IAAI,CAAC,qBAAqB,CAC3B;IACH,CAAC;IACH,OAAA,wBAAC;AAAD,CAAC,EA7HD;AA+HO,IAAM,wBAAwB,GAAG,IAAI,wBAAwB;;;;;"}
@@ -1,153 +0,0 @@
1
- import { __awaiter, __generator } from '../node_modules/tslib/tslib.es6.js';
2
-
3
- var DocumentDetectionService = /** @class */ (function () {
4
- function DocumentDetectionService() {
5
- this.detectionTimeoutId = null;
6
- this.model = null;
7
- this.isDetecting = false;
8
- this.lastResult = null;
9
- this.DETECTION_INTERVAL_MS = 350;
10
- }
11
- DocumentDetectionService.prototype.loadModel = function () {
12
- return __awaiter(this, void 0, void 0, function () {
13
- var tf, cocoSsd, _a;
14
- return __generator(this, function (_b) {
15
- switch (_b.label) {
16
- case 0:
17
- if (!!this.model) return [3 /*break*/, 7];
18
- return [4 /*yield*/, import('@tensorflow/tfjs')];
19
- case 1:
20
- tf = _b.sent();
21
- return [4 /*yield*/, import('@tensorflow/tfjs-backend-webgl')];
22
- case 2:
23
- _b.sent();
24
- return [4 /*yield*/, import('@tensorflow-models/coco-ssd')];
25
- case 3:
26
- cocoSsd = _b.sent();
27
- return [4 /*yield*/, tf.setBackend("webgl")];
28
- case 4:
29
- _b.sent();
30
- return [4 /*yield*/, tf.ready()];
31
- case 5:
32
- _b.sent();
33
- _a = this;
34
- return [4 /*yield*/, cocoSsd.load()];
35
- case 6:
36
- _a.model = _b.sent();
37
- _b.label = 7;
38
- case 7: return [2 /*return*/];
39
- }
40
- });
41
- });
42
- };
43
- DocumentDetectionService.prototype.startDocumentDetection = function (videoElement, onDetectionChange) {
44
- return __awaiter(this, void 0, void 0, function () {
45
- var detect;
46
- var _this = this;
47
- return __generator(this, function (_a) {
48
- switch (_a.label) {
49
- case 0:
50
- this.stopDocumentDetection();
51
- return [4 /*yield*/, this.loadModel()];
52
- case 1:
53
- _a.sent();
54
- detect = function () { return __awaiter(_this, void 0, void 0, function () {
55
- var predictions, documentLike, result, err_1;
56
- return __generator(this, function (_a) {
57
- switch (_a.label) {
58
- case 0:
59
- if (!videoElement || !this.model || !videoElement.videoWidth) {
60
- this.scheduleNextDetection(detect);
61
- return [2 /*return*/];
62
- }
63
- if (this.isDetecting) {
64
- this.scheduleNextDetection(detect);
65
- return [2 /*return*/];
66
- }
67
- this.isDetecting = true;
68
- _a.label = 1;
69
- case 1:
70
- _a.trys.push([1, 3, 4, 5]);
71
- return [4 /*yield*/, this.model.detect(videoElement)];
72
- case 2:
73
- predictions = _a.sent();
74
- documentLike = predictions.find(function (pred) {
75
- return ["book", "cell phone", "tv", "laptop"].includes(pred.class) &&
76
- pred.score > 0.6;
77
- });
78
- result = documentLike
79
- ? {
80
- hasDocument: true,
81
- score: documentLike.score,
82
- label: documentLike.class,
83
- bbox: documentLike.bbox,
84
- timestamp: Date.now(),
85
- }
86
- : {
87
- hasDocument: false,
88
- timestamp: Date.now(),
89
- };
90
- if (this.shouldEmitResult(result)) {
91
- onDetectionChange(result);
92
- this.lastResult = result;
93
- }
94
- return [3 /*break*/, 5];
95
- case 3:
96
- err_1 = _a.sent();
97
- console.error("[DocumentDetection] Error during detection:", err_1);
98
- return [3 /*break*/, 5];
99
- case 4:
100
- this.isDetecting = false;
101
- this.scheduleNextDetection(detect);
102
- return [7 /*endfinally*/];
103
- case 5: return [2 /*return*/];
104
- }
105
- });
106
- }); };
107
- detect();
108
- return [2 /*return*/, {
109
- stop: function () { return _this.stopDocumentDetection(); },
110
- }];
111
- }
112
- });
113
- });
114
- };
115
- DocumentDetectionService.prototype.stopDocumentDetection = function () {
116
- if (this.detectionTimeoutId) {
117
- clearTimeout(this.detectionTimeoutId);
118
- this.detectionTimeoutId = null;
119
- }
120
- this.isDetecting = false;
121
- this.lastResult = null;
122
- };
123
- DocumentDetectionService.prototype.shouldEmitResult = function (result) {
124
- if (!this.lastResult) {
125
- return true;
126
- }
127
- if (this.lastResult.hasDocument !== result.hasDocument) {
128
- return true;
129
- }
130
- if (!result.hasDocument) {
131
- return false;
132
- }
133
- if (!this.lastResult.bbox || !result.bbox) {
134
- return true;
135
- }
136
- var _a = this.lastResult.bbox, x1 = _a[0], y1 = _a[1], w1 = _a[2], h1 = _a[3];
137
- var _b = result.bbox, x2 = _b[0], y2 = _b[1], w2 = _b[2], h2 = _b[3];
138
- var delta = Math.abs(x1 - x2) + Math.abs(y1 - y2);
139
- var sizeDelta = Math.abs(w1 - w2) + Math.abs(h1 - h2);
140
- return delta > 10 || sizeDelta > 10;
141
- };
142
- DocumentDetectionService.prototype.scheduleNextDetection = function (callback) {
143
- if (this.detectionTimeoutId) {
144
- clearTimeout(this.detectionTimeoutId);
145
- }
146
- this.detectionTimeoutId = window.setTimeout(callback, this.DETECTION_INTERVAL_MS);
147
- };
148
- return DocumentDetectionService;
149
- }());
150
- var documentDetectionService = new DocumentDetectionService();
151
-
152
- export { DocumentDetectionService, documentDetectionService };
153
- //# sourceMappingURL=documentDetectionService.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"documentDetectionService.js","sources":["../../../../src/services/documentDetectionService.ts"],"sourcesContent":["import type { ObjectDetection } from \"@tensorflow-models/coco-ssd\";\n\nexport interface DocumentDetectionResult {\n hasDocument: boolean;\n score?: number;\n label?: string;\n bbox?: [number, number, number, number];\n timestamp: number;\n}\n\nexport class DocumentDetectionService {\n private detectionTimeoutId: number | null = null;\n private model: ObjectDetection | null = null;\n private isDetecting = false;\n private lastResult: DocumentDetectionResult | null = null;\n private readonly DETECTION_INTERVAL_MS = 350;\n\n async loadModel() {\n if (!this.model) {\n const tf = await import(\"@tensorflow/tfjs\");\n await import(\"@tensorflow/tfjs-backend-webgl\");\n const cocoSsd = await import(\"@tensorflow-models/coco-ssd\");\n\n await tf.setBackend(\"webgl\");\n await tf.ready();\n\n this.model = await cocoSsd.load();\n }\n }\n\n async startDocumentDetection(\n videoElement: HTMLVideoElement | null,\n onDetectionChange: (result: DocumentDetectionResult) => void\n ): Promise<{ stop: () => void }> {\n this.stopDocumentDetection();\n await this.loadModel();\n\n const detect = async () => {\n if (!videoElement || !this.model || !videoElement.videoWidth) {\n this.scheduleNextDetection(detect);\n return;\n }\n\n if (this.isDetecting) {\n this.scheduleNextDetection(detect);\n return;\n }\n\n this.isDetecting = true;\n\n try {\n const predictions = await this.model.detect(videoElement);\n\n const documentLike = predictions.find(\n (pred) =>\n [\"book\", \"cell phone\", \"tv\", \"laptop\"].includes(pred.class) &&\n pred.score > 0.6\n );\n\n const result: DocumentDetectionResult = documentLike\n ? {\n hasDocument: true,\n score: documentLike.score,\n label: documentLike.class,\n bbox: documentLike.bbox,\n timestamp: Date.now(),\n }\n : {\n hasDocument: false,\n timestamp: Date.now(),\n };\n\n if (this.shouldEmitResult(result)) {\n onDetectionChange(result);\n this.lastResult = result;\n }\n } catch (err) {\n console.error(\"[DocumentDetection] Error during detection:\", err);\n } finally {\n this.isDetecting = false;\n this.scheduleNextDetection(detect);\n }\n };\n\n detect();\n\n return {\n stop: () => this.stopDocumentDetection(),\n };\n }\n\n stopDocumentDetection() {\n if (this.detectionTimeoutId) {\n clearTimeout(this.detectionTimeoutId);\n this.detectionTimeoutId = null;\n }\n this.isDetecting = false;\n this.lastResult = null;\n }\n\n private shouldEmitResult(result: DocumentDetectionResult): boolean {\n if (!this.lastResult) {\n return true;\n }\n\n if (this.lastResult.hasDocument !== result.hasDocument) {\n return true;\n }\n\n if (!result.hasDocument) {\n return false;\n }\n\n if (!this.lastResult.bbox || !result.bbox) {\n return true;\n }\n\n const [x1, y1, w1, h1] = this.lastResult.bbox;\n const [x2, y2, w2, h2] = result.bbox;\n\n const delta = Math.abs(x1 - x2) + Math.abs(y1 - y2);\n const sizeDelta = Math.abs(w1 - w2) + Math.abs(h1 - h2);\n\n return delta > 10 || sizeDelta > 10;\n }\n\n private scheduleNextDetection(callback: () => void) {\n if (this.detectionTimeoutId) {\n clearTimeout(this.detectionTimeoutId);\n }\n this.detectionTimeoutId = window.setTimeout(\n callback,\n this.DETECTION_INTERVAL_MS\n );\n }\n}\n\nexport const documentDetectionService = new DocumentDetectionService();\n"],"names":[],"mappings":";;AAUA,IAAA,wBAAA,kBAAA,YAAA;AAAA,IAAA,SAAA,wBAAA,GAAA;QACU,IAAA,CAAA,kBAAkB,GAAkB,IAAI;QACxC,IAAA,CAAA,KAAK,GAA2B,IAAI;QACpC,IAAA,CAAA,WAAW,GAAG,KAAK;QACnB,IAAA,CAAA,UAAU,GAAmC,IAAI;QACxC,IAA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