# This file was auto generated by Styx.
# Do not edit this file directly.

import typing
import pathlib
from styxdefs import *

VOLUME_MERGE_METADATA = Metadata(
    id="0484245a1d35b7ed32a01a9b8363c20b753b6141.boutiques",
    name="volume-merge",
    package="workbench",
    container_image_tag="brainlife/connectome_workbench:1.5.0-freesurfer-update",
)


VolumeMergeUpToParameters = typing.TypedDict('VolumeMergeUpToParameters', {
    "@type": typing.NotRequired[typing.Literal["up_to"]],
    "last_subvol": str,
    "opt_reverse": bool,
})
VolumeMergeUpToParametersTagged = typing.TypedDict('VolumeMergeUpToParametersTagged', {
    "@type": typing.Literal["up_to"],
    "last_subvol": str,
    "opt_reverse": bool,
})


VolumeMergeSubvolumeParameters = typing.TypedDict('VolumeMergeSubvolumeParameters', {
    "@type": typing.NotRequired[typing.Literal["subvolume"]],
    "subvol": str,
    "up_to": typing.NotRequired[VolumeMergeUpToParameters | None],
})
VolumeMergeSubvolumeParametersTagged = typing.TypedDict('VolumeMergeSubvolumeParametersTagged', {
    "@type": typing.Literal["subvolume"],
    "subvol": str,
    "up_to": typing.NotRequired[VolumeMergeUpToParameters | None],
})


VolumeMergeVolumeParameters = typing.TypedDict('VolumeMergeVolumeParameters', {
    "@type": typing.NotRequired[typing.Literal["volume"]],
    "volume_in": InputPathType,
    "subvolume": typing.NotRequired[list[VolumeMergeSubvolumeParameters] | None],
})
VolumeMergeVolumeParametersTagged = typing.TypedDict('VolumeMergeVolumeParametersTagged', {
    "@type": typing.Literal["volume"],
    "volume_in": InputPathType,
    "subvolume": typing.NotRequired[list[VolumeMergeSubvolumeParameters] | None],
})


VolumeMergeParameters = typing.TypedDict('VolumeMergeParameters', {
    "@type": typing.NotRequired[typing.Literal["workbench/volume-merge"]],
    "volume_out": str,
    "volume": typing.NotRequired[list[VolumeMergeVolumeParameters] | None],
})
VolumeMergeParametersTagged = typing.TypedDict('VolumeMergeParametersTagged', {
    "@type": typing.Literal["workbench/volume-merge"],
    "volume_out": str,
    "volume": typing.NotRequired[list[VolumeMergeVolumeParameters] | None],
})


def volume_merge_up_to_params(
    last_subvol: str,
    opt_reverse: bool = False,
) -> VolumeMergeUpToParametersTagged:
    """
    Build parameters.
    
    Args:
        last_subvol: the number or name of the last subvolume to include.
        opt_reverse: use the range in reverse order.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "up_to",
        "last_subvol": last_subvol,
        "opt_reverse": opt_reverse,
    }
    return params


def volume_merge_up_to_cargs(
    params: VolumeMergeUpToParameters,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.append("-up-to")
    cargs.append(params.get("last_subvol", None))
    if params.get("opt_reverse", False):
        cargs.append("-reverse")
    return cargs


def volume_merge_subvolume_params(
    subvol: str,
    up_to: VolumeMergeUpToParameters | None = None,
) -> VolumeMergeSubvolumeParametersTagged:
    """
    Build parameters.
    
    Args:
        subvol: the subvolume number or name.
        up_to: use an inclusive range of subvolumes.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "subvolume",
        "subvol": subvol,
    }
    if up_to is not None:
        params["up_to"] = up_to
    return params


