1 import { getTypeValues } from '../../helpers/contacts';
2 import type { PreVcardProperty, PreVcardsContact } from '../../interfaces/contacts/Import';
3 import type { VCardKey } from '../../interfaces/contacts/VCard';
5 // See './csv.ts' for the definition of pre-vCard and pre-vCards contact
8 * Modify the field (and accordingly the type, if needed) of a pre-vCard
10 const modifyPreVcardField = (preVcard: PreVcardProperty, newField: string) => {
11 const types = getTypeValues();
13 let newType: VCardKey | undefined = undefined;
14 if (types[newField]?.includes(preVcard.type || '')) {
15 newType = preVcard.type;
16 } else if (types[newField]?.length) {
17 newType = types[newField][0] as VCardKey;
20 return { ...preVcard, field: newField, type: newType, custom: false };
24 * Modify the field (and accordingly the type) of a pre-vCard inside a pre-vCards contact
26 export const modifyContactField = (preVcardsContact: PreVcardsContact, index: number, newField: string) => {
27 return preVcardsContact.map((preVcards, i) =>
28 i !== index ? preVcards : preVcards.map((preVcard) => modifyPreVcardField(preVcard, newField))
33 * Modify the type of a pre-vCard
35 const modifyPreVcardType = (preVcard: PreVcardProperty, newType: string) => ({
37 type: newType as VCardKey,
41 * Modify the type of a pre-vCard inside a pre-vCards contact
43 export const modifyContactType = (preVcardsContact: PreVcardsContact, index: number, newField: string) => {
44 return preVcardsContact.map((preVcards, i) =>
45 i !== index ? preVcards : preVcards.map((preVcard) => modifyPreVcardType(preVcard, newField))
50 * Toggle the checked attribute of a pre-vCard inside a pre-vCards contact
51 * @param {Object} preVcardsContact A pre-vCards contact
52 * @param {Number} groupIndex The index of the group of pre-Vcards where the pre-vCard to be modified is
53 * @param {Number} index The index of the pre-vCard within the group of pre-vCards
55 * @return {Array<Array<Object>>} the pre-vCards contact with the modified pre-vCard
57 export const toggleContactChecked = (preVcardsContact: PreVcardsContact, [groupIndex, index]: number[]) => {
58 const toggleFN = preVcardsContact[groupIndex][index].combineInto === 'fn-main';
59 const groupIndexN = toggleFN ? preVcardsContact.findIndex((group) => group[0].combineInto === 'n') : -1;
61 return preVcardsContact.map((preVcards, i) => {
62 if (i === groupIndex) {
63 return preVcards.map((preVcard, j) =>
64 j !== index ? preVcard : { ...preVcard, checked: !preVcard.checked }
67 if (toggleFN && i === groupIndexN) {
68 // When FN components are toggled, we also toggle the corresponding N components
69 const headerFN = preVcardsContact[groupIndex][index].header;
70 const indexN = preVcardsContact[groupIndexN].findIndex(({ header }) => header === headerFN);
71 return preVcards.map((preVcard, j) =>
72 j !== indexN ? preVcard : { ...preVcard, checked: !preVcard.checked }