# Copyright (c) 2015, JINGROW
# License: MIT. See LICENSE

import jingrow
from jingrow.model.page import Page


class DynamicLink(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.types import DF

		link_pagetype: DF.Link
		link_name: DF.DynamicLink
		link_title: DF.ReadOnly | None
		parent: DF.Data
		parentfield: DF.Data
		parenttype: DF.Data
	# end: auto-generated types
	pass


def on_pagetype_update():
	jingrow.db.add_index("Dynamic Link", ["link_pagetype", "link_name"])


def deduplicate_dynamic_links(pg):
	links, duplicate = [], False
	for l in pg.links or []:
		t = (l.link_pagetype, l.link_name)
		if t not in links:
			links.append(t)
		else:
			duplicate = True

	if duplicate:
		pg.links = []
		for l in links:
			pg.append("links", dict(link_pagetype=l[0], link_name=l[1]))
