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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import type { Page, Frame } from 'playwright';
import { logger } from '../../utils/logger';
import {
  QUOTA_CONFIRM_LABELS,
  SELECTORS,
  readScopeDeepText,
  readScopeVisibleDeepText,
} from '../selectors';

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

export interface QuotaLimitResult {
  limited: true;
  limit: number;
  retryText: string;
  message: string;
}

export interface QuotaWarningResult {
  warning: true;
  message: string;
}

export interface QuotaCheckResult {
  quotaLimited: boolean;
  limit: number | null;
  retryText: string | null;
  message: string | null;
  warning: string | null;
}

// ---------------------------------------------------------------------------
// Parsers
// ---------------------------------------------------------------------------

function parseSubmissionQuotaLimit(text: string): QuotaLimitResult | null {
  const normalized = String(text || '')
    .replace(/\s+/g, ' ')
    .trim();
  const match = normalized.match(
    /You have reached your limit of\s+(\d+)\s+submissions\.\s+You can submit again\s+(.+?)(?:\.|$)/i,
  );
  if (!match) return null;

  return {
    limited: true,
    limit: Number(match[1]),
    retryText: match[2],
    message: match[0],
  };
}

function parseSubmissionQuotaWarning(
  text: string,
): QuotaWarningResult | null {
  const normalized = String(text || '')
    .replace(/\s+/g, ' ')
    .trim();
  const patterns = [
    /(?:only\s+)?(?:1|one)\s+submissions?\s+remaining/i,
    /(?:only\s+)?(?:1|one)\s+remaining\s+submissions?/i,
    /(?:only\s+)?(?:1|one)\s+more\s+submissions?\s+left/i,
    /final\s+submission/i,
    /last\s+submission/i,
    /remaining\s+submissions?/i,
    /limited\s+number\s+of\s+submissions/i,
  ];

  if (!patterns.some((p) => p.test(normalized))) return null;

  return {
    warning: true,
    message: normalized.slice(0, 500),
  };
}

// ---------------------------------------------------------------------------
// Scope helpers
// ---------------------------------------------------------------------------

type Scope = Page | Frame;

const QUOTA_ACTION_SELECTOR = [
  'button',
  'input[type="button"]',
  'input[type="submit"]',
  'tdl-button',
  'tii-grn-button',
  '[role="button"]',
  '[slot="accept-button"]',
  '[part*="button"]',
  '[data-px*="Continue"]',
  '[with-data-px*="Continue"]',
].join(', ');

