# Path: imap_tools/query.py
# class AND(LogicOperator):
#     """When multiple keys are specified, the result is the intersection of all the messages that match those keys."""
# 
#     def combine_params(self) -> str:
#         return self.prefix_join('', itertools.chain(self.converted_strings, self.converted_params))
# 
# class OR(LogicOperator):
#     """OR <search-key1> <search-key2> Messages that match either search key."""
# 
#     def combine_params(self) -> str:
#         return self.prefix_join('OR ', itertools.chain(self.converted_strings, self.converted_params))
# 
# class NOT(LogicOperator):
#     """NOT <search-key> Messages that do not match the specified search key."""
# 
#     def combine_params(self) -> str:
#         return 'NOT {}'.format(self.prefix_join('', itertools.chain(self.converted_strings, self.converted_params)))
# 
# A = AND
# 
# H = Header
# 
# U = UidRange


import datetime as dt
from imap_tools import AND, OR, NOT, A, H, U
# Infix notation (natural to humans)
    NOT ((FROM='11' OR TO="22" OR TEXT="33") AND CC="44" AND BCC="55")
# Prefix notation (Polish notation, IMAP version)
    NOT (((OR OR FROM "11" TO "22" TEXT "33") CC "44" BCC "55"))
# Python query builder
    NOT(AND(OR(from_='11', to='22', text='33'), cc='44', bcc='55'))

# python to prefix notation steps:
1. OR(1=11, 2=22, 3=33) ->
    "(OR OR FROM "11" TO "22" TEXT "33")"
2. AND("(OR OR FROM "11" TO "22" TEXT "33")", cc='44', bcc='55') ->
    "AND(OR(from_='11', to='22', text='33'), cc='44', bcc='55')"
3. NOT("AND(OR(from_='11', to='22', text='33'), cc='44', bcc='55')") ->
    "NOT (((OR OR FROM "1" TO "22" TEXT "33") CC "44" BCC "55"))"
"""


# date in the date list (date=date1 OR date=date3 OR date=date2)
q1 = OR(date=[dt.date(2019, 10, 1), dt.date(2019, 10, 10), dt.date(2019, 10, 15)])
# '(OR OR ON 1-Oct-2019 ON 10-Oct-2019 ON 15-Oct-2019)'

# date not in the date list (NOT(date=date1 OR date=date3 OR date=date2))
q2 = NOT(OR(date=[dt.date(2019, 10, 1), dt.date(2019, 10, 10), dt.date(2019, 10, 15)]))
# 'NOT ((OR OR ON 1-Oct-2019 ON 10-Oct-2019 ON 15-Oct-2019))'

# subject contains "hello" AND date greater than or equal dt.date(2019, 10, 10)
q3 = A(subject='hello', date_gte=dt.date(2019, 10, 10))
# '(SUBJECT "hello" SINCE 10-Oct-2019)'

# from contains one of the address parts

# Path: imap_tools/query.py
# class AND(LogicOperator):
#     """When multiple keys are specified, the result is the intersection of all the messages that match those keys."""
# 
#     def combine_params(self) -> str:
#         return self.prefix_join('', itertools.chain(self.converted_strings, self.converted_params))
# 
# class OR(LogicOperator):
#     """OR <search-key1> <search-key2> Messages that match either search key."""
# 
#     def combine_params(self) -> str:
#         return self.prefix_join('OR ', itertools.chain(self.converted_strings, self.converted_params))
# 
# class NOT(LogicOperator):
#     """NOT <search-key> Messages that do not match the specified search key."""
# 
#     def combine_params(self) -> str:
#         return 'NOT {}'.format(self.prefix_join('', itertools.chain(self.converted_strings, self.converted_params)))
# 
# A = AND
# 
# H = Header
# 
# U = UidRange


# Path: examples/search.py
import datetime as dt
from imap_tools import AND, OR, NOT, A, H, U

# Infix notation (natural to humans)
    NOT ((FROM='11' OR TO="22" OR TEXT="33") AND CC="44" AND BCC="55")
# Prefix notation (Polish notation, IMAP version)
    NOT (((OR OR FROM "11" TO "22" TEXT "33") CC "44" BCC "55"))
# Python query builder
    NOT(AND(OR(from_='11', to='22', text='33'), cc='44', bcc='55'))

# python to prefix notation steps:
1. OR(1=11, 2=22, 3=33) ->
    "(OR OR FROM "11" TO "22" TEXT "33")"
2. AND("(OR OR FROM "11" TO "22" TEXT "33")", cc='44', bcc='55') ->
    "AND(OR(from_='11', to='22', text='33'), cc='44', bcc='55')"
3. NOT("AND(OR(from_='11', to='22', text='33'), cc='44', bcc='55')") ->
    "NOT (((OR OR FROM "1" TO "22" TEXT "33") CC "44" BCC "55"))"
"""


# date in the date list (date=date1 OR date=date3 OR date=date2)
q1 = OR(date=[dt.date(2019, 10, 1), dt.date(2019, 10, 10), dt.date(2019, 10, 15)])
# '(OR OR ON 1-Oct-2019 ON 10-Oct-2019 ON 15-Oct-2019)'

# date not in the date list (NOT(date=date1 OR date=date3 OR date=date2))
q2 = NOT(OR(date=[dt.date(2019, 10, 1), dt.date(2019, 10, 10), dt.date(2019, 10, 15)]))
# 'NOT ((OR OR ON 1-Oct-2019 ON 10-Oct-2019 ON 15-Oct-2019))'

# subject contains "hello" AND date greater than or equal dt.date(2019, 10, 10)
q3 = A(subject='hello', date_gte=dt.date(2019, 10, 10))
# '(SUBJECT "hello" SINCE 10-Oct-2019)'

# from contains one of the address parts

q4 = OR(from_=["@spam.ru", "@tricky-spam.ru"])