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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 { IMolsToConsider } from "@/FileSystem/LoadSaveMolModels/SaveMolModels/Types";
import { getMoleculesFromStore } from "@/Store/StoreExternalAccess";
import { TreeNode } from "../../../TreeNodes/TreeNode/TreeNode";
import { SelectedType, TreeNodeType } from "./TreeInterfaces";
import { TreeNodeList } from "../../../TreeNodes/TreeNodeList/TreeNodeList";
import { treeNodeDeepClone } from "../../../TreeNodes/Deserializers";
import { makeEasyParser } from "@/FileSystem/LoadSaveMolModels/ParseMolModels/EasyParser";
import { EasyParserParent } from "@/FileSystem/LoadSaveMolModels/ParseMolModels/EasyParser/EasyParserParent";
import * as api from "@/Api";
import { YesNo } from "@/UI/MessageAlerts/Popups/InterfacesAndEnums";
import { getSetting } from "@/Plugins/Core/Settings/LoadSaveSettings";

/**
 * Selects nodes in a list based on a condition function.
 *
 * @param {TreeNodeList} treeNodeList  The list of nodes to process.
 * @param {(node: TreeNode) => boolean} conditionFunc  Returns true if the node should be selected.
 */
export function selectNodesBasedOnCondition(
    treeNodeList: TreeNodeList,
    conditionFunc: (node: TreeNode) => boolean
) {
    const allNodes = treeNodeList.flattened;
    allNodes.forEach((n) => {
        if (conditionFunc(n)) {
            n.selected = SelectedType.True;
        } else {
            n.selected = SelectedType.False;
        }
    });
}

/**
 * Given a IMolsToConsider variable, gets the molecules to consider.
 *
 * @param  {IMolsToConsider} molsToConsider   The molsToUse variable.
 * @param  {TreeNodeList} [terminalNodes]  The list of molecules to consider.
 *                                            If undefined, gets all molecules
 *                                            from VueX store.
 * @returns {TreeNodeList}  The molecules to consider.
 */
export function getTerminalNodesToConsider(
    molsToConsider: IMolsToConsider,
    terminalNodes?: TreeNodeList
): TreeNodeList {
    if (terminalNodes === undefined) {
        terminalNodes = getMoleculesFromStore();
    }

    // Get the terminal nodes
    terminalNodes = terminalNodes.filters.onlyTerminal;

    let molsToKeep = new TreeNodeList();

    if (molsToConsider.visible) {
        molsToKeep.extend(terminalNodes.filters.keepVisible());
    }

    if (molsToConsider.selected) {
        molsToKeep.extend(terminalNodes.filters.keepSelected());
    }

    if (molsToConsider.hiddenAndUnselected) {
        molsToKeep.extend(
            // mol_filter_ok
            terminalNodes.filter(
                (m: TreeNode) => !m.visible && m.selected === SelectedType.False
            )
        );
    }

    // Make sure there are no duplicates (visible and selected, for example).
    molsToKeep = molsToKeep.filters.onlyUnique;

    return molsToKeep;
}

/**
 * Clones a list of molecules, optionally including their ancestors (direct) and
 * all children. Also assigns new ids, etc. Can't be under TreeNodeList because
 * it calls treeNodeDeepClone (circular dependencies).
 *
 * @param  {TreeNodeList} treeNodeList        The list of molecules to clone.
 * @param  {boolean}  [includeAncestors=true]  If true, includes the ancestors
 *                                             of the molecules in the clone.
 * @returns {Promise<TreeNodeList>}  A promise that resolves the cloned list of
 *     molecules.
 */
