File size: 12,639 Bytes
41e1749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53a22ae
 
 
 
 
 
 
 
 
 
 
41e1749
 
 
53a22ae
a9630ec
 
1eb5022
 
 
 
 
 
 
a9630ec
1eb5022
41e1749
 
 
a9630ec
41e1749
 
 
 
 
 
1eb5022
41e1749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e1b7ae
 
41e1749
 
1e1b7ae
afdf449
1e1b7ae
afdf449
 
1e1b7ae
 
41e1749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6451a95
 
 
 
 
 
 
 
 
41e1749
 
 
 
 
1eb5022
a9630ec
 
 
 
41e1749
 
 
 
a9630ec
 
41e1749
 
 
 
1eb5022
 
 
 
a9630ec
 
1eb5022
a9630ec
1eb5022
 
 
 
 
 
 
 
 
 
41e1749
 
 
a9630ec
 
41e1749
 
 
 
a9630ec
 
 
41e1749
 
 
 
 
 
 
 
a9630ec
 
41e1749
 
 
acf7dfb
41e1749
 
 
 
 
 
 
ed2cae2
 
acf7dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed2cae2
41e1749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Bayan UI helpers — score, suggestions list, mobile nav, bottom sheet

const TYPE_LABELS = {
  spelling: 'إملائي',
  grammar: 'نحوي',
  punctuation: 'ترقيم'
};

const SCORE_CIRCUMFERENCE = 440;

/**
 * Calculate writing score from suggestion counts
 */
function calculateWritingScore(spelling, grammar, punctuation) {
  const score = 100 - spelling * 8 - grammar * 6 - punctuation * 3;
  return Math.max(0, Math.min(100, score));
}

/**
 * Update the score ring UI
 */
function updateWritingScore(spelling, grammar, punctuation) {
  const score = calculateWritingScore(spelling, grammar, punctuation);
  const valueEl = document.getElementById('score-value');
  const circleEl = document.getElementById('score-circle');
  const hintEl = document.getElementById('score-hint');

  if (valueEl) {
    valueEl.textContent = score > 0 || (spelling + grammar + punctuation) > 0
      ? score.toLocaleString('ar-EG')
      : '--';
  }

  if (circleEl) {
    const offset = SCORE_CIRCUMFERENCE - (score / 100) * SCORE_CIRCUMFERENCE;
    circleEl.style.strokeDashoffset = String(offset);
  }

  if (hintEl) {
    const total = spelling + grammar + punctuation;
    if (total === 0) {
      hintEl.innerHTML = 'ابدأ الكتابة لرؤية تقييمك<br><span class="text-xs">تحسين القواعد يرفع التقييم</span>';
    } else if (score >= 90) {
      hintEl.textContent = 'كتابة ممتازة! استمر.';
    } else if (score >= 70) {
      hintEl.textContent = 'جيد — راجع الاقتراحات لتحسين النص.';
    } else {
      hintEl.textContent = 'يحتاج النص إلى بعض التحسينات.';
    }
  }

  const sheetCount = document.getElementById('mobile-suggestion-count');
  if (sheetCount) {
    const total = spelling + grammar + punctuation;
    sheetCount.textContent = total.toLocaleString('ar-EG');
  }
}

/**
 * Build HTML for a single suggestion card
 */
/**
 * Resolve alternatives for a suggestion, falling back to [correction, original]
 * when the model doesn't provide alternatives (e.g. grammar, punctuation).
 * Shared logic — must stay in sync with tooltip rendering in editor.js.
 */
function resolveAlternatives(suggestion) {
  return (suggestion.alternatives && suggestion.alternatives.length > 0)
    ? suggestion.alternatives
    : [suggestion.correction, suggestion.original];
}

function buildSuggestionCardHTML(suggestion, index) {
  const badgeClass = `badge-${suggestion.type}`;
  const label = TYPE_LABELS[suggestion.type] || suggestion.type;
  const alts = resolveAlternatives(suggestion);
  // Pipeline Hardening v3.3: Use suggestion.id (UUID) instead of array index
  const suggestionId = suggestion.id || index;

  let altsHTML = '';
  alts.forEach((alt, i) => {
    const isKeep = alt === suggestion.original;
    const isMain = i === 0;
    const cls = isKeep ? 'alt-chip alt-chip--keep' : (isMain ? 'alt-chip alt-chip--main' : 'alt-chip');
    const chipLabel = isKeep ? `${escapeHtml(alt)} ✓` : escapeHtml(alt);
    altsHTML += `<button class="${cls}" data-card-alt="${escapeHtml(alt)}" data-card-id="${suggestionId}" type="button">${chipLabel}</button>`;
  });

  return `
    <div class="suggestion-card" role="listitem" tabindex="0"
      data-suggestion-id="${suggestionId}"
      aria-label="${label}: ${suggestion.original} إلى ${suggestion.correction}">
      <span class="suggestion-card-badge ${badgeClass}">${label}</span>
      <div class="suggestion-card-change">
        <span class="suggestion-card-original">${escapeHtml(suggestion.original)}</span>
        <span class="suggestion-card-arrow">←</span>
      </div>
      <div class="suggestion-card-alts">${altsHTML}</div>
    </div>`;
}

