from typing import Iterable


def csv_mkln(
		*args,
		delim=',',
		quote='\"',
		esc='\\'):
	"""
	Formats a CSV row that can be written to a CSV file to be
	reloaded later. The result of this function can be passed
	to `csv_parseln` to parse it back into a list of strings.

	:param args
		The list of cells to format into a CSV row ready for
		output to an external medium. If args is a list of
		a single value, being a list, then that list will
		be used as the cells for the row.

	:return str
		The string representing the formatted CSV row.

	If `delim` is None or `quote` is None or `esc` is None, this function throws
	a ValueError.

	If len(`delim`) != 1 or len(`quote`) != 1 or len(`esc`) != 1, this function
	"""
	if delim is None or quote is None or esc is None:
		raise ValueError("delim, quote, and/or esc cannot be None")
	
	if len(delim) != 1 and len(quote) != 1 and len(esc) != 1:
		raise ValueError("len of delim, quote, and esc must be 1")

	if len(args) == 1 and isinstance(args[0], Iterable):
		return csv_mkln(*args[0])

	def _format_cell(raw):
		return quote + str(raw).replace(quote, esc + quote) + quote

	return delim.join([_format_cell(x) for x in args])