File size: 12,953 Bytes
07bbbbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * ConvertDBDialog Component
 * 
 * Dialog for configuring collation parameters and database conversion options
 * Uses ICU4X collation settings based on BCP 47 locale extensions
 */

import React, { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import {
  IonItem,
  IonLabel,
  IonList,
  IonNote,
  IonCheckbox,
} from '@ionic/react';
import { MdxProfile } from '../types';

// ICU4X Collation Types
export interface CollationOptions {
  language: string;
  collationType?: string;
  strength: 'level1' | 'level2' | 'level3' | 'level4';
  alternateHandling: 'noignore' | 'shifted';
  caseLevel: 'true' | 'false';
  caseFirst: 'off' | 'upper' | 'lower';
}

// Conversion Options
export interface ConvertOptions {
  collation: CollationOptions;
  removeOldFiles: boolean;
  createFulltextIndex: boolean;
}

// Supported languages with collation (based on ICU4X documentation)
// This list includes languages with special collation rules
const COLLATION_LANGUAGES = [
  { code: 'ar', nameKey: 'lang_arabic', collations: ['standard'] },
  { code: 'ca', nameKey: 'lang_catalan', collations: ['standard'] },
  { code: 'cs', nameKey: 'lang_czech', collations: ['standard'] },
  { code: 'da', nameKey: 'lang_danish', collations: ['standard'] },
  { code: 'de', nameKey: 'lang_german', collations: ['standard', 'phonebk'] },
  { code: 'el', nameKey: 'lang_greek', collations: ['standard'] },
  { code: 'en', nameKey: 'lang_english', collations: ['standard'] },
  { code: 'es', nameKey: 'lang_spanish', collations: ['standard', 'trad'] },
  { code: 'fi', nameKey: 'lang_finnish', collations: ['standard'] },
  { code: 'fr', nameKey: 'lang_french', collations: ['standard'] },
  { code: 'he', nameKey: 'lang_hebrew', collations: ['standard'] },
  { code: 'hi', nameKey: 'lang_hindi', collations: ['standard'] },
  { code: 'hu', nameKey: 'lang_hungarian', collations: ['standard'] },
  { code: 'is', nameKey: 'lang_icelandic', collations: ['standard'] },
  { code: 'it', nameKey: 'lang_italian', collations: ['standard'] },
  { code: 'ja', nameKey: 'lang_japanese', collations: ['standard'] },
  { code: 'ko', nameKey: 'lang_korean', collations: ['standard'] },
  { code: 'nb', nameKey: 'lang_norwegian', collations: ['standard'] },
  { code: 'nl', nameKey: 'lang_dutch', collations: ['standard'] },
  { code: 'pl', nameKey: 'lang_polish', collations: ['standard'] },
  { code: 'pt', nameKey: 'lang_portuguese', collations: ['standard'] },
  { code: 'ro', nameKey: 'lang_romanian', collations: ['standard'] },
  { code: 'ru', nameKey: 'lang_russian', collations: ['standard'] },
  { code: 'sv', nameKey: 'lang_swedish', collations: ['standard'] },
  { code: 'th', nameKey: 'lang_thai', collations: ['standard'] },
  { code: 'tr', nameKey: 'lang_turkish', collations: ['standard'] },
  { code: 'uk', nameKey: 'lang_ukrainian', collations: ['standard'] },
  { code: 'vi', nameKey: 'lang_vietnamese', collations: ['standard'] },
  { code: 'zh', nameKey: 'lang_chinese', collations: ['standard','pinyin', 'stroke', 'zhuyin'] },
];

// Strength level descriptions (keys for i18n)
const STRENGTH_DESCRIPTIONS = {
  level1: {
    labelKey: 'Level 1 (Primary)',
    descriptionKey: 'Compares base characters only, ignoring accents and case',
    exampleKey: 'strength_level1_example',
  },
  level2: {
    labelKey: 'Level 2 (Secondary)',
    descriptionKey: 'Compares base characters and accents, ignoring case',
    exampleKey: 'strength_level2_example',
  },
  level3: {
    labelKey: 'Level 3 (Tertiary)',
    descriptionKey: 'Compares base characters, accents, and case',
    exampleKey: 'strength_level3_example',
  },
  level4: {
    labelKey: 'Level 4 (Quaternary)',
    descriptionKey: 'Distinguishes punctuation and whitespace',
    exampleKey: 'strength_level4_example',
  },
};

interface ConvertDBDialogProps {
  profile: MdxProfile;
  options: ConvertOptions;
  onOptionsChange: (options: ConvertOptions) => void;
  isProfileInUse?: boolean;
}

export const ConvertDBDialog: React.FC<ConvertDBDialogProps> = ({
  profile,
  options,
  onOptionsChange,
  isProfileInUse = false,
}) => {
  const { t } = useTranslation();

  // Get available collation types for selected language
  const availableCollations = useMemo(() => {
    const lang = COLLATION_LANGUAGES.find((l) => l.code === options.collation.language);
    return lang?.collations || ['standard'];
  }, [options.collation.language]);

  // Shared select styles
  const selectStyle: React.CSSProperties = {
    width: '100%',
    padding: '10px',
    marginTop: '8px',
    fontSize: '16px',
    border: '1px solid var(--ion-color-medium)',
    borderRadius: '8px',
    backgroundColor: 'var(--ion-background-color)',
    color: 'var(--ion-text-color)',
    cursor: 'pointer',
  };

  return (
      <>
        <h2 style={{ marginTop: 0 }}>{t('Rebuild Index')} : {profile.title}</h2>
        <IonLabel>
          {t('Collation settings for dictionary index')}
        </IonLabel>

        <IonList style={{ 
          background: 'transparent',
          maxHeight: '60vh',
          overflowY: 'auto'
        }}>
            {/* Language Selection */}
            <IonItem lines="none">
              <IonLabel position="stacked">
                {t('Language') + ': '}
                <IonNote>{t('Select the primary language for sorting')}</IonNote>
              </IonLabel>
              <select
                value={options.collation.language}
                onChange={(e) => {
                  const newLang = e.target.value;
                  const lang = COLLATION_LANGUAGES.find((l) => l.code === newLang);
                  onOptionsChange({
                    ...options,
                    collation: {
                      ...options.collation,
                      language: newLang,
                      collationType: lang?.collations[0] || 'standard',
                    },
                  });
                }}
                style={selectStyle}
              >
                {COLLATION_LANGUAGES.map((lang) => (
                  <option key={lang.code} value={lang.code}>
                    {t(lang.nameKey)} ({lang.code})
                  </option>
                ))}
              </select>
            </IonItem>

            {/* Collation Type Selection */}
            {availableCollations.length > 1 && (
              <IonItem lines="none">
                <IonLabel position="stacked">
                  {t('Collation Type') + ': '}
                  <IonNote>
                    {t('Language-specific sorting algorithm')}
                  </IonNote>
                </IonLabel>
                <select
                  value={options.collation.collationType}
                  onChange={(e) =>
                    onOptionsChange({ 
                      ...options, 
                      collation: { ...options.collation, collationType: e.target.value }
                    })
                  }
                  style={selectStyle}
                >
                  {availableCollations.map((collation) => (
                    <option key={collation} value={collation}>
                      {t(`collation_${collation}`)}
                    </option>
                  ))}
                </select>
              </IonItem>
            )}

            {/* Strength Level */}
            <IonItem lines="none">
              <IonLabel position="stacked">
                {t('Collation Strength') + ': '}
                <IonNote>
                  {t('Determines how strictly characters are compared during sorting')}
                </IonNote>
              </IonLabel>
              <select
                value={options.collation.strength}
                onChange={(e) => onOptionsChange({ 
                  ...options, 
                  collation: { ...options.collation, strength: e.target.value as any }
                })}
                style={selectStyle}
              >
                {Object.entries(STRENGTH_DESCRIPTIONS).map(([key, desc]) => (
                  <option key={key} value={key}>
                    {t(desc.labelKey)} - {t(desc.descriptionKey)}
                  </option>
                ))}
              </select>
              <IonNote style={{ marginTop: '8px', display: 'block' }}>
                <strong>{t('Example')}:</strong>{' '}
                {t(STRENGTH_DESCRIPTIONS[options.collation.strength].exampleKey)}
              </IonNote>
            </IonItem>

            {/* Alternate Handling */}
            <IonItem lines="none">
              <IonLabel position="stacked">
                {t('Alternate Handling') + ': '}
                <IonNote>
                  {t('How to treat punctuation and whitespace')}
                </IonNote>
              </IonLabel>
              <select
                value={options.collation.alternateHandling}
                onChange={(e) =>
                  onOptionsChange({ 
                    ...options, 
                    collation: { ...options.collation, alternateHandling: e.target.value as any }
                  })
                }
                style={selectStyle}
              >
                <option value="noignore">
                  {t('Non-Ignorable')} - {t('Treat punctuation as significant')}
                </option>
                <option value="shifted">
                  {t('Shifted')} - {t('Treat punctuation as minor importance')}
                </option>
              </select>
            </IonItem>

            {/* Case Level */}
            <IonItem lines="none">
              <IonLabel position="stacked">
                {t('Case Level') + ': '}
                <IonNote>
                  {t('Enable case-sensitive sorting at primary strength')}
                </IonNote>
              </IonLabel>
              <select
                value={options.collation.caseLevel}
                onChange={(e) => onOptionsChange({ 
                  ...options, 
                  collation: { ...options.collation, caseLevel: e.target.value as any }
                })}
                style={selectStyle}
              >
                <option value="false">{t('Off')}</option>
                <option value="true">{t('On')}</option>
              </select>
            </IonItem>

            {/* Case First */}
            <IonItem lines="none">
              <IonLabel position="stacked">
                {t('Case First') + ': '}
                <IonNote>
                  {t('Sort uppercase or lowercase letters first')}
                </IonNote>
              </IonLabel>
              <select
                value={options.collation.caseFirst}
                onChange={(e) => onOptionsChange({ 
                  ...options, 
                  collation: { ...options.collation, caseFirst: e.target.value as any }
                })}
                style={selectStyle}
              >
                <option value="off">{t('Default')}</option>
                <option value="upper">{t('Uppercase First')}</option>
                <option value="lower">{t('Lowercase First')}</option>
              </select>
            </IonItem>

            {/* Replace Original File */}
            <IonItem lines="none">
              <IonLabel>
                <h3>{t('Replace Original File')}</h3>
                <IonNote>
                  {isProfileInUse 
                    ? t('File is currently in use and cannot be replaced')
                    : t('Automatically replace original MDX/MDD files after successful conversion')}
                </IonNote>
              </IonLabel>
              <IonCheckbox
                slot="start"
                checked={options.removeOldFiles && !isProfileInUse}
                disabled={isProfileInUse}
                onIonChange={(e) => onOptionsChange({ ...options, removeOldFiles: e.detail.checked })}
              />
            </IonItem>

            {/* Create Fulltext Index */}
            <IonItem lines="none">
              <IonLabel>
                <h3>{t('Create Fulltext Index')}</h3>
                <IonNote>
                  {t('Create fulltext search index after conversion for faster searching')}
                </IonNote>
              </IonLabel>
              <IonCheckbox
                slot="start"
                checked={options.createFulltextIndex}
                onIonChange={(e) => onOptionsChange({ ...options, createFulltextIndex: e.detail.checked })}
              />
            </IonItem>
          </IonList>
      </>
  );
};

/**
 * Hook for using ConvertDBDialog imperatively
 */
export const useConvertDBDialog = () => {
  const showConvertDBDialog = (
    profile: MdxProfile,
    onConvert: (profileId: number, options: ConvertOptions) => Promise<void>
  ) => {
    // This would need to be implemented using the EnhancedDialog system
    // For now, we'll use this as a typed interface
    return { profile, onConvert };
  };

  return { showConvertDBDialog };
};