export function cloneMolsWithAncestry(
    treeNodeList: TreeNodeList,
    includeAncestors = true
): Promise<TreeNodeList> {
    const allMols = getMoleculesFromStore();
    const promises: Promise<TreeNode>[] = [];

    treeNodeList.forEach((nodeToActOn: TreeNode) => {
        // Get ancestors if includeAncestors specified
        let nodeGenealogy: TreeNodeList;
        if (includeAncestors) {
            // nodeGenealogy = getNodeAncestory(nodeToActOn.id as string, allMols);
            nodeGenealogy = nodeToActOn.getAncestry(allMols);

            // Make shallow copies of all the nodes in the ancestory
            for (let i = 0; i < nodeGenealogy.length; i++) {
                // nodeGenealogy.set(i, new TreeNode({ ...nodeGenealogy.get(i) }));
                const clonedNode = nodeGenealogy.get(i).shallowCopy();
                nodeGenealogy.set(i, clonedNode);
            }

            // Empty the nodes except for the last one (which is the same as
            // nodeToActOn). To avoid including sibling nodes (outside of this
            // ancestory).
            for (let i = 0; i < nodeGenealogy.length - 1; i++) {
                // TODO: This is clearing nodes from main molecule. Has not made
                // copies
                nodeGenealogy.get(i).clearChildren();
            }

            // Place each node in the ancestory under the next node.
            for (let i = 0; i < nodeGenealogy.length - 1; i++) {
                nodeGenealogy.get(i).nodes?.push(nodeGenealogy.get(i + 1));

                // Also, parentId
                nodeGenealogy.get(i + 1).parentId = nodeGenealogy.get(i).id;
            }
        } else {
            // No ancestors requested. Just include this one.
            nodeGenealogy = new TreeNodeList([nodeToActOn.shallowCopy()]);
        }

        // Now you must make a deep copy of everything from the top node down,
        // including children of current nodeToActOn.
        const clonedTree = treeNodeDeepClone(nodeGenealogy.get(0), true)
            .then((topNode: TreeNode): TreeNode => {
                // Get all the nodes as a flat TreeNodeList, including topNode. This
                // is just to make it easier to process each node.
                const allNodesFlattened = new TreeNodeList([topNode]);
                const children = topNode.nodes;
                if (children) {
                    allNodesFlattened.extend(children.flattened);
                }

                // Also unselect, unfocus, and mark as viewerDirty.
                allNodesFlattened.forEach((node: TreeNode) => {
                    node.selected = SelectedType.False;
                    node.viewerDirty = true;
                    node.focused = false;
                });

                return topNode;
            })
            .catch((err: Error) => {
                throw err;
            });

        promises.push(clonedTree);
    });

    return Promise.all(promises).then((treeNodes: TreeNode[]) => {
        return new TreeNodeList(treeNodes);
    });
}

/**
 * Merges a list of molecules into a single molecule.
 *
 * @param  {TreeNodeList} nodeList               The list of molecules to merge.
 * @param  {string}       [newName="mergedMol"]  The name of the new merged
 *                                               molecule.
 * @returns {Promise<TreeNode>}  A promise that resolves the merged molecule.
 */
export function mergeTreeNodes(
    nodeList: TreeNodeList,
    newName = "mergedMol"
): Promise<TreeNode> {
    // Start by deepcloning all the molecules
    return Promise.all(
        nodeList.map((node: TreeNode) => {
            return treeNodeDeepClone(node, true); // reid
        })
    )
        .then((nodeList: TreeNode[]) => {
            const merged = nodeList[0];
            for (let i = 1; i < nodeList.length; i++) {
                merged.mergeInto(nodeList[i]);
            }
            merged.title = newName;
            return merged;
        })
        .catch((err: Error) => {
            throw err;
        });

    // const treeNode = nodeList.get(0);

    // // Keep going through the nodes of each container and merge them into the
    // // first container.
    // for (let i = 1; i < nodeList.length; i++) {
    //     const treeNode = nodeList.get(i);

    //     // Get the terminal nodes
    //     const terminalNodes = new TreeNodeList([treeNode]).filters
    //         .onlyTerminal;

    //     // Get ancestry of each terminal node
    //     terminalNodes.forEach((terminalNode) => {
    //         const ancestry = terminalNode.getAncestry(
    //             new TreeNodeList([treeNode])
    //         );

    //         // Remove first one, which is the root node
    //         ancestry.shift();

    //         let treeNodePointer = treeNode;
    //         const mergedMolNodesTitles = treeNodePointer.nodes?.map(
    //             (node: TreeNode) => node.title
    //         ) as string[];

    //         while (
    //             mergedMolNodesTitles?.indexOf(ancestry.get(0).title) !== -1
    //         ) {
    //             if (!mergedTreeNodePointer.nodes) {
    //                 // When does this happen?
    //                 break;
    //             }

    //             // Update the pointer
    //             mergedTreeNodePointer =
    //                 mergedTreeNodePointer.nodes.find(
    //                     (node: TreeNode) => node.title === ancestry.get(0).title
    //                 ) as TreeNode;

    //             // Remove the first node from the ancestry
    //             ancestry.shift();
    //         }

    //         // You've reached the place where the node should be added. First,
    //         // update its parentId.
    //         const nodeToAdd = ancestry.get(0);
    //         nodeToAdd.parentId = mergedTreeNodePointer.id;

    //         // And add it
    //         mergedTreeNodePointer.nodes?.push(nodeToAdd);
    //     });
    // }

    // mergedTreeNode.title = newName;

    // return mergedTreeNode;
}

/**
 * Retrieves unique residue names and IDs from all currently visible terminal
 * molecular models.
 *
 * @returns {{ names: string[], ids: number[] }} An object containing an
 *  alphabetically sorted array of unique residue names and a numerically
 *  sorted array of unique residue IDs.
 */
