# 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
                    elif step.get('source_url'):
                        if step['source_url'] != an_step['source_url']:
                            step['cmp_commission_other_url'] = an_step['source_url']
                    else:
                        step['source_url'] = an_step['source_url']
            if dos.get('url_jo') and not step.get('source_url'):
                print('[warning] [merge] empty CMP steps announced in Senate in a promulgated text but missing in the AN-side', dos['url_dossier_assemblee'])
                continue

        # Choose best CC url available
        if step.get('stage') == 'constitutionnalité' and not validate_link_CC_decision(step.get('source_url')):
            cc_an = [s.get('source_url') for s in an['steps'] if s.get('stage') == 'constitutionnalité']
            if validate_link_CC_decision(cc_an[0]):
                step['source_url'] = cc_an[0]

        # Choose best JO url available
        if step.get('stage') == 'promulgation':
            an_step_promulgation = [(i, s) for i, s in enumerate(an['steps']) if s.get('stage') == 'promulgation']
            if an_step_promulgation:
                an_index, an_step = an_step_promulgation[0]
                an_offset = an_index - i
                step = merge_promulgation_steps(step, an_step)

        # 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:

# 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

                    elif step.get('source_url'):
                        if step['source_url'] != an_step['source_url']:
                            step['cmp_commission_other_url'] = an_step['source_url']
                    else:
                        step['source_url'] = an_step['source_url']
            if dos.get('url_jo') and not step.get('source_url'):
                print('[warning] [merge] empty CMP steps announced in Senate in a promulgated text but missing in the AN-side', dos['url_dossier_assemblee'])
                continue

        # Choose best CC url available
        if step.get('stage') == 'constitutionnalité' and not validate_link_CC_decision(step.get('source_url')):
            cc_an = [s.get('source_url') for s in an['steps'] if s.get('stage') == 'constitutionnalité']
            if validate_link_CC_decision(cc_an[0]):
                step['source_url'] = cc_an[0]

        # Choose best JO url available
        if step.get('stage') == 'promulgation':
            an_step_promulgation = [(i, s) for i, s in enumerate(an['steps']) if s.get('stage') == 'promulgation']
            if an_step_promulgation:
                an_index, an_step = an_step_promulgation[0]
                an_offset = an_index - i
                step = merge_promulgation_steps(step, an_step)

        # 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):