# Path: pyranges/methods/join.py
# def _both_dfs(scdf, ocdf, how=False):
# 
#     _self_indexes, _other_indexes = _both_indexes(scdf, ocdf, how)
# 
#     if how in ["outer", "left", "right"]:
# 
#         sh = null_types(scdf.head(1))
#         oh = null_types(ocdf.head(1))
#         sh.index = [-1]
#         oh.index = [-1]
# 
#         scdf = scdf.append(sh)
#         ocdf = ocdf.append(oh)
# 
#         scdf = scdf.reindex(_self_indexes)
#         ocdf = ocdf.reindex(_other_indexes)
# 
#         if "Strand" in scdf and "Strand" in ocdf:
# 
#             if how == "left":
#                 x = ocdf.index.values == -1
#                 ocdf.loc[x, "Strand"] = scdf[x].Strand.values
#             elif how == "right":
#                 x = scdf.index.values == -1
#                 scdf.loc[x, "Strand"] = ocdf[x].Strand.values
# 
#     else:
#         scdf = scdf.reindex(_self_indexes)
#         ocdf = ocdf.reindex(_other_indexes)
# 
#     return scdf, ocdf
# 
# Path: pyranges/methods/sort.py
# def sort_one_by_one(d, col1, col2):
#     """
#     Equivalent to pd.sort_values(by=[col1, col2]), but faster.
#     """
# 
#     d = d.sort_values(by=[col2])
#     return d.sort_values(by=[col1], kind='mergesort')


import pandas as pd
from .join import _both_dfs
from .sort import sort_one_by_one
from sorted_nearest import (nearest_previous_nonoverlapping,
                            nearest_next_nonoverlapping,
                            nearest_nonoverlapping)
    return r_idx, dist


def _nearest(scdf, ocdf, **kwargs):

    if scdf.empty or ocdf.empty:
        return None

    overlap = kwargs["overlap"]
    how = kwargs["how"]
    suffix = kwargs["suffix"]

    if how == "upstream":
        strand = scdf.Strand.iloc[0]
        how = {"+": "previous", "-": "next"}[strand]
    elif how == "downstream":
        strand = scdf.Strand.iloc[0]
        how = {"+": "next", "-": "previous"}[strand]

    ocdf = ocdf.reset_index(drop=True)

    if overlap:
        nearest_df, df_to_find_nearest_in = _overlapping_for_nearest(
            scdf, ocdf, suffix)
    else:
        df_to_find_nearest_in = scdf

    if not df_to_find_nearest_in.empty:
        df_to_find_nearest_in = sort_one_by_one(df_to_find_nearest_in, "Start",
                                                "End")

# Path: pyranges/methods/join.py
# def _both_dfs(scdf, ocdf, how=False):
# 
#     _self_indexes, _other_indexes = _both_indexes(scdf, ocdf, how)
# 
#     if how in ["outer", "left", "right"]:
# 
#         sh = null_types(scdf.head(1))
#         oh = null_types(ocdf.head(1))
#         sh.index = [-1]
#         oh.index = [-1]
# 
#         scdf = scdf.append(sh)
#         ocdf = ocdf.append(oh)
# 
#         scdf = scdf.reindex(_self_indexes)
#         ocdf = ocdf.reindex(_other_indexes)
# 
#         if "Strand" in scdf and "Strand" in ocdf:
# 
#             if how == "left":
#                 x = ocdf.index.values == -1
#                 ocdf.loc[x, "Strand"] = scdf[x].Strand.values
#             elif how == "right":
#                 x = scdf.index.values == -1
#                 scdf.loc[x, "Strand"] = ocdf[x].Strand.values
# 
#     else:
#         scdf = scdf.reindex(_self_indexes)
#         ocdf = ocdf.reindex(_other_indexes)
# 
#     return scdf, ocdf
# 
# Path: pyranges/methods/sort.py
# def sort_one_by_one(d, col1, col2):
#     """
#     Equivalent to pd.sort_values(by=[col1, col2]), but faster.
#     """
# 
#     d = d.sort_values(by=[col2])
#     return d.sort_values(by=[col1], kind='mergesort')


# Path: pyranges/methods/nearest.py
import pandas as pd
from .join import _both_dfs
from .sort import sort_one_by_one
from sorted_nearest import (nearest_previous_nonoverlapping,
                            nearest_next_nonoverlapping,
                            nearest_nonoverlapping)

    return r_idx, dist


def _nearest(scdf, ocdf, **kwargs):

    if scdf.empty or ocdf.empty:
        return None

    overlap = kwargs["overlap"]
    how = kwargs["how"]
    suffix = kwargs["suffix"]

    if how == "upstream":
        strand = scdf.Strand.iloc[0]
        how = {"+": "previous", "-": "next"}[strand]
    elif how == "downstream":
        strand = scdf.Strand.iloc[0]
        how = {"+": "next", "-": "previous"}[strand]

    ocdf = ocdf.reset_index(drop=True)

    if overlap:
        nearest_df, df_to_find_nearest_in = _overlapping_for_nearest(
            scdf, ocdf, suffix)
    else:
        df_to_find_nearest_in = scdf

    if not df_to_find_nearest_in.empty:
        df_to_find_nearest_in = sort_one_by_one(df_to_find_nearest_in, "Start",
                                                "End")

        ocdf = sort_one_by_one(ocdf, "Start", "End")