export function getUniqueResiduesFromVisibleMolecules(): {
    names: string[];
    ids: number[];
} {
    const allMolecules: TreeNodeList = getMoleculesFromStore();
    const visibleTerminalNodes: TreeNodeList =
        allMolecules.filters.onlyTerminal.filters.keepVisible();
    const allResidueNames = new Set<string>();
    const allResidueIds = new Set<number>();
    visibleTerminalNodes.forEach((node: TreeNode) => {
        if (node.model) {
            const parser: EasyParserParent = makeEasyParser(node.model);
            const { names: nodeResNames, ids: nodeResIds } =
                parser.getUniqueResidues();
            nodeResNames.forEach((name) => allResidueNames.add(name));
            nodeResIds.forEach((id) => allResidueIds.add(id));
        }
    });
    return {
        names: Array.from(allResidueNames).sort(),
        ids: Array.from(allResidueIds).sort((a, b) => a - b),
    };
}


/**
 * Organizes a flat list of terminal TreeNode objects into a standard hierarchical structure.
 *
 * @param {TreeNode[]} treeNodes The flat list of terminal nodes to organize.
 * @param {string} rootTitle The title for the root node of the new hierarchy.
 * @param {boolean} [divideCompoundsByChain=true] Whether to group compounds by chain.
 * @returns {TreeNode} The new root TreeNode containing the organized hierarchy.
 */
export function organizeNodesIntoHierarchy(
    treeNodes: TreeNode[],
    rootTitle: string,
    divideCompoundsByChain = true
): TreeNode {
    // Divide the nodes into categories for all possible types.
    const categories: { [key: string]: any } = {};
    for (const treeNode of treeNodes) {
        if (treeNode.type) {
            let categoryName =
                treeNode.type.charAt(0).toUpperCase() + treeNode.type.slice(1);
            if (
                [
                    TreeNodeType.Compound,
                    TreeNodeType.Metal,
                    TreeNodeType.Lipid,
                    TreeNodeType.Ions,
                ].includes(treeNode.type)
            ) {
                categoryName += "s";
            }
            if (!categories[categoryName]) {
                categories[categoryName] = [];
            }
            categories[categoryName].push(treeNode);
        }
    }
    // Further divide the Compounds into chains.
    if (categories["Compounds"]) {
        if (divideCompoundsByChain) {
            const compounds = categories["Compounds"];
            const newCompounds: { [key: string]: TreeNode[] } = {};
            // Fallback list of chains to assign if a compound has NO chain defined.
            const availableChainsOrig = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
            for (const treeNode of compounds) {
                const chain = getChain(treeNode, availableChainsOrig);
                if (!newCompounds[chain]) {
                    newCompounds[chain] = [];
                }
                newCompounds[chain].push(treeNode);
            }
            categories["Compounds"] = newCompounds;
        } else {
            categories["Compounds"] = {
                A: categories["Compounds"],
            };
        }
    }
    // Create the root node
    const rootNode = new TreeNode({
        title: rootTitle,
        treeExpanded: false,
        visible: true,
        selected: SelectedType.False,
        focused: false,
        viewerDirty: true,
        nodes: new TreeNodeList([]),
    });
    const categoryOrder = [
        "Protein",
        "Nucleic",
        "Compounds",
        "Metals",
        "Lipids",
        "Ions",
        "Solvent",
    ];
    const titleToTypeMap: Map<string, TreeNodeType> = new Map([
        ["Protein", TreeNodeType.Protein],
        ["Nucleic", TreeNodeType.Nucleic],
        ["Compounds", TreeNodeType.Compound],
        ["Metals", TreeNodeType.Metal],
        ["Lipids", TreeNodeType.Lipid],
        ["Ions", TreeNodeType.Ions],
        ["Solvent", TreeNodeType.Solvent],
    ]);
    for (const title of categoryOrder) {
        const type = titleToTypeMap.get(title);
        if (!type || !categories[title] || categories[title].length === 0) {
            continue;
        }
        const categoryNode = new TreeNode({
            title: title,
            treeExpanded: false,
            visible: true,
            selected: SelectedType.False,
            focused: false,
            viewerDirty: true,
            type: type,
            nodes: new TreeNodeList([]),
        });
        rootNode.nodes?.push(categoryNode);
        if (title === "Compounds") {
            for (const chain of Object.keys(categories[title])) {
                const chainNode = new TreeNode({
                    title: chain as string,
                    treeExpanded: false,
                    visible: true,
                    selected: SelectedType.False,
                    focused: false,
                    viewerDirty: true,
                    type: type,
                    nodes: new TreeNodeList(categories[title][chain]),
                });
                categoryNode.nodes?.push(chainNode);
            }
        } else {
            // Logic for other categories that are grouped by chain directly
            const availableChainsOrig = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
            for (const treeNode of categories[title]) {
                const chain = getChain(treeNode, availableChainsOrig);
                treeNode.title = chain as string;
                categoryNode.nodes?.push(treeNode);
            }
        }
    }
    return rootNode;
}

