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

import typing
import pathlib
from styxdefs import *

LABEL_MODIFY_KEYS_METADATA = Metadata(
    id="e84f8183032da13712cc4d51e831efb848fef7f4.workbench",
    name="label-modify-keys",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_LabelModifyKeysParamsDictNoTag = typing.TypedDict('_LabelModifyKeysParamsDictNoTag', {
    "label-in": InputPathType,
    "remap-file": str,
    "label-out": str,
    "column": typing.NotRequired[str | None],
})
LabelModifyKeysParamsDictTagged = typing.TypedDict('LabelModifyKeysParamsDictTagged', {
    "@type": typing.Literal["workbench/label-modify-keys"],
    "label-in": InputPathType,
    "remap-file": str,
    "label-out": str,
    "column": typing.NotRequired[str | None],
})
LabelModifyKeysParamsDict = _LabelModifyKeysParamsDictNoTag | LabelModifyKeysParamsDictTagged


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


def label_modify_keys_params(
    label_in: InputPathType,
    remap_file: str,
    label_out: str,
    column: str | None = None,
) -> LabelModifyKeysParamsDictTagged:
    """
    Build parameters.
    
    Args:
        label_in: the input label file.
        remap_file: text file with old and new key values.
        label_out: output label file.
        column: select a single column to use\
            \
            the column number or name.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/label-modify-keys",
        "label-in": label_in,
        "remap-file": remap_file,
        "label-out": label_out,
    }
    if column is not None:
        params["column"] = column
    return params


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


def label_modify_keys_cargs(
    params: LabelModifyKeysParamsDict,
    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-modify-keys"
    ])
    cargs.append(execution.input_file(params.get("label-in", None)))
    cargs.append(params.get("remap-file", None))
    cargs.append(params.get("label-out", None))
    if params.get("column", None) is not None:
        cargs.extend([
            "-column",
            params.get("column", None)
        ])
    return cargs


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


def label_modify_keys_execute(
    params: LabelModifyKeysParamsDict,
    runner: Runner | None = None,
) -> LabelModifyKeysOutputs:
    """
    CHANGE KEY VALUES IN A LABEL FILE.
    
    <remap-file> should have lines of the form 'oldkey newkey', like so:
    
    3 5
    5 8
    8 2
    
    This would change the current label with key '3' to use the key '5' instead,
    5 would use 8, and 8 would use 2. Any collision in key values results in the
    label that was not specified in the remap file getting remapped to an
    otherwise unused key. Remapping more than one key to the same new key, or
    the same key to more than one new key, results in an error. This will not
    change the appearance of the file when displayed, as it will change the key
    values in the data at the same time.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelModifyKeysOutputs`).
    """
    label_modify_keys_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(LABEL_MODIFY_KEYS_METADATA)
    params = execution.params(params)
    cargs = label_modify_keys_cargs(params, execution)
    ret = label_modify_keys_outputs(params, execution)
    execution.run(cargs)
    return ret


def label_modify_keys(
    label_in: InputPathType,
    remap_file: str,
    label_out: str,
    column: str | None = None,
    runner: Runner | None = None,
) -> LabelModifyKeysOutputs:
    """
    CHANGE KEY VALUES IN A LABEL FILE.
    
    <remap-file> should have lines of the form 'oldkey newkey', like so:
    
    3 5
    5 8
    8 2
    
    This would change the current label with key '3' to use the key '5' instead,
    5 would use 8, and 8 would use 2. Any collision in key values results in the
    label that was not specified in the remap file getting remapped to an
    otherwise unused key. Remapping more than one key to the same new key, or
    the same key to more than one new key, results in an error. This will not
    change the appearance of the file when displayed, as it will change the key
    values in the data at the same time.
    
    Args:
        label_in: the input label file.
        remap_file: text file with old and new key values.
        label_out: output label file.
        column: select a single column to use\
            \
            the column number or name.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `LabelModifyKeysOutputs`).
    """
    params = label_modify_keys_params(
        label_in=label_in,
        remap_file=remap_file,
        label_out=label_out,
        column=column,
    )
    return label_modify_keys_execute(params, runner)


__all__ = [
    "LABEL_MODIFY_KEYS_METADATA",
    "LabelModifyKeysOutputs",
    "LabelModifyKeysParamsDict",
    "LabelModifyKeysParamsDictTagged",
    "label_modify_keys",
    "label_modify_keys_execute",
    "label_modify_keys_params",
]
