#
# Copyright (c) Daniel Sheffield 2023
#
# All rights reserved
#
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
from typing import Callable, Tuple

def to_numbered_field(x: Tuple[str, str]) -> Tuple[str, int, str]:
    if len(x[0].split('#', 1)) > 1:
        name, idx = x[0].split('#', 1)
        idx = int(idx)
    else:
        name, idx = x[0], 0

    return name, int(idx), x[1]

def to_unnumbered_field(x: Tuple[str, int, str]) -> Tuple[str, str]:
    return x[0], x[2]

def in_same_row(name: str) -> Callable[[Tuple[str, int, str]], bool]:
    if len(name.split('#', 1)) > 1:
        _, row = name.split('#', 1)
    else:
        row = 0
    return lambda x: x[1] == int(row)

def to_named_value(name: str) -> Callable[[Tuple[int, str]], Tuple[str, str]]:
    return lambda e: (f'{name}#{e[0]}', e[1])