/**
 * Gets the chain of a given tree node.
 *
 * @param {TreeNode} treeNode The tree node.
 * @param {string[]} availableChains The available chains to use if the node has no chain.
 * @returns {string} The chain.
 */
function getChain(treeNode: TreeNode, availableChains: string[]): string {
    let chain: string | undefined = undefined;
    if (!treeNode.model) {
        // If there's no model, use first available chain from the fallback list.
        chain = availableChains.shift();
    } else {
        const firstAtom = makeEasyParser(treeNode.model).getAtom(0);
        if (!firstAtom) {
            // If there are no atoms in the model, use first available chain.
            chain = availableChains.shift();
        } else {
            const firstAtomChain = firstAtom.chain;
            if (firstAtomChain === "" || firstAtomChain === undefined) {
                // If the first atom has no chain, use first available chain.
                chain = availableChains.shift();
            } else {
                // Use the actual chain from the molecule.
                chain = firstAtomChain;
            }
        }
    }
    return chain || "A"; // Fallback to 'A' if everything else fails
}

/**
 * Given a list of tree nodes, flattens them to terminals and organizes them into a standard hierarchy.
 *
 * @param {TreeNode[]} treeNodes The tree nodes to organize.
 * @param {string} rootNodeTitle The title for the root node of the new hierarchy.
 * @param {boolean} [divideCompoundsByChain=true] Whether to divide compounds by chain.
 * @returns {TreeNode} The root tree node of the loaded tree.
 */
export function loadHierarchicallyFromTreeNodes(
    treeNodes: TreeNode[],
    rootNodeTitle: string,
    divideCompoundsByChain = true
): TreeNode {
    // Consider only the terminal nodes
    const allTreeNodes: TreeNode[] = [];
    for (const treeNode of treeNodes) {
        if (treeNode.nodes) {
            const terminalNodes = treeNode.nodes.terminals;
            const nodes = terminalNodes._nodes;
            if (nodes.length === 1) {
                nodes[0].title = treeNode.title;
            } else {
                for (let i = 0; i < nodes.length; i++) {
                    nodes[i].title = `${treeNode.title}:${i + 1}`;
                }
            }
            allTreeNodes.push(...nodes);
        } else {
            allTreeNodes.push(treeNode);
        }
    }
    return organizeNodesIntoHierarchy(
        allTreeNodes,
        rootNodeTitle,
        divideCompoundsByChain
    );
}

/**
 * Toggles visibility of a list of nodes. If too many nodes are to become
 * visible, it prompts the user for confirmation.
 *
 * @param {TreeNodeList} nodesToToggle The list of nodes whose visibility should
 *      be toggled.
 */
export async function toggleVisibilityWithConfirmation(
    nodesToToggle: TreeNodeList
): Promise<void> {
    if (nodesToToggle.length === 0) {
        return;
    }

    // Determine the new visibility state. If any node is visible, all will be hidden.
    const anyVisible = nodesToToggle.some((n) => n.visible);
    const newVisibleState = !anyVisible;

    const performToggle = () => {
        nodesToToggle.forEach((nodeToToggle: TreeNode) => {
            nodeToToggle.visible = newVisibleState; // This setter will propagate to children.
            nodeToToggle.viewerDirty = true;
        });
    };

    if (newVisibleState) {
        // Only prompt when making nodes visible
        const allAffectedTerminals = new TreeNodeList();
        nodesToToggle.forEach((node) => {
            if (node.model) {
                // is terminal
                allAffectedTerminals.push(node);
            } else if (node.nodes) {
                // is container
                allAffectedTerminals.extend(node.nodes.terminals);
            }
        });

        const uniqueTerminals = allAffectedTerminals.filters.onlyUnique;
        const countToMakeVisible = uniqueTerminals.filter(
            (n) => !n.visible
        ).length;
        const visibilityThreshold = await getSetting("initialCompoundsVisible");
        if (countToMakeVisible > visibilityThreshold) {
            const resp = await api.messages.popupYesNo(
                `You are about to make ${countToMakeVisible} molecules visible at once. This may impact performance. Do you want to continue?`,
                "Performance Warning",
                "Yes, Continue",
                "Cancel"
            );
            if (resp === YesNo.Yes) {
                performToggle();
            }
            // If No or Cancel, do nothing.
        } else {
            performToggle();
        }
    } else {
        // Hiding nodes doesn't need a warning
        performToggle();
    }
}