# Copyright (c) 2020, InfluxFramework LLC
# License: MIT. See LICENSE

import influxframework


def validate_route_conflict(doctype, name):
	"""
	Raises exception if name clashes with routes from other documents for /app routing
	"""

	all_names = []
	for _doctype in ["Page", "Workspace", "DocType"]:
		try:
			all_names.extend(
				[
					slug(d) for d in influxframework.get_all(_doctype, pluck="name") if (doctype != _doctype and d != name)
				]
			)
		except influxframework.db.TableMissingError:
			pass

	if slug(name) in all_names:
		influxframework.msgprint(influxframework._("Name already taken, please set a new name"))
		raise influxframework.NameError


def slug(name):
	return name.lower().replace(" ", "-")
