import pandas as pd

def expansion(cur_df, prv_trj, prv_df, cur_news, dt, merg_exp, spl_exp):
    """
    This function calculates the normalized expansion between two clusters.

    Parameters
    ----------
    cur_df : geopandas.GeoDataFrame
        Current dataframe.
    prv_df : geopandas.GeoDataFrame
        Previous dataframe.
    dt : float
        Time difference in minutes.
    merg_exp : bool
        Whether to consider merges in expansion calculation.
    spl_exp : bool
        Whether to consider splits in expansion calculation.
    
    Returns
    -------
    expansions : list
        List of normalized expansion values for each cluster.
    """
    expansions = []
    expansion_idx = []
    #dt from minutes to seconds:
    dt = dt * 60
    for i,row in cur_df.iterrows():
        if isinstance(row['merge_idx'], list) and merg_exp: # Merge expansion correction
            #Get the size of the current and previous clusters:
            p=prv_df.loc[row['merge_idx']]['size'].sum() 
            #Get the size of the current cluster:
            c=row['size']
            expansion = ((1 / ((c + p) / 2)) * ((c - p) / dt)) * 1e6
            expansions.append(expansion)
            expansion_idx.append(i)
        if not pd.isna(row['split_cr_idx']) and spl_exp: # Split expansion correction
            # Get the size of the current cluster:
            c = row['size']
            # Get the size of the previous cluster:
            p = prv_df.loc[row['past_idx']]['size']
            # Get the size of the new cluster generated by the split:
            c_new = cur_news.loc[row['split_cr_idx']]['size'].sum()
            # C = área corrente (current size)
            # P = área no tempo anterior (previous size)
            # C_new = área do novo sistema gerado pelo split (não pertence ao sistema que está sendo corrigido)
            expansion = ((1 / ((c + (p - c_new)) / 2)) * ((c - (p - c_new)) / dt)) * 1e6
            expansions.append(expansion)
            expansion_idx.append(i)
        else:
            p=prv_df.loc[row['past_idx']]['size']
            c=row['size']
            expansion = ((1 / ((c + p) / 2)) * ((c - p) / dt)) * 1e6
            expansions.append(expansion)
            expansion_idx.append(i)

    return expansion_idx, expansions