# 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="fff7d7c8a2075faa5da5ccad34db6f9ba87459dd.workbench",
    name="volume-merge",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_VolumeMergeUpToParamsDictNoTag = typing.TypedDict('_VolumeMergeUpToParamsDictNoTag', {
    "last-subvol": str,
    "reverse": bool,
})
VolumeMergeUpToParamsDictTagged = typing.TypedDict('VolumeMergeUpToParamsDictTagged', {
    "@type": typing.Literal["up-to"],
    "last-subvol": str,
    "reverse": bool,
})
VolumeMergeUpToParamsDict = _VolumeMergeUpToParamsDictNoTag | VolumeMergeUpToParamsDictTagged


_VolumeMergeSubvolumeParamsDictNoTag = typing.TypedDict('_VolumeMergeSubvolumeParamsDictNoTag', {
    "subvol": str,
    "up-to": typing.NotRequired[VolumeMergeUpToParamsDict | None],
})
VolumeMergeSubvolumeParamsDictTagged = typing.TypedDict('VolumeMergeSubvolumeParamsDictTagged', {
    "@type": typing.Literal["subvolume"],
    "subvol": str,
    "up-to": typing.NotRequired[VolumeMergeUpToParamsDict | None],
})
VolumeMergeSubvolumeParamsDict = _VolumeMergeSubvolumeParamsDictNoTag | VolumeMergeSubvolumeParamsDictTagged


_VolumeMergeVolumeParamsDictNoTag = typing.TypedDict('_VolumeMergeVolumeParamsDictNoTag', {
    "volume-in": InputPathType,
    "subvolume": typing.NotRequired[list[VolumeMergeSubvolumeParamsDict] | None],
})
VolumeMergeVolumeParamsDictTagged = typing.TypedDict('VolumeMergeVolumeParamsDictTagged', {
    "@type": typing.Literal["volume"],
    "volume-in": InputPathType,
    "subvolume": typing.NotRequired[list[VolumeMergeSubvolumeParamsDict] | None],
})
VolumeMergeVolumeParamsDict = _VolumeMergeVolumeParamsDictNoTag | VolumeMergeVolumeParamsDictTagged


_VolumeMergeParamsDictNoTag = typing.TypedDict('_VolumeMergeParamsDictNoTag', {
    "volume-out": str,
    "volume": typing.NotRequired[list[VolumeMergeVolumeParamsDict] | None],
})
VolumeMergeParamsDictTagged = typing.TypedDict('VolumeMergeParamsDictTagged', {
    "@type": typing.Literal["workbench/volume-merge"],
    "volume-out": str,
    "volume": typing.NotRequired[list[VolumeMergeVolumeParamsDict] | None],
})
VolumeMergeParamsDict = _VolumeMergeParamsDictNoTag | VolumeMergeParamsDictTagged


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


def volume_merge_up_to_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeMergeUpToParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("last-subvol", None) is None:
        raise StyxValidationError("`last-subvol` must not be None")
    if not isinstance(params["last-subvol"], str):
        raise StyxValidationError(f'`last-subvol` has the wrong type: Received `{type(params.get("last-subvol", None))}` expected `str`')
    if params.get("reverse", False) is None:
        raise StyxValidationError("`reverse` must not be None")
    if not isinstance(params["reverse"], bool):
        raise StyxValidationError(f'`reverse` has the wrong type: Received `{type(params.get("reverse", False))}` expected `bool`')