def volume_merge_subvolume_cargs(
    params: VolumeMergeSubvolumeParameters,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.append("-subvolume")
    cargs.append(params.get("subvol", None))
    if params.get("up_to", None) is not None:
        cargs.extend(volume_merge_up_to_cargs(params.get("up_to", None), execution))
    return cargs


def volume_merge_volume_params(
    volume_in: InputPathType,
    subvolume: list[VolumeMergeSubvolumeParameters] | None = None,
) -> VolumeMergeVolumeParametersTagged:
    """
    Build parameters.
    
    Args:
        volume_in: a volume file to use subvolumes from.
        subvolume: select a single subvolume to use.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "volume",
        "volume_in": volume_in,
    }
    if subvolume is not None:
        params["subvolume"] = subvolume
    return params


def volume_merge_volume_cargs(
    params: VolumeMergeVolumeParameters,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.append("-volume")
    cargs.append(execution.input_file(params.get("volume_in", None)))
    if params.get("subvolume", None) is not None:
        cargs.extend([a for c in [volume_merge_subvolume_cargs(s, execution) for s in params.get("subvolume", None)] for a in c])
    return cargs


class VolumeMergeOutputs(typing.NamedTuple):
    """
    Output object returned when calling `VolumeMergeParameters(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    volume_out: OutputPathType
    """the output volume file"""


def volume_merge_params(
    volume_out: str,
    volume: list[VolumeMergeVolumeParameters] | None = None,
) -> VolumeMergeParametersTagged:
    """
    Build parameters.
    
    Args:
        volume_out: the output volume file.
        volume: specify an input volume file.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/volume-merge",
        "volume_out": volume_out,
    }
    if volume is not None:
        params["volume"] = volume
    return params


def volume_merge_cargs(
    params: VolumeMergeParameters,
    execution: Execution,
) -> list[str]:
    """
    Build command-line arguments from parameters.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Command-line arguments.
    """
    cargs = []
    cargs.append("wb_command")
    cargs.append("-volume-merge")
    cargs.append(params.get("volume_out", None))
    if params.get("volume", None) is not None:
        cargs.extend([a for c in [volume_merge_volume_cargs(s, execution) for s in params.get("volume", None)] for a in c])
    return cargs


def volume_merge_outputs(
    params: VolumeMergeParameters,
    execution: Execution,
) -> VolumeMergeOutputs:
    """
    Build outputs object containing output file paths and possibly stdout/stderr.
    
    Args:
        params: The parameters.
        execution: The execution object for resolving input paths.
    Returns:
        Outputs object.
    """
    ret = VolumeMergeOutputs(
        root=execution.output_file("."),
        volume_out=execution.output_file(params.get("volume_out", None)),
    )
    return ret


def volume_merge_execute(
    params: VolumeMergeParameters,
    runner: Runner | None = None,
) -> VolumeMergeOutputs:
    """
    volume-merge
    
    Merge volume files into a new file.
    
    Takes one or more volume files and constructs a new volume file by
    concatenating subvolumes from them. The input volume files must have the
    same volume space.
    
    Example: wb_command -volume-merge out.nii -volume first.nii -subvolume 1
    -volume second.nii
    
    This example would take the first subvolume from first.nii, followed by all
    subvolumes from second.nii, and write these to out.nii.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeMergeOutputs`).
    """
    runner = runner or get_global_runner()
    execution = runner.start_execution(VOLUME_MERGE_METADATA)
    params = execution.params(params)
    cargs = volume_merge_cargs(params, execution)
    ret = volume_merge_outputs(params, execution)
    execution.run(cargs)
    return ret


def volume_merge(
    volume_out: str,
    volume: list[VolumeMergeVolumeParameters] | None = None,
    runner: Runner | None = None,
) -> VolumeMergeOutputs:
    """
    volume-merge
    
    Merge volume files into a new file.
    
    Takes one or more volume files and constructs a new volume file by
    concatenating subvolumes from them. The input volume files must have the
    same volume space.
    
    Example: wb_command -volume-merge out.nii -volume first.nii -subvolume 1
    -volume second.nii
    
    This example would take the first subvolume from first.nii, followed by all
    subvolumes from second.nii, and write these to out.nii.
    
    Author: Connectome Workbench Developers
    
    URL: https://github.com/Washington-University/workbench
    
    Args:
        volume_out: the output volume file.
        volume: specify an input volume file.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeMergeOutputs`).
    """
    params = volume_merge_params(
        volume_out=volume_out,
        volume=volume,
    )
    return volume_merge_execute(params, runner)


__all__ = [
    "VOLUME_MERGE_METADATA",
    "VolumeMergeOutputs",
    "volume_merge",
    "volume_merge_execute",
    "volume_merge_params",
    "volume_merge_subvolume_params",
    "volume_merge_up_to_params",
    "volume_merge_volume_params",
]
