import textwrap


async def snippet_to_embed(file_contents, file_path, start_line, end_line):
    """Given file contents, file path, start line and end line creates a code block"""

    split_file_contents = file_contents.splitlines()

    if start_line is None:
        start_line, end_line = 1, len(split_file_contents)
    elif end_line is None:
        start_line = end_line = int(start_line)
    else:
        start_line = int(start_line)
        end_line = int(end_line)

    if start_line > end_line:
        start_line, end_line = end_line, start_line
    if start_line > len(split_file_contents) or end_line < 1:
        return ""

    start_line = max(1, start_line)
    end_line = min(len(split_file_contents), end_line)

    required = "\n".join(split_file_contents[start_line - 1:end_line])
    required = textwrap.dedent(required).rstrip().replace("`", "`\u200b")

    language = file_path.split("/")[-1].split(".")[-1]
    if not language.replace("-", "").replace("+", "").replace("_", "").isalnum():
        language = ""

    if len(required) != 0:
        return f"```{language}\n{required}```\n"
    return "``` ```\n"