from .cli_bootstrap import check_dict, check_list

from typing import (
	Any,
	Mapping,
	MutableMapping,
	Iterable,
)


def toml_add_overlay(
	toml: Any,
	overlay: Any,
) -> Any:
	if isinstance(toml, MutableMapping):
		overlay2 = check_dict(
			overlay,
			str,
		)

		for k, v in overlay2.items():
			if not k in toml:
				toml[k] = v
			elif isinstance(toml[k], MutableMapping) and isinstance(v, Mapping):
				toml_add_overlay(
					toml[k],
					v,
				)
			elif isinstance(toml[k], list) and isinstance(v, list):
				check_list(toml[k], str)
				check_list(v, str)
				toml[k] = v
			else:
				raise NotImplementedError
	else:
		raise NotImplementedError
