# Copyright (c) 2025, JINGROW
# For license information, please see license.txt

import jingrow
from jingrow.model.document import Page


class ActionStep(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

		method: DF.Data | None
		output: DF.Code | None
		parent: DF.Data
		parentfield: DF.Data
		parenttype: DF.Data
		reference_pagetype: DF.Link | None
		reference_name: DF.DynamicLink | None
		wait: DF.Check
	# end: auto-generated types

	def before_save(self):
		try:
			target_pg = jingrow.get_pg(self.reference_pagetype, self.reference_name)
		except jingrow.DoesNotExistError:
			jingrow.throw(
				f"Invalid reference: {self.reference_pagetype} {self.reference_name}",
				jingrow.DoesNotExistError,
			)

		if not hasattr(target_pg, self.method):
			jingrow.throw(
				f"Method '{self.method}' does not exist on document '{target_pg.name}'",
				jingrow.ValidationError,
			)
