# 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
                an['steps'][an_cmp_step_from_an[0]].get('source_url')

    return dos


def merge_promulgation_steps(senat_step, an_step):
    step = {**senat_step}
    senat_url = senat_step.get('source_url')
    if not senat_url or 'jo_pdf' in senat_url and an_step.get('source_url'):
        step['source_url'] = an_step['source_url']
    if not senat_step.get('date') and an_step.get('date'):
        step['date'] = an_step['date']
    return step


def merge_senat_with_an(senat, an):
    """Takes a senat dosleg and an AN dosleg and returns a merged version"""
    dos = copy.deepcopy(senat)

    dos['url_dossier_assemblee'] = an['url_dossier_assemblee']
    dos['assemblee_slug'] = an['assemblee_slug']
    dos['assemblee_id'] = an['assemblee_id']
    dos['assemblee_legislature'] = an['assemblee_legislature']

    if ('url_jo' not in dos or 'jo_pdf' in dos['url_jo']) and 'url_jo' in an:
        dos['url_jo'] = an['url_jo']
        if not dos.get('end') and an.get('end'):
            dos['end'] = an['end']

    dos['steps'] = []

# 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

                an['steps'][an_cmp_step_from_an[0]].get('source_url')

    return dos


def merge_promulgation_steps(senat_step, an_step):
    step = {**senat_step}
    senat_url = senat_step.get('source_url')
    if not senat_url or 'jo_pdf' in senat_url and an_step.get('source_url'):
        step['source_url'] = an_step['source_url']
    if not senat_step.get('date') and an_step.get('date'):
        step['date'] = an_step['date']
    return step


def merge_senat_with_an(senat, an):
    """Takes a senat dosleg and an AN dosleg and returns a merged version"""
    dos = copy.deepcopy(senat)

    dos['url_dossier_assemblee'] = an['url_dossier_assemblee']
    dos['assemblee_slug'] = an['assemblee_slug']
    dos['assemblee_id'] = an['assemblee_id']
    dos['assemblee_legislature'] = an['assemblee_legislature']

    if ('url_jo' not in dos or 'jo_pdf' in dos['url_jo']) and 'url_jo' in an:
        dos['url_jo'] = an['url_jo']
        if not dos.get('end') and an.get('end'):
            dos['end'] = an['end']

    dos['steps'] = []

    an['steps'] = [s for s in an['steps'] if not should_ignore_commission_text(s, an)]