def volume_merge_up_to_cargs(
    params: VolumeMergeUpToParamsDict,
    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.extend([
        "-up-to",
        params.get("last-subvol", None)
    ])
    if params.get("reverse", False):
        cargs.append("-reverse")
    return cargs


def volume_merge_subvolume(
    subvol: str,
    up_to: VolumeMergeUpToParamsDict | None = None,
) -> VolumeMergeSubvolumeParamsDictTagged:
    """
    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_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeMergeSubvolumeParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("subvol", None) is None:
        raise StyxValidationError("`subvol` must not be None")
    if not isinstance(params["subvol"], str):
        raise StyxValidationError(f'`subvol` has the wrong type: Received `{type(params.get("subvol", None))}` expected `str`')
    if params.get("up-to", None) is not None:
        volume_merge_up_to_validate(params["up-to"])


def volume_merge_subvolume_cargs(
    params: VolumeMergeSubvolumeParamsDict,
    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.extend([
        "-subvolume",
        params.get("subvol", None),
        *(volume_merge_up_to_cargs(params.get("up-to", None), execution) if (params.get("up-to", None) is not None) else [])
    ])
    return cargs


def volume_merge_volume(
    volume_in: InputPathType,
    subvolume: list[VolumeMergeSubvolumeParamsDict] | None = None,
) -> VolumeMergeVolumeParamsDictTagged:
    """
    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_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeMergeVolumeParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("volume-in", None) is None:
        raise StyxValidationError("`volume-in` must not be None")
    if not isinstance(params["volume-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`volume-in` has the wrong type: Received `{type(params.get("volume-in", None))}` expected `InputPathType`')
    if params.get("subvolume", None) is not None:
        if not isinstance(params["subvolume"], list):
            raise StyxValidationError(f'`subvolume` has the wrong type: Received `{type(params.get("subvolume", None))}` expected `list[VolumeMergeSubvolumeParamsDict] | None`')
        for e in params["subvolume"]:
            volume_merge_subvolume_validate(e)


def volume_merge_volume_cargs(
    params: VolumeMergeVolumeParamsDict,
    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.extend([
        "-volume",
        execution.input_file(params.get("volume-in", None)),
        *([a for c in [volume_merge_subvolume_cargs(s, execution) for s in params.get("subvolume", None)] for a in c] if (params.get("subvolume", None) is not None) else [])
    ])
    return cargs


class VolumeMergeOutputs(typing.NamedTuple):
    """
    Output object returned when calling `VolumeMergeParamsDict(...)`.
    """
    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[VolumeMergeVolumeParamsDict] | None = None,
) -> VolumeMergeParamsDictTagged:
    """
    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_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `VolumeMergeParamsDict` object.
    
    Args:
        params: The parameters object to validate.
    """
    if params is None or not isinstance(params, dict):
        raise StyxValidationError(f'Params object has the wrong type \'{type(params)}\'')
    if params.get("volume-out", None) is None:
        raise StyxValidationError("`volume-out` must not be None")
    if not isinstance(params["volume-out"], str):
        raise StyxValidationError(f'`volume-out` has the wrong type: Received `{type(params.get("volume-out", None))}` expected `str`')
    if params.get("volume", None) is not None:
        if not isinstance(params["volume"], list):
            raise StyxValidationError(f'`volume` has the wrong type: Received `{type(params.get("volume", None))}` expected `list[VolumeMergeVolumeParamsDict] | None`')
        for e in params["volume"]:
            volume_merge_volume_validate(e)


def volume_merge_cargs(
    params: VolumeMergeParamsDict,
    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.extend([
        "wb_command",
        "-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: VolumeMergeParamsDict,
    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: VolumeMergeParamsDict,
    runner: Runner | None = None,
) -> VolumeMergeOutputs:
    """
    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.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `VolumeMergeOutputs`).
    """
    volume_merge_validate(params)
    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[VolumeMergeVolumeParamsDict] | None = None,
    runner: Runner | None = None,
) -> VolumeMergeOutputs:
    """
    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.
    
    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",
    "VolumeMergeParamsDict",
    "VolumeMergeParamsDictTagged",
    "VolumeMergeSubvolumeParamsDict",
    "VolumeMergeSubvolumeParamsDictTagged",
    "VolumeMergeUpToParamsDict",
    "VolumeMergeUpToParamsDictTagged",
    "VolumeMergeVolumeParamsDict",
    "VolumeMergeVolumeParamsDictTagged",
    "volume_merge",
    "volume_merge_execute",
    "volume_merge_params",
    "volume_merge_subvolume",
    "volume_merge_up_to",
    "volume_merge_volume",
]
