# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.

"""
DataLab strings utilities
"""

from __future__ import annotations

import difflib
import os.path as osp
import webbrowser


def save_html_diff(
    text1: str, text2: str, desc1: str, desc2: str, filename: str
) -> None:
    """Generates HTML diff between two strings, saves it to a file, and opens it
    in a web browser (Windows only).

    Args:
        text1 (str): The first string to compare.
        text2 (str): The second string to compare.
        desc1 (str): Description of the first string.
        desc2 (str): Description of the second string.
        filename (str): The name of the file to save the HTML diff to.

    Returns:
        None
    """
    differ = difflib.HtmlDiff()
    diff_html = differ.make_file(text1.splitlines(), text2.splitlines(), desc1, desc2)
    with open(filename, "w", encoding="utf-8") as file:
        file.write(diff_html)
    webbrowser.open("file://" + osp.realpath(filename))
