# Path: tlfp/tools/detect_anomalies.py
# def find_anomalies(dossiers, verbose=True):
#     anomalies = 0
#     for dos in dossiers:
#         prev_step = ''
#         for step in dos['steps']:
#             step_name = ' • '.join((x for x in (step.get('stage'), step.get('institution'), step.get('step','')) if x))
#             if procedure.get(prev_step, {}).get(step_name, False) is False:
#                 if verbose:
#                     print('INCORRECT', prev_step, '->', step_name)
#                     print(dos.get('url_dossier_senat'), '|',dos.get('url_dossier_assemblee'))
#                     print()
#                 anomalies += 1
# 
#             #print(step_name, '      \t\t\t\t===>>', procedure.get(prev_step, {}).get(step_name))
# 
#             prev_step = step_name
# 
#     if verbose and anomalies:
#         print(anomalies, 'anomalies (', len(dossiers), 'doslegs)')
#     return anomalies
# 
# Path: tlfp/tools/_step_logic.py
# def should_ignore_commission_text(step, dos):
#     return step.get('step') == 'commission' and (
#         step.get('stage') == 'l. définitive' or (
#         use_old_procedure(step, dos) and step['institution'] in ('senat', 'assemblee'))
#     )
# 
# def use_old_procedure(step, dos=None):
#     if dos and dos.get('use_old_procedure'):
#         return True
#     return (step.get("enddate", step.get("date", "9999-99-99")) or "9999-99-99") < "2009-03-01"


import copy
from tlfp.tools.detect_anomalies import find_anomalies
from tlfp.tools._step_logic import should_ignore_commission_text, use_old_procedure
from lawfactory_utils.urls import validate_link_CC_decision

        # Only keep first empty consecutive step as next one to come
        if not dos.get('url_jo'):
            if step.get('source_url'):
                empty_last_step = False
            elif empty_last_step:
                break
            else:
                if not step.get('step') == 'commission' and use_old_procedure(step, dos):
                    empty_last_step = True

        if len(steps_to_add) == 0:
            steps_to_add = [step]

        dos['steps'] += steps_to_add

    dos = fix_an_cmp_step_url(dos, an)

    # ## SANITY CHECKS ## #

    # detect AN leftovers we didn't merge
    if len(an['steps']) > i + an_offset + 1:
        leftovers = an['steps'][an_index:]
        if not dos.get('url_jo'):
            dos['steps'] += leftovers
        else:
            print('[warning] [merge] some AN steps didn\'t get merged ( the last', len(leftovers), ')')

    # compare the number of anomalies before and after merging
    if find_anomalies([senat], verbose=False) < find_anomalies([dos], verbose=False) or \

# Path: tlfp/tools/detect_anomalies.py
# def find_anomalies(dossiers, verbose=True):
#     anomalies = 0
#     for dos in dossiers:
#         prev_step = ''
#         for step in dos['steps']:
#             step_name = ' • '.join((x for x in (step.get('stage'), step.get('institution'), step.get('step','')) if x))
#             if procedure.get(prev_step, {}).get(step_name, False) is False:
#                 if verbose:
#                     print('INCORRECT', prev_step, '->', step_name)
#                     print(dos.get('url_dossier_senat'), '|',dos.get('url_dossier_assemblee'))
#                     print()
#                 anomalies += 1
# 
#             #print(step_name, '      \t\t\t\t===>>', procedure.get(prev_step, {}).get(step_name))
# 
#             prev_step = step_name
# 
#     if verbose and anomalies:
#         print(anomalies, 'anomalies (', len(dossiers), 'doslegs)')
#     return anomalies
# 
# Path: tlfp/tools/_step_logic.py
# def should_ignore_commission_text(step, dos):
#     return step.get('step') == 'commission' and (
#         step.get('stage') == 'l. définitive' or (
#         use_old_procedure(step, dos) and step['institution'] in ('senat', 'assemblee'))
#     )
# 
# def use_old_procedure(step, dos=None):
#     if dos and dos.get('use_old_procedure'):
#         return True
#     return (step.get("enddate", step.get("date", "9999-99-99")) or "9999-99-99") < "2009-03-01"


# Path: tlfp/merge.py
import copy
from tlfp.tools.detect_anomalies import find_anomalies
from tlfp.tools._step_logic import should_ignore_commission_text, use_old_procedure
from lawfactory_utils.urls import validate_link_CC_decision


        # Only keep first empty consecutive step as next one to come
        if not dos.get('url_jo'):
            if step.get('source_url'):
                empty_last_step = False
            elif empty_last_step:
                break
            else:
                if not step.get('step') == 'commission' and use_old_procedure(step, dos):
                    empty_last_step = True

        if len(steps_to_add) == 0:
            steps_to_add = [step]

        dos['steps'] += steps_to_add

    dos = fix_an_cmp_step_url(dos, an)

    # ## SANITY CHECKS ## #

    # detect AN leftovers we didn't merge
    if len(an['steps']) > i + an_offset + 1:
        leftovers = an['steps'][an_index:]
        if not dos.get('url_jo'):
            dos['steps'] += leftovers
        else:
            print('[warning] [merge] some AN steps didn\'t get merged ( the last', len(leftovers), ')')

    # compare the number of anomalies before and after merging
    if find_anomalies([senat], verbose=False) < find_anomalies([dos], verbose=False) or \

       find_anomalies([an], verbose=False) < find_anomalies([dos], verbose=False):