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

import typing
import pathlib
from styxdefs import *

METADATA_STRING_REPLACE_METADATA = Metadata(
    id="28580214d2e8a263c11d2757b02229cfd3b15b21.workbench",
    name="metadata-string-replace",
    package="workbench",
    container_image_tag="nx10x/workbench:2.1.0",
)


_MetadataStringReplaceParamsDictNoTag = typing.TypedDict('_MetadataStringReplaceParamsDictNoTag', {
    "input-file": str,
    "find-string": str,
    "replace-string": str,
    "output-file": str,
    "case-insensitive": bool,
})
MetadataStringReplaceParamsDictTagged = typing.TypedDict('MetadataStringReplaceParamsDictTagged', {
    "@type": typing.Literal["workbench/metadata-string-replace"],
    "input-file": str,
    "find-string": str,
    "replace-string": str,
    "output-file": str,
    "case-insensitive": bool,
})
MetadataStringReplaceParamsDict = _MetadataStringReplaceParamsDictNoTag | MetadataStringReplaceParamsDictTagged


class MetadataStringReplaceOutputs(typing.NamedTuple):
    """
    Output object returned when calling `MetadataStringReplaceParamsDict(...)`.
    """
    root: OutputPathType
    """Output root folder. This is the root folder for all outputs."""


def metadata_string_replace_params(
    input_file: str,
    find_string: str,
    replace_string: str,
    output_file: str,
    case_insensitive: bool = False,
) -> MetadataStringReplaceParamsDictTagged:
    """
    Build parameters.
    
    Args:
        input_file: the file to replace metadata in.
        find_string: the string to find.
        replace_string: the string to replace <find-string> with.
        output_file: output - the name to save the modified file as.
        case_insensitive: match with case variation also.
    Returns:
        Parameter dictionary
    """
    params = {
        "@type": "workbench/metadata-string-replace",
        "input-file": input_file,
        "find-string": find_string,
        "replace-string": replace_string,
        "output-file": output_file,
        "case-insensitive": case_insensitive,
    }
    return params


def metadata_string_replace_validate(
    params: typing.Any,
) -> None:
    """
    Validate parameters. Throws an error if `params` is not a valid
    `MetadataStringReplaceParamsDict` 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("input-file", None) is None:
        raise StyxValidationError("`input-file` must not be None")
    if not isinstance(params["input-file"], str):
        raise StyxValidationError(f'`input-file` has the wrong type: Received `{type(params.get("input-file", None))}` expected `str`')
    if params.get("find-string", None) is None:
        raise StyxValidationError("`find-string` must not be None")
    if not isinstance(params["find-string"], str):
        raise StyxValidationError(f'`find-string` has the wrong type: Received `{type(params.get("find-string", None))}` expected `str`')
    if params.get("replace-string", None) is None:
        raise StyxValidationError("`replace-string` must not be None")
    if not isinstance(params["replace-string"], str):
        raise StyxValidationError(f'`replace-string` has the wrong type: Received `{type(params.get("replace-string", None))}` expected `str`')
    if params.get("output-file", None) is None:
        raise StyxValidationError("`output-file` must not be None")
    if not isinstance(params["output-file"], str):
        raise StyxValidationError(f'`output-file` has the wrong type: Received `{type(params.get("output-file", None))}` expected `str`')
    if params.get("case-insensitive", False) is None:
        raise StyxValidationError("`case-insensitive` must not be None")
    if not isinstance(params["case-insensitive"], bool):
        raise StyxValidationError(f'`case-insensitive` has the wrong type: Received `{type(params.get("case-insensitive", False))}` expected `bool`')


def metadata_string_replace_cargs(
    params: MetadataStringReplaceParamsDict,
    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",
        "-metadata-string-replace"
    ])
    cargs.append(params.get("input-file", None))
    cargs.append(params.get("find-string", None))
    cargs.append(params.get("replace-string", None))
    cargs.append(params.get("output-file", None))
    if params.get("case-insensitive", False):
        cargs.append("-case-insensitive")
    return cargs


def metadata_string_replace_outputs(
    params: MetadataStringReplaceParamsDict,
    execution: Execution,
) -> MetadataStringReplaceOutputs:
    """
    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 = MetadataStringReplaceOutputs(
        root=execution.output_file("."),
    )
    return ret


def metadata_string_replace_execute(
    params: MetadataStringReplaceParamsDict,
    runner: Runner | None = None,
) -> MetadataStringReplaceOutputs:
    """
    REPLACE A STRING IN ALL METADATA OF A FILE.
    
    Replaces all occurrences of <find-string> in the metadata and map names of
    <input-file> with <replace-string>.
    
    Args:
        params: The parameters.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetadataStringReplaceOutputs`).
    """
    metadata_string_replace_validate(params)
    runner = runner or get_global_runner()
    execution = runner.start_execution(METADATA_STRING_REPLACE_METADATA)
    params = execution.params(params)
    cargs = metadata_string_replace_cargs(params, execution)
    ret = metadata_string_replace_outputs(params, execution)
    execution.run(cargs)
    return ret


def metadata_string_replace(
    input_file: str,
    find_string: str,
    replace_string: str,
    output_file: str,
    case_insensitive: bool = False,
    runner: Runner | None = None,
) -> MetadataStringReplaceOutputs:
    """
    REPLACE A STRING IN ALL METADATA OF A FILE.
    
    Replaces all occurrences of <find-string> in the metadata and map names of
    <input-file> with <replace-string>.
    
    Args:
        input_file: the file to replace metadata in.
        find_string: the string to find.
        replace_string: the string to replace <find-string> with.
        output_file: output - the name to save the modified file as.
        case_insensitive: match with case variation also.
        runner: Command runner.
    Returns:
        NamedTuple of outputs (described in `MetadataStringReplaceOutputs`).
    """
    params = metadata_string_replace_params(
        input_file=input_file,
        find_string=find_string,
        replace_string=replace_string,
        output_file=output_file,
        case_insensitive=case_insensitive,
    )
    return metadata_string_replace_execute(params, runner)


__all__ = [
    "METADATA_STRING_REPLACE_METADATA",
    "MetadataStringReplaceOutputs",
    "MetadataStringReplaceParamsDict",
    "MetadataStringReplaceParamsDictTagged",
    "metadata_string_replace",
    "metadata_string_replace_execute",
    "metadata_string_replace_params",
]
