# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from io import StringIO
import sys
from pathlib import Path
import black
from . import _get_all_libraries, _runtime
from textwrap import dedent


def stubgen(out: StringIO = sys.stdout) -> str:
    import numba_cuda_mlir

    unformatted = StringIO()

    numba_cuda_mlir_root = Path(numba_cuda_mlir.__path__[0]).parent
    runtime_path = Path(__file__).relative_to(numba_cuda_mlir_root)
    unformatted.write(
        dedent(
            f"""
    # Autogenerated by {runtime_path}
    # Please do not edit

    from numba_cuda_mlir.typing import none, ptr, int8, int16, int32, int64, bool, float32, float64, UniTuple, Tuple

    """
        )
    )
    for rtlib in _get_all_libraries():
        rtlib = getattr(_runtime, rtlib)
        for func in rtlib.functions.keys():
            unformatted.write(str(getattr(rtlib, func)) + "\n")
    unformatted.write("\n")
    out.write(black.format_str(unformatted.getvalue(), mode=black.Mode()))


if __name__ == "__main__":
    stubgen()
