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

import typing
import pathlib
from styxdefs import *

LABEL_MERGE_METADATA = Metadata(
    id="791909e8270318bd504b97ada9b61a7c8e2898d1.workbench",
    name="label-merge",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_LabelMergeUpToParamsDictNoTag = typing.TypedDict('_LabelMergeUpToParamsDictNoTag', {
    "last-column": str,
    "reverse": bool,
})
LabelMergeUpToParamsDictTagged = typing.TypedDict('LabelMergeUpToParamsDictTagged', {
    "@type": typing.Literal["up-to"],
    "last-column": str,
    "reverse": bool,
})
LabelMergeUpToParamsDict = _LabelMergeUpToParamsDictNoTag | LabelMergeUpToParamsDictTagged


_LabelMergeColumnParamsDictNoTag = typing.TypedDict('_LabelMergeColumnParamsDictNoTag', {
    "column": str,
    "up-to": typing.NotRequired[LabelMergeUpToParamsDict | None],
})
LabelMergeColumnParamsDictTagged = typing.TypedDict('LabelMergeColumnParamsDictTagged', {
    "@type": typing.Literal["column"],
    "column": str,
    "up-to": typing.NotRequired[LabelMergeUpToParamsDict | None],
})
LabelMergeColumnParamsDict = _LabelMergeColumnParamsDictNoTag | LabelMergeColumnParamsDictTagged


_LabelMergeLabelParamsDictNoTag = typing.TypedDict('_LabelMergeLabelParamsDictNoTag', {
    "label-in": InputPathType,
    "column": typing.NotRequired[list[LabelMergeColumnParamsDict] | None],
})
LabelMergeLabelParamsDictTagged = typing.TypedDict('LabelMergeLabelParamsDictTagged', {
    "@type": typing.Literal["label"],
    "label-in": InputPathType,
    "column": typing.NotRequired[list[LabelMergeColumnParamsDict] | None],
})
LabelMergeLabelParamsDict = _LabelMergeLabelParamsDictNoTag | LabelMergeLabelParamsDictTagged


_LabelMergeParamsDictNoTag = typing.TypedDict('_LabelMergeParamsDictNoTag', {
    "label-out": str,
    "label": typing.NotRequired[list[LabelMergeLabelParamsDict] | None],
})
LabelMergeParamsDictTagged = typing.TypedDict('LabelMergeParamsDictTagged', {
    "@type": typing.Literal["workbench/label-merge"],
    "label-out": str,
    "label": typing.NotRequired[list[LabelMergeLabelParamsDict] | None],
})
LabelMergeParamsDict = _LabelMergeParamsDictNoTag | LabelMergeParamsDictTagged


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


def label_merge_up_to_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `LabelMergeUpToParamsDict` 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-column", None) is None:
        raise StyxValidationError("`last-column` must not be None")
    if not isinstance(params["last-column"], str):
        raise StyxValidationError(f'`last-column` has the wrong type: Received `{type(params.get("last-column", 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 label_merge_up_to_cargs(
    params: LabelMergeUpToParamsDict,
    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-column", None)
    ])
    if params.get("reverse", False):
        cargs.append("-reverse")
    return cargs


def label_merge_column(
    column: str,
    up_to: LabelMergeUpToParamsDict | None = None,
) -> LabelMergeColumnParamsDictTagged:
    """
    Build parameters.
    
    Args:
        column: the column number or name.
        up_to: use an inclusive range of columns.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "column",
        "column": column,
    }
    if up_to is not None:
        params["up-to"] = up_to
    return params


def label_merge_column_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `LabelMergeColumnParamsDict` 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("column", None) is None:
        raise StyxValidationError("`column` must not be None")
    if not isinstance(params["column"], str):
        raise StyxValidationError(f'`column` has the wrong type: Received `{type(params.get("column", None))}` expected `str`')
    if params.get("up-to", None) is not None:
        label_merge_up_to_validate(params["up-to"])


def label_merge_column_cargs(
    params: LabelMergeColumnParamsDict,
    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([
        "-column",
        params.get("column", None),
        *(label_merge_up_to_cargs(params.get("up-to", None), execution) if (params.get("up-to", None) is not None) else [])
    ])
    return cargs


def label_merge_label(
    label_in: InputPathType,
    column: list[LabelMergeColumnParamsDict] | None = None,
) -> LabelMergeLabelParamsDictTagged:
    """
    Build parameters.
    
    Args:
        label_in: a label file to use columns from.
        column: select a single column to use.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "label",
        "label-in": label_in,
    }
    if column is not None:
        params["column"] = column
    return params


