from pathlib import Path


def resolve_open_in_colab(content, page_info):
    """
    Replaces [[open-in-colab]] special markers by the proper svelte component.

    Args:
        content (`str`): The documentation to treat.
        page_info (`Dict[str, str]`, *optional*): Some information about the page.
    """
    if "[[open-in-colab]]" not in content:
        return content

    package_name = page_info["package_name"]
    language = page_info.get("language", "en")
    page_name = Path(page_info["page"]).stem
    nb_prefix = f"/github/huggingface/notebooks/blob/main/{package_name}_doc/{language}/"
    nb_prefix_colab = f"https://colab.research.google.com{nb_prefix}"
    nb_prefix_awsstudio = f"https://studiolab.sagemaker.aws/import{nb_prefix}"
    links = [
        ("Mixed", f"{nb_prefix_colab}{page_name}.ipynb"),
        ("PyTorch", f"{nb_prefix_colab}pytorch/{page_name}.ipynb"),
        ("TensorFlow", f"{nb_prefix_colab}tensorflow/{page_name}.ipynb"),
        ("Mixed", f"{nb_prefix_awsstudio}{page_name}.ipynb"),
        ("PyTorch", f"{nb_prefix_awsstudio}pytorch/{page_name}.ipynb"),
        ("TensorFlow", f"{nb_prefix_awsstudio}tensorflow/{page_name}.ipynb"),
    ]
    formatted_links = ['    {label: "' + key + '", value: "' + value + '"},' for key, value in links]

    svelte_component = """<DocNotebookDropdown
  classNames="absolute z-10 right-0 top-0"
  options={[
"""
    svelte_component += "\n".join(formatted_links)
    svelte_component += "\n]} />"

    return content.replace("[[open-in-colab]]", svelte_component)