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

import typing
import pathlib
from styxdefs import *

METRIC_MERGE_METADATA = Metadata(
    id="58734bb6d294ee25611f347ddc44d2a3b041b696.workbench",
    name="metric-merge",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


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


_MetricMergeColumnParamsDictNoTag = typing.TypedDict('_MetricMergeColumnParamsDictNoTag', {
    "column": str,
    "up-to": typing.NotRequired[MetricMergeUpToParamsDict | None],
})
MetricMergeColumnParamsDictTagged = typing.TypedDict('MetricMergeColumnParamsDictTagged', {
    "@type": typing.Literal["column"],
    "column": str,
    "up-to": typing.NotRequired[MetricMergeUpToParamsDict | None],
})
MetricMergeColumnParamsDict = _MetricMergeColumnParamsDictNoTag | MetricMergeColumnParamsDictTagged


_MetricMergeMetricParamsDictNoTag = typing.TypedDict('_MetricMergeMetricParamsDictNoTag', {
    "metric-in": InputPathType,
    "column": typing.NotRequired[list[MetricMergeColumnParamsDict] | None],
})
MetricMergeMetricParamsDictTagged = typing.TypedDict('MetricMergeMetricParamsDictTagged', {
    "@type": typing.Literal["metric"],
    "metric-in": InputPathType,
    "column": typing.NotRequired[list[MetricMergeColumnParamsDict] | None],
})
MetricMergeMetricParamsDict = _MetricMergeMetricParamsDictNoTag | MetricMergeMetricParamsDictTagged


_MetricMergeParamsDictNoTag = typing.TypedDict('_MetricMergeParamsDictNoTag', {
    "metric-out": str,
    "metric": typing.NotRequired[list[MetricMergeMetricParamsDict] | None],
})
MetricMergeParamsDictTagged = typing.TypedDict('MetricMergeParamsDictTagged', {
    "@type": typing.Literal["workbench/metric-merge"],
    "metric-out": str,
    "metric": typing.NotRequired[list[MetricMergeMetricParamsDict] | None],
})
MetricMergeParamsDict = _MetricMergeParamsDictNoTag | MetricMergeParamsDictTagged


def metric_merge_up_to(
    last_column: str,
    reverse: bool = False,
) -> MetricMergeUpToParamsDictTagged:
    """
    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 metric_merge_up_to_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MetricMergeUpToParamsDict` 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 metric_merge_up_to_cargs(
    params: MetricMergeUpToParamsDict,
    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 metric_merge_column(
    column: str,
    up_to: MetricMergeUpToParamsDict | None = None,
) -> MetricMergeColumnParamsDictTagged:
    """
    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 metric_merge_column_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MetricMergeColumnParamsDict` 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:
        metric_merge_up_to_validate(params["up-to"])


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


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


def metric_merge_metric_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MetricMergeMetricParamsDict` 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("metric-in", None) is None:
        raise StyxValidationError("`metric-in` must not be None")
    if not isinstance(params["metric-in"], (pathlib.Path, str)):
        raise StyxValidationError(f'`metric-in` has the wrong type: Received `{type(params.get("metric-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[MetricMergeColumnParamsDict] | None`')
        for e in params["column"]:
            metric_merge_column_validate(e)


def metric_merge_metric_cargs(
    params: MetricMergeMetricParamsDict,
    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([
        "-metric",
        execution.input_file(params.get("metric-in", None)),
        *([a for c in [metric_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 MetricMergeOutputs(typing.NamedTuple):
    """
    Output object returned when calling `MetricMergeParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""
    metric_out: OutputPathType
    """the output metric"""


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


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


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


def metric_merge_outputs(
    params: MetricMergeParamsDict,
    execution: Execution,
) -> MetricMergeOutputs:
    """
    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 = MetricMergeOutputs(
        root=execution.output_file("."),
        metric_out=execution.output_file(params.get("metric-out", None)),
    )
    return ret


def metric_merge_execute(
    params: MetricMergeParamsDict,
    runner: Runner | None = None,
) -> MetricMergeOutputs:
    """
    MERGE METRIC FILES INTO A NEW FILE.
    
    Takes one or more metric files and constructs a new metric file by
    concatenating columns from them. The input metric files must have the same
    number of vertices and same structure.
    
    Example: wb_command -metric-merge out.func.gii -metric first.func.gii
    -column 1 -metric second.func.gii
    
    This example would take the first column from first.func.gii, followed by
    all columns from second.func.gii, and write these columns to out.func.gii.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetricMergeOutputs`).
    """
    metric_merge_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(METRIC_MERGE_METADATA)
    params = execution.params(params)
    cargs = metric_merge_cargs(params, execution)
    ret = metric_merge_outputs(params, execution)
    execution.run(cargs)
    return ret


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


__all__ = [
    "METRIC_MERGE_METADATA",
    "MetricMergeColumnParamsDict",
    "MetricMergeColumnParamsDictTagged",
    "MetricMergeMetricParamsDict",
    "MetricMergeMetricParamsDictTagged",
    "MetricMergeOutputs",
    "MetricMergeParamsDict",
    "MetricMergeParamsDictTagged",
    "MetricMergeUpToParamsDict",
    "MetricMergeUpToParamsDictTagged",
    "metric_merge",
    "metric_merge_column",
    "metric_merge_execute",
    "metric_merge_metric",
    "metric_merge_params",
    "metric_merge_up_to",
]
