import pathlib
import typing


def _get_file_format_id(path: pathlib.Path, file_format: typing.Optional[str]) -> str:
    """Determine the file format for writing based on the arguments."""
    formats = {
        "yaml": path.name.endswith((".yml", ".yaml")),
        "toml": path.name.endswith(".toml"),
        "json": path.name.endswith(".json"),
    }
    finder = (k for k, v in formats.items() if file_format == k or v)
    return next(finder, "json")