function getScopes(page: Page): Scope[] {
  return [
    page,
    ...page.frames().filter((f) => !f.url().includes('cookie-shim')),
  ];
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/**
 * Detect whether the page shows a hard quota limit (all submissions used).
 */
export async function detectQuotaLimit(
  page: Page,
): Promise<QuotaLimitResult | null> {
  for (const scope of getScopes(page)) {
    const text = await readScopeDeepText(scope);
    const result = parseSubmissionQuotaLimit(text);
    if (result) return result;
  }
  return null;
}

/**
 * Detect whether the page shows a quota warning (e.g. 1 submission remaining).
 */
export async function detectQuotaWarning(
  page: Page,
): Promise<QuotaWarningResult | null> {
  for (const scope of getScopes(page)) {
    const text = await readScopeVisibleDeepText(scope);
    const result = parseSubmissionQuotaWarning(text);
    if (result) return result;
  }
  return null;
}

/**
 * Attempt to confirm/dismiss a quota warning popup by clicking a continue
 * button. Returns true if a warning was found and confirmed.
 *
 * Improvements:
 * - Skips if the warning text is just background info (no active dialog/modal)
 * - Uses dialog.close() as a nuclear fallback to guarantee modal closure
 */
export async function confirmQuotaWarning(
  page: Page,
): Promise<boolean> {
  for (const scope of getScopes(page)) {
    const text = await readScopeVisibleDeepText(scope);
    const warning = parseSubmissionQuotaWarning(text);
    if (!warning) continue;

    // Check if there is ACTUALLY an interactive modal/dialog open.
    // Without this check, background informational text ("You have 1 more
    // submission left") keeps triggering the handler even when no dialog exists.
    const hasActionableDialog = await scope
      .evaluate(() => {
        const roots: (Document | ShadowRoot)[] = [document];
        const seen = new Set<Element>();
        for (let i = 0; i < roots.length; i++) {
          const root = roots[i];
          for (const el of Array.from(root.querySelectorAll('*'))) {
            if (seen.has(el)) continue;
            seen.add(el);
            if ((el as any).shadowRoot) roots.push((el as any).shadowRoot);
          }
        }
        const isVisible = (el: Element): boolean => {
          if (!(el instanceof HTMLElement)) return false;
          const s = window.getComputedStyle(el);
          const r = el.getBoundingClientRect();
          return s.visibility !== 'hidden' && s.display !== 'none' && r.width > 0 && r.height > 0;
        };
        const hasText = (el: Element, pattern: RegExp): boolean =>
          pattern.test((el as HTMLElement).innerText || el.textContent || '');

        for (const root of roots) {
          // Native <dialog open>
          for (const d of Array.from(root.querySelectorAll('dialog[open]'))) {
            if (isVisible(d) && hasText(d, /more submission|file upload|continue/i)) return true;
          }
          // Custom modal/overlay elements
          for (const el of Array.from(
            root.querySelectorAll('[role="dialog"], [aria-modal="true"], tii-modal, .tii-modal, [id*="modal"]'),
          )) {
            if (isVisible(el) && hasText(el, /more submission|file upload|continue/i)) return true;
          }
          // tii-grn-button with "Continue to File Upload" visible => quota dialog must be open
          for (const el of Array.from(root.querySelectorAll('tii-grn-button'))) {
            if (!isVisible(el)) continue;
            const t = ((el as HTMLElement).innerText || el.textContent || '').toLowerCase();
            if (t.includes('continue to file upload')) return true;
          }
        }
        return false;
      })
      .catch(() => false);

    if (!hasActionableDialog) {
      // Only background informational text — no interactive dialog to dismiss.
      logger.info('Quota warning text is background-only (no active dialog); skipping dismissal');
      continue;
    }

    logger.info('Submission quota warning dialog detected; attempting to dismiss', {
      message: warning.message,
    });

    const clicked = await clickQuotaWarningContinue(scope);
    if (!clicked) {
      logger.warn('Submission quota warning continue button was not found');
    }

    await page.waitForTimeout(1500);

    // If warning is still visible, try coordinate-based click
    const stillVisible = parseSubmissionQuotaWarning(
      await readScopeVisibleDeepText(scope),
    );
    if (stillVisible) {
      await clickQuotaWarningContinueByCoordinates(page, scope);
      await page.waitForTimeout(1500);
    }

    // Nuclear fallback: forcibly close any open dialog with quota/upload text
    const stillVisibleAfter = parseSubmissionQuotaWarning(
      await readScopeVisibleDeepText(scope),
    );
    if (stillVisibleAfter) {
      logger.warn('Quota warning still visible; using dialog.close() nuclear fallback');
      await scope
        .evaluate(() => {
          const roots: (Document | ShadowRoot)[] = [document];
          const seen = new Set<Element>();
          for (let i = 0; i < roots.length; i++) {
            const root = roots[i];
            for (const el of Array.from(root.querySelectorAll('*'))) {
              if (seen.has(el)) continue;
              seen.add(el);
              if ((el as any).shadowRoot) roots.push((el as any).shadowRoot);
            }
          }
          for (const root of roots) {
            for (const d of Array.from(root.querySelectorAll('dialog[open], dialog'))) {
              if (d instanceof HTMLDialogElement) {
                const t = (d as HTMLElement).innerText || d.textContent || '';
                if (/more submission|file upload|continue/i.test(t)) {
                  d.close();
                }
              }
            }
          }
        })
        .catch(() => {});
      await page.waitForTimeout(1000);
    }

    return true;
  }

  return false;
}

/**
 * Run a complete quota check: detect limit and/or warning.
 */
export async function runQuotaCheck(
  page: Page,
): Promise<QuotaCheckResult> {
  const limit = await detectQuotaLimit(page);
  if (limit) {
    return {
      quotaLimited: true,
      limit: limit.limit,
      retryText: limit.retryText,
      message: limit.message,
      warning: null,
    };
  }

  const warning = await detectQuotaWarning(page);
  return {
    quotaLimited: false,
    limit: null,
    retryText: null,
    message: null,
    warning: warning?.message || null,
  };
}

// ---------------------------------------------------------------------------
// Internal: click quota warning continue buttons
// ---------------------------------------------------------------------------

async function clickQuotaWarningContinue(
  scope: Scope,
): Promise<boolean> {
  // Try each preferred label in order
  for (const label of QUOTA_CONFIRM_LABELS) {
    const escaped = label.replace(/"/g, '\\"');
    const button = scope
      .locator(
        `tii-grn-button:has-text("${escaped}"), button:has-text("${escaped}"), tdl-button:has-text("${escaped}"), [role="button"]:has-text("${escaped}")`,
      )
      .last();
    if (await button.isVisible({ timeout: 1000 }).catch(() => false)) {
      await button.click({ force: true });
      return true;
    }

    const textTarget = scope.getByText(label, { exact: false }).last();
    if (
      await textTarget.isVisible({ timeout: 1000 }).catch(() => false)
    ) {
      await textTarget.click({ force: true });
      return true;
    }
  }

  // Try filled action slot button
  const filledAction = scope
    .locator(SELECTORS.quota.warningConfirmButton)
    .last();
  if (
    await filledAction.isVisible({ timeout: 1000 }).catch(() => false)
  ) {
    await filledAction.click({ force: true });
    return true;
  }

  // Deep shadow DOM scan fallback. Turnitin often renders the visible label in
  // a shadow/slot child, while the actual clickable target is the host element.
  return scope
    .evaluate(
      ({
        labels,
        selector,
      }: {
        labels: string[];
        selector: string;
      }) => {
      const roots: (Document | ShadowRoot)[] = [document];
      const seen = new Set<Element>();

      for (let i = 0; i < roots.length; i++) {
        const root = roots[i];
        for (const el of Array.from(root.querySelectorAll('*'))) {
          if (seen.has(el)) continue;
          seen.add(el);
          if (el.shadowRoot) roots.push(el.shadowRoot);
        }
      }

      const isVisible = (el: Element): boolean => {
        if (!(el instanceof HTMLElement)) return false;
        const style = window.getComputedStyle(el);
        const rect = el.getBoundingClientRect();
        return (
          style.visibility !== 'hidden' &&
          style.display !== 'none' &&
          rect.width > 0 &&
          rect.height > 0
        );
      };

      const isDisabled = (el: Element): boolean => {
        if (!(el instanceof HTMLElement)) return true;
        return (
          (el as HTMLButtonElement).disabled === true ||
          el.getAttribute('disabled') !== null ||
          el.getAttribute('aria-disabled') === 'true' ||
          el.getAttribute('with-disabled') === 'true' ||
          el.closest(
            '[disabled], [aria-disabled="true"], [with-disabled="true"], .disabled, .is-disabled',
          ) !== null
        );
      };

      const readText = (el: Element): string =>
        [
          (el as HTMLElement).innerText,
          el.textContent,
          el.getAttribute('aria-label'),
          el.getAttribute('value'),
          el.getAttribute('part'),
          el.getAttribute('slot'),
          el.getAttribute('data-px'),
          el.getAttribute('with-data-px'),
          el.getAttribute('with-px-label'),
        ]
          .filter(Boolean)
          .join(' ')
          .replace(/\s+/g, ' ')
          .trim()
          .toLowerCase();

      const resolveClickable = (el: Element): HTMLElement | null => {
        const direct = (el as HTMLElement).closest?.(
          'button, a, [role="button"], tdl-button, tii-grn-button, [slot="accept-button"]',
        ) as HTMLElement | null;
        const root = el.getRootNode();
        const host =
          root instanceof ShadowRoot ? (root.host as HTMLElement) : null;
        const clickable = direct || host || (el as HTMLElement);
        const innerButton = clickable.shadowRoot?.querySelector(
          'button:not([disabled]), input[type="submit"]:not([disabled]), input[type="button"]:not([disabled])',
        );
        return innerButton instanceof HTMLElement
          ? innerButton
          : clickable;
      };

      const candidates: Element[] = [];
      for (const root of roots) {
        candidates.push(...Array.from(root.querySelectorAll(selector)));
        candidates.push(...Array.from(root.querySelectorAll('*')));
      }

      const normalizedLabels = labels.map((l) => l.toLowerCase());
      const target = candidates
        .filter(isVisible)
        .map((el) => {
          const rect = el.getBoundingClientRect();
          const text = readText(el);
          const labelIndex = normalizedLabels.findIndex((l) =>
            text.includes(l),
          );
          const clickable = resolveClickable(el);
          return {
            element: el,
            clickable,
            labelIndex,
            area: rect.width * rect.height,
            disabled: clickable ? isDisabled(clickable) : true,
          };
        })
        .filter((c) => c.labelIndex >= 0 && !c.disabled && c.clickable)
        .sort(
          (a, b) => a.labelIndex - b.labelIndex || a.area - b.area,
        )[0]?.clickable;

      if (!target) return false;
      target.scrollIntoView({ block: 'center', inline: 'center' });
      target.click();
      return true;
    },
      { labels: QUOTA_CONFIRM_LABELS, selector: QUOTA_ACTION_SELECTOR },
    )
    .catch(() => false);
}

async function clickQuotaWarningContinueByCoordinates(
  page: Page,
  scope: Scope,
): Promise<boolean> {
  // Try filled action slot button first
  const filledAction = scope
    .locator(SELECTORS.quota.warningConfirmButton)
    .last();
  if (
    await filledAction.isVisible({ timeout: 1000 }).catch(() => false)
  ) {
    await filledAction.click({ force: true });
    return true;
  }

  // Try coordinate-based click on "Continue to File Upload"
  const label = 'Continue to File Upload';
  const textTarget = scope.getByText(label, { exact: false }).last();
  const box = await textTarget
    .boundingBox({ timeout: 1000 })
    .catch(() => null);
  if (box) {
    await page.mouse
      .click(box.x + box.width / 2, box.y + box.height / 2)
      .catch(() => {});
    return true;
  }

  // Deep shadow DOM coordinate search
  const point = await scope
    .evaluate(() => {
      const roots: (Document | ShadowRoot)[] = [document];
      const seen = new Set<Element>();

      for (let i = 0; i < roots.length; i++) {
        const root = roots[i];
        for (const el of Array.from(root.querySelectorAll('*'))) {
          if (seen.has(el)) continue;
          seen.add(el);
          if (el.shadowRoot) roots.push(el.shadowRoot);
        }
      }

      const isVisible = (el: Element): boolean => {
        if (!(el instanceof HTMLElement)) return false;
        const style = window.getComputedStyle(el);
        const rect = el.getBoundingClientRect();
        return (
          style.visibility !== 'hidden' &&
          style.display !== 'none' &&
          rect.width > 0 &&
          rect.height > 0
        );
      };

      const target = roots
        .flatMap((r) => Array.from(r.querySelectorAll('*')))
        .filter(isVisible)
        .map((el) => {
          const rect = el.getBoundingClientRect();
          const text = [
            (el as HTMLElement).innerText,
            el.textContent,
          ]
            .filter(Boolean)
            .join(' ')
            .replace(/\s+/g, ' ')
            .trim()
            .toLowerCase();
          return { rect, text, area: rect.width * rect.height };
        })
        .filter((c) => c.text.includes('continue to file upload'))
        .sort((a, b) => a.area - b.area)[0];

      if (!target) return null;
      return {
        x: target.rect.x + target.rect.width / 2,
        y: target.rect.y + target.rect.height / 2,
      };
    })
    .catch(() => null);

  if (point) {
    await page.mouse.click(point.x, point.y).catch(() => {});
    return true;
  }

  // Final fallback: click approximate center of common dialog area
  await page.mouse.click(890, 670).catch(() => {});
  return true;
}