/**
 * Render suggestions into sidebar and bottom sheet lists
 */
function updateSuggestionsList(suggestions) {
  const lists = [
    document.getElementById('suggestions-list'),
    document.getElementById('bottom-sheet-list')
  ].filter(Boolean);

  const applyAllBtn = document.getElementById('apply-all-btn');
  const applyAllSheet = document.getElementById('apply-all-sheet');

  if (!suggestions || suggestions.length === 0) {
    const editorText = typeof getEditorText === 'function' ? getEditorText().trim() : '';
    const hasText = editorText.length > 0;
    const emptyHTML = `
      <div class="empty-state">
        <div class="empty-state__icon" style="color: ${hasText ? '#22c55e' : 'var(--text-secondary)'}">
          <svg width="24" height="24" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="${hasText ? 'M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z' : 'M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z'}"/>
          </svg>
        </div>
        <div class="empty-state__title">${hasText ? 'نصك ممتاز!' : 'لا توجد اقتراحات'}</div>
        <div class="empty-state__desc">${hasText ? 'لم نجد أي أخطاء — أحسنت! ✨' : 'ابدأ بكتابة نص عربي وسيتم تحليله تلقائياً'}</div>
      </div>`;

    lists.forEach((el) => { el.innerHTML = emptyHTML; });
    if (applyAllBtn) applyAllBtn.classList.add('is-hidden');
    if (applyAllSheet) applyAllSheet.classList.add('is-hidden');
    return;
  }

  const cardsHTML = suggestions.map((s, i) => buildSuggestionCardHTML(s, i)).join('');

  lists.forEach((el) => {
    el.innerHTML = cardsHTML;
    bindSuggestionCardEvents(el);
  });

  const showApplyAll = suggestions.length >= 2;
  const countLabel = suggestions.length.toLocaleString('ar-EG');
  if (applyAllBtn) {
    applyAllBtn.classList.toggle('is-hidden', !showApplyAll);
    if (showApplyAll) applyAllBtn.textContent = '\u062a\u0637\u0628\u064a\u0642 \u0627\u0644\u0643\u0644 (' + countLabel + ')';
  }
  if (applyAllSheet) {
    applyAllSheet.classList.toggle('is-hidden', !showApplyAll);
    if (showApplyAll) applyAllSheet.textContent = '\u062a\u0637\u0628\u064a\u0642 \u0627\u0644\u0643\u0644 (' + countLabel + ')';
  }
}

function bindSuggestionCardEvents(container) {
  container.querySelectorAll('.suggestion-card').forEach((card) => {
    card.addEventListener('click', (e) => {
      if (e.target.closest('.alt-chip') || e.target.closest('.suggestion-card-apply')) return;
      // Pipeline Hardening v3.3: UUID-based lookup
      const suggestionId = card.dataset.suggestionId;
      scrollToSuggestion(suggestionId);
      focusSuggestionInEditor(suggestionId);
    });

    card.addEventListener('keydown', (e) => {
      if (e.key === 'Enter') {
        const suggestionId = card.dataset.suggestionId;
        applySuggestionById(suggestionId);
      }
    });
  });

  // Alt chip clicks
  container.querySelectorAll('.alt-chip').forEach((chip) => {
    chip.addEventListener('click', (e) => {
      e.stopPropagation();
      // Pipeline Hardening v3.3: UUID-based lookup
      const suggestionId = chip.dataset.cardId;
      const altText = chip.dataset.cardAlt;
      const suggestion = findSuggestionById(suggestionId);
      if (!suggestion) return;

      if (altText === suggestion.original) {
        dismissSuggestion(suggestion);
      } else {
        applyAlternativeCorrection(suggestion, altText);
      }
    });
  });

  container.querySelectorAll('.suggestion-card-apply').forEach((btn) => {
    btn.addEventListener('click', (e) => {
      e.stopPropagation();
      const suggestionId = btn.dataset.applyId;
      applySuggestionById(suggestionId);
    });
  });
}

function scrollToSuggestion(suggestionId) {
  // Pipeline Hardening v3.3: UUID-based span lookup
  const span = document.querySelector(`[data-suggestion-id="${suggestionId}"]`);
  if (span) {
    span.scrollIntoView({ behavior: 'smooth', block: 'center' });
    span.classList.add('highlight-active');
    setTimeout(() => span.classList.remove('highlight-active'), 1500);
    showTooltip(span);
  }
}