def label_merge_label_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `LabelMergeLabelParamsDict` 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("label-in", None) is None:
        raise StyxValidationError("`label-in` must not be None")
    if not isinstance(params["label-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`label-in` has the wrong type: Received `{type(params.get("label-in", None))}` expected `InputPathType`')
    if params.get("column", None) is not None:
        if not isinstance(params["column"], list):
            raise StyxValidationError(f'`column` has the wrong type: Received `{type(params.get("column", None))}` expected `list[LabelMergeColumnParamsDict] | None`')
        for e in params["column"]:
            label_merge_column_validate(e)


def label_merge_label_cargs(
    params: LabelMergeLabelParamsDict,
    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([
        "-label",
        execution.input_file(params.get("label-in", None)),
        *([a for c in [label_merge_column_cargs(s, execution) for s in params.get("column", None)] for a in c] if (params.get("column", None) is not None) else [])
    ])
    return cargs


class LabelMergeOutputs(typing.NamedTuple):
    """
    Output object returned when calling `LabelMergeParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    label_out: OutputPathType
    """the output label"""


def label_merge_params(
    label_out: str,
    label: list[LabelMergeLabelParamsDict] | None = None,
) -> LabelMergeParamsDictTagged:
    """
    Build parameters.
    
    Args:
        label_out: the output label.
        label: specify an input label.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/label-merge",
        "label-out": label_out,
    }
    if label is not None:
        params["label"] = label
    return params


def label_merge_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `LabelMergeParamsDict` 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("label-out", None) is None:
        raise StyxValidationError("`label-out` must not be None")
    if not isinstance(params["label-out"], str):
        raise StyxValidationError(f'`label-out` has the wrong type: Received `{type(params.get("label-out", None))}` expected `str`')
    if params.get("label", None) is not None:
        if not isinstance(params["label"], list):
            raise StyxValidationError(f'`label` has the wrong type: Received `{type(params.get("label", None))}` expected `list[LabelMergeLabelParamsDict] | None`')
        for e in params["label"]:
            label_merge_label_validate(e)


def label_merge_cargs(
    params: LabelMergeParamsDict,
    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",
        "-label-merge"
    ])
    cargs.append(params.get("label-out", None))
    if params.get("label", None) is not None:
        cargs.extend([a for c in [label_merge_label_cargs(s, execution) for s in params.get("label", None)] for a in c])
    return cargs


def label_merge_outputs(
    params: LabelMergeParamsDict,
    execution: Execution,
) -> LabelMergeOutputs:
    """
    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 = LabelMergeOutputs(
        root=execution.output_file("."),
        label_out=execution.output_file(params.get("label-out", None)),
    )
    return ret


def label_merge_execute(
    params: LabelMergeParamsDict,
    runner: Runner | None = None,
) -> LabelMergeOutputs:
    """
    MERGE LABEL FILES INTO A NEW FILE.
    
    Takes one or more label files and constructs a new label file by
    concatenating columns from them. The input files must have the same number
    of vertices and the same structure.
    
    Example: wb_command -label-merge out.label.gii -label first.label.gii
    -column 1 -label second.label.gii
    
    This example would take the first column from first.label.gii and all
    subvolumes from second.label.gii, and write these to out.label.gii.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelMergeOutputs`).
    """
    label_merge_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(LABEL_MERGE_METADATA)
    params = execution.params(params)
    cargs = label_merge_cargs(params, execution)
    ret = label_merge_outputs(params, execution)
    execution.run(cargs)
    return ret


def label_merge(
    label_out: str,
    label: list[LabelMergeLabelParamsDict] | None = None,
    runner: Runner | None = None,
) -> LabelMergeOutputs:
    """
    MERGE LABEL FILES INTO A NEW FILE.
    
    Takes one or more label files and constructs a new label file by
    concatenating columns from them. The input files must have the same number
    of vertices and the same structure.
    
    Example: wb_command -label-merge out.label.gii -label first.label.gii
    -column 1 -label second.label.gii
    
    This example would take the first column from first.label.gii and all
    subvolumes from second.label.gii, and write these to out.label.gii.
    
    Args:
        label_out: the output label.
        label: specify an input label.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelMergeOutputs`).
    """
    params = label_merge_params(
        label_out=label_out,
        label=label,
    )
    return label_merge_execute(params, runner)


__all__ = [
    "LABEL_MERGE_METADATA",
    "LabelMergeColumnParamsDict",
    "LabelMergeColumnParamsDictTagged",
    "LabelMergeLabelParamsDict",
    "LabelMergeLabelParamsDictTagged",
    "LabelMergeOutputs",
    "LabelMergeParamsDict",
    "LabelMergeParamsDictTagged",
    "LabelMergeUpToParamsDict",
    "LabelMergeUpToParamsDictTagged",
    "label_merge",
    "label_merge_column",
    "label_merge_execute",
    "label_merge_label",
    "label_merge_params",
    "label_merge_up_to",
]
