"""Toolchain definitions for Wails."""

WailsToolchainInfo = provider(
    doc = "Caller-supplied Wails executable, Go executable, and runfiles.",
    fields = {
        "default_runfiles": "Runfiles for the Wails executable.",
        "go_default_runfiles": "Runfiles for the Go executable.",
        "go_executable": "Executable file for the Go tool.",
        "executable": "Executable file for the Wails tool.",
        "files_to_run": "FilesToRunProvider for the Wails tool.",
    },
)

def _wails_toolchain_impl(ctx):
    wails_default_info = ctx.attr.wails[DefaultInfo]
    go_default_info = ctx.attr.go[DefaultInfo]

    return [
        platform_common.ToolchainInfo(
            wails = WailsToolchainInfo(
                default_runfiles = wails_default_info.default_runfiles,
                go_default_runfiles = go_default_info.default_runfiles,
                go_executable = ctx.file.go,
                executable = ctx.executable.wails,
                files_to_run = wails_default_info.files_to_run,
            ),
        ),
    ]

wails_toolchain = rule(
    implementation = _wails_toolchain_impl,
    attrs = {
        "go": attr.label(
            mandatory = True,
            cfg = "exec",
            allow_single_file = True,
            doc = "Executable file used for hermetic `go run` invocations inside the Wails tool.",
        ),
        "wails": attr.label(
            mandatory = True,
            cfg = "exec",
            executable = True,
            allow_files = True,
            doc = "Executable target used to invoke Wails.",
        ),
    },
    doc = "Registers caller-supplied Wails and Go executables as a Bazel toolchain.",
)
