# Path: importer/importer/utils/parsing.py

# 
# Path: importer/importer/utils/collections.py
# def find_first(needles, haystack):
#     for needle in needles:
#         if needle in haystack:
#             return needle
# 
# Path: importer/importer/utils/strings.py
# def strip_links(text):
#     return link_expression.sub('\\1', text)
# 
# def find_numbers(text):
#     return [
#         int(''.join(filter(str.isdigit, s)))
#         for s in number_expression.findall(text)
#         if s.strip()
#     ]
# 
# def is_from_string(text):
#     return bool(from_expression.match(text))
# 
# def is_up_to_string(text):
#     return bool(up_to_expression.match(text))


from operator import itemgetter
from funcy import flatten, project, first
from datetime import datetime, time, timedelta
from isodate import parse_date
from .utils.parsing import maybe_parse_date, maybe_parse_time
from .utils.collections import find_first
from .utils.strings import (
    strip_links,
    find_numbers,
    is_from_string,
    is_up_to_string,
)
# main objects

def transform_event(kudago_event, parent_id, children_count):
    tags = kudago_event['tags']
    categories = kudago_event['categories']
    place = kudago_event['place']

    kind = find_first(('festival', 'exhibition', 'theater'), categories)
    dates = filter(is_date_finite, kudago_event['dates'])
    if kind not in ('festival', 'exhibition'):
        dates = flatten(map(split_date, dates))
    dates = list(sorted(map(transform_date, dates), key=itemgetter('start')))

    participants = [
        {'role': p['role']['slug'], 'agent': p['agent']['id']}
        for p in kudago_event['participants']
    ]

    return {
        '_id': kudago_event['id'],
        '_type': 'event',

        'kind': kind,
        'is_for_kids': 'kids' in categories,
        'is_premiere': 'премьера' in tags,

        'name': kudago_event['short_title'],
        'full_name': kudago_event['title'],
        'tagline': kudago_event['tagline'],
        'lead': strip_links(kudago_event['description']),

# Path: importer/importer/utils/parsing.py

# 
# Path: importer/importer/utils/collections.py
# def find_first(needles, haystack):
#     for needle in needles:
#         if needle in haystack:
#             return needle
# 
# Path: importer/importer/utils/strings.py
# def strip_links(text):
#     return link_expression.sub('\\1', text)
# 
# def find_numbers(text):
#     return [
#         int(''.join(filter(str.isdigit, s)))
#         for s in number_expression.findall(text)
#         if s.strip()
#     ]
# 
# def is_from_string(text):
#     return bool(from_expression.match(text))
# 
# def is_up_to_string(text):
#     return bool(up_to_expression.match(text))


# Path: importer/importer/transform.py
from operator import itemgetter
from funcy import flatten, project, first
from datetime import datetime, time, timedelta
from isodate import parse_date
from .utils.parsing import maybe_parse_date, maybe_parse_time
from .utils.collections import find_first
from .utils.strings import (
    strip_links,
    find_numbers,
    is_from_string,
    is_up_to_string,
)

# main objects

def transform_event(kudago_event, parent_id, children_count):
    tags = kudago_event['tags']
    categories = kudago_event['categories']
    place = kudago_event['place']

    kind = find_first(('festival', 'exhibition', 'theater'), categories)
    dates = filter(is_date_finite, kudago_event['dates'])
    if kind not in ('festival', 'exhibition'):
        dates = flatten(map(split_date, dates))
    dates = list(sorted(map(transform_date, dates), key=itemgetter('start')))

    participants = [
        {'role': p['role']['slug'], 'agent': p['agent']['id']}
        for p in kudago_event['participants']
    ]

    return {
        '_id': kudago_event['id'],
        '_type': 'event',

        'kind': kind,
        'is_for_kids': 'kids' in categories,
        'is_premiere': 'премьера' in tags,

        'name': kudago_event['short_title'],
        'full_name': kudago_event['title'],
        'tagline': kudago_event['tagline'],
        'lead': strip_links(kudago_event['description']),

        'description': strip_links(kudago_event['body_text']),