function focusSuggestionInEditor(suggestionId) {
  const span = document.querySelector(`[data-suggestion-id="${suggestionId}"]`);
  if (span) showTooltip(span);
}

let _analyzingTimer = null;
function setAnalyzingState(isAnalyzing) {
  const editor = getEditorElement();
  const indicator = document.getElementById('analyzing-indicator');

  if (editor) {
    editor.setAttribute('aria-busy', isAnalyzing ? 'true' : 'false');
  }

  if (isAnalyzing) {
    // Debounce: only show indicator after 400ms to prevent flicker on fast re-analyses
    if (!_analyzingTimer) {
      _analyzingTimer = setTimeout(() => {
        if (editor) editor.classList.add('analyzing');
        if (indicator) indicator.classList.add('active');
        // Show skeleton loading in suggestions panel
        const lists = [
          document.getElementById('suggestions-list'),
          document.getElementById('bottom-sheet-list')
        ].filter(Boolean);
        const skeletonHTML = `
          <div style="padding: 16px;">
            <div class="skeleton skeleton-line" style="width:80%"></div>
            <div class="skeleton skeleton-line" style="width:100%"></div>
            <div class="skeleton skeleton-line" style="width:65%"></div>
          </div>`;
        lists.forEach(el => { el.innerHTML = skeletonHTML; });
      }, 400);
    }
  } else {
    // Clear timer and hide immediately
    if (_analyzingTimer) {
      clearTimeout(_analyzingTimer);
      _analyzingTimer = null;
    }
    if (editor) editor.classList.remove('analyzing');
    if (indicator) indicator.classList.remove('active');
  }
}

/* ── Mobile navigation ── */
function initMobileNav() {
  const btn = document.getElementById('mobile-menu-btn');
  const drawer = document.getElementById('mobile-drawer');
  const backdrop = document.getElementById('mobile-drawer-backdrop');
  const closeBtn = document.getElementById('mobile-drawer-close');

  if (!btn || !drawer) return;

  function openDrawer() {
    drawer.classList.add('open');
    btn.setAttribute('aria-expanded', 'true');
    document.body.style.overflow = 'hidden';
  }

  function closeDrawer() {
    drawer.classList.remove('open');
    btn.setAttribute('aria-expanded', 'false');
    document.body.style.overflow = '';
  }

  btn.addEventListener('click', openDrawer);
  if (backdrop) backdrop.addEventListener('click', closeDrawer);
  if (closeBtn) closeBtn.addEventListener('click', closeDrawer);

  drawer.querySelectorAll('.mobile-drawer-link').forEach((link) => {
    link.addEventListener('click', () => {
      closeDrawer();
      const page = link.dataset.page;
      if (page) showPage(page);
    });
  });
}

/* ── Bottom sheet ── */
function initBottomSheet() {
  const trigger = document.getElementById('mobile-sheet-trigger');
  const sheet = document.getElementById('bottom-sheet');
  const backdrop = document.getElementById('bottom-sheet-backdrop');
  const closeBtn = document.getElementById('bottom-sheet-close');

  if (!trigger || !sheet) return;

  function openSheet() {
    sheet.classList.add('open');
    trigger.setAttribute('aria-expanded', 'true');
  }

  function closeSheet() {
    sheet.classList.remove('open');
    trigger.setAttribute('aria-expanded', 'false');
  }

  trigger.addEventListener('click', openSheet);
  if (backdrop) backdrop.addEventListener('click', closeSheet);
  if (closeBtn) closeBtn.addEventListener('click', closeSheet);
}

function initUI() {
  initMobileNav();
  initBottomSheet();
  updateWritingScore(0, 0, 0);
}

/**
 * Show non-blocking document operation toast
 * @param {string} message
 * @param {'success'|'error'|'info'} type
 */
function showDocToast(message, type = 'info') {
  let toast = document.getElementById('doc-toast');
  if (!toast) {
    toast = document.createElement('div');
    toast.id = 'doc-toast';
    toast.className = 'doc-toast';
    toast.setAttribute('role', 'status');
    toast.setAttribute('aria-live', 'polite');
    document.body.appendChild(toast);
  }
  toast.textContent = message;
  toast.className = `doc-toast doc-toast--${type} is-visible`;
  clearTimeout(toast._hideTimer);
  toast._hideTimer = setTimeout(() => toast.classList.remove('is-visible'), 3500);
}

/**
 * Show/hide analysis length warning banner
 * @param {boolean} show
 */
function updateAnalysisLimitBanner(show) {
  const banner = document.getElementById('analysis-limit-banner');
  if (!banner) return;
  if (show) {
    banner.classList.remove('is-hidden');
    banner.textContent = 'النص أطول من الحد المسموح للتحليل. سيتم تحليل أول 5000 حرف فقط.';
  } else {
    banner.classList.add('is-hidden');
  }
}