Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / sieve / src / toSieve / toSieveTree.ts
blob5bedd661ecabe7e5e4177bb8a763b6530692e73a
1 import type { SimpleObject } from '@proton/components/containers/filters/interfaces';
3 import { V1 } from '../constants';
4 import type { SIEVE_VERSION } from '../interface';
5 import {
6     buildBasicTree,
7     buildCondition,
8     buildFileInto,
9     buildMark,
10     buildRedirects,
11     buildVacation,
12     validateSimpleRepresentation,
13 } from './toSieveTree.helpers';
15 /**
16  * Transforms a simple representation to a filter tree.
17  */
18 export const toSieveTree = (simple: SimpleObject, version: SIEVE_VERSION = V1) => {
19     try {
20         validateSimpleRepresentation(simple);
22         const condition = buildCondition(simple.Conditions);
23         const fileInto = buildFileInto(simple.Actions.FileInto);
24         const mark = buildMark(simple.Actions.Mark);
25         const vacation = buildVacation(simple.Actions.Vacation, version);
26         const redirects = buildRedirects(simple.Actions.Redirects);
28         return buildBasicTree(
29             {
30                 type: simple.Operator.value,
31                 requires: vacation.blocks.length ? ['vacation'] : [],
32                 tests: condition.tests,
33                 comparators: condition.comparators,
34                 dollarNeeded: condition.dollarNeeded || fileInto.dollarNeeded || vacation.dollarNeeded,
35                 thens: [...fileInto.blocks, ...mark.blocks, ...vacation.blocks, ...redirects.blocks],
36             },
37             version
38         );
39     } catch (exception) {
40         throw exception;
41     }