# Tests that complex schema definitions with multiple type aliases, comprehensions,
# and nested struct unification evaluate correctly in the v3 evaluator. The issue
# reported that evalv3 produced validation errors for code accepted by evalv2.
# https://cuelang.org/issue/3805
-- x.cue --
#PatientViewSections: {
	[string]: #PatientViewSection
}

#PatientViewSection: #PvSection @test(shareID=pvSection)

#PvSection: {
	@test(shareID=pvSection)
	Section: {
		display?: string
		entries:  #PatientViewEntries
	}
	_json: [for k, v in Section.entries {(k): v._json}]
}

#PatientViewEntries: [string]: #PatientViewEntry

#PatientViewEntry: {
	#PvSingleForm | #PvLink
}

#PvSingleForm: {
	SingleForm: {
		formId:                 string
		showNumberOfInstances?: bool
	}
	_json: [{
		"iDoc":     "singleFormIDoc"
		"X-FormId": SingleForm.formId
	}]
}

#PvLink: {
	Link: {
		icon?:    string
		sections: #PatientViewSections
	}
	_json: [for k, v in Link.sections {(k): v._json}]
}

#TabcatsJson: {
	config: {
		tabcats: #PatientViewSections
		...
	}
	json: {
		//tabcats: #OrgContents
		tabcats: [for k, v in config.tabcats {(k): v._json}]
	}
}

tabcatsExample: #TabcatsJson
tabcatsExample: {
	_e1: #PatientViewEntry
	_e1: {
		Link: {
			sections: "Formulare": {
				Section: entries: "Fotodokumentation": _e2
			}
		}
	}
	_e2: #PatientViewEntry
	_e2: {
		SingleForm: {
			formId: "fotodokumentation"
		}
	}
	_s: #PvSection
	_s: Section: entries: "Dokumentation": _e1
	config: {
		tabcats: "02": _s
	}
	@test(eq, {
		_e1: {
			Link: {
				sections: {
					Formulare: {
						Section: {
							entries: {
								Fotodokumentation: {
									SingleForm: {
										formId: "fotodokumentation"
										showNumberOfInstances?: bool
									}
									_json: [{
										iDoc: "singleFormIDoc"
										"X-FormId": "fotodokumentation"
									}]
								}
							}
							display?: string
						}
						_json: [{
							Fotodokumentation: [{
								iDoc: "singleFormIDoc"
								"X-FormId": "fotodokumentation"
							}]
						}]
					}
				}
				icon?: string
			}
			_json: [{
				Formulare: [{
					Fotodokumentation: [{
						iDoc: "singleFormIDoc"
						"X-FormId": "fotodokumentation"
					}]
				}]
			}]
		}
		_e2: {
			SingleForm: {
				formId: "fotodokumentation"
				showNumberOfInstances?: bool
			}
			_json: [{
				iDoc: "singleFormIDoc"
				"X-FormId": "fotodokumentation"
			}]
		}
		_s: {
			Section: {
				entries: Dokumentation: {
					Link: {
						sections: Formulare: {
							Section: {
								entries: Fotodokumentation: {
									SingleForm: {
										formId: "fotodokumentation"
										showNumberOfInstances?: bool
									}
									_json: [{
										iDoc: "singleFormIDoc"
										"X-FormId": "fotodokumentation"
									}]
								}
								display?: string
							}
							_json: [{
								Fotodokumentation: [{
									iDoc: "singleFormIDoc"
									"X-FormId": "fotodokumentation"
								}]
							}]
						}
						icon?: string
					}
					_json: [{
						Formulare: [{
							Fotodokumentation: [{
								iDoc: "singleFormIDoc"
								"X-FormId": "fotodokumentation"
							}]
						}]
					}]
				}
				display?: string
			}
			_json: [{
				Dokumentation: [{
					Formulare: [{
						Fotodokumentation: [{
							iDoc: "singleFormIDoc"
							"X-FormId": "fotodokumentation"
						}]
					}]
				}]
			}]
		}
		config: tabcats: "02": {
			Section: {
				entries: {
					Dokumentation: {
						Link: {
							sections: Formulare: {
								Section: {
									entries: Fotodokumentation: {
										SingleForm: {
											formId: "fotodokumentation"
											showNumberOfInstances?: bool
										}
										_json: [{
											iDoc: "singleFormIDoc"
											"X-FormId": "fotodokumentation"
										}]
									}
									display?: string
								}
								_json: [{
									Fotodokumentation: [{
										iDoc: "singleFormIDoc"
										"X-FormId": "fotodokumentation"
									}]
								}]
							}
							icon?: string
						}
						_json: [{
							Formulare: [{
								Fotodokumentation: [{
									iDoc: "singleFormIDoc"
									"X-FormId": "fotodokumentation"
								}]
							}]
						}]
					}
				}
				display?: string
			}
			_json: [{
				Dokumentation: [{ Formulare: [{
					Fotodokumentation: [{
						iDoc: "singleFormIDoc"
						"X-FormId": "fotodokumentation"
					}]
				}]
			}] }]
		}
		json: tabcats: [{ "02": [{ Dokumentation: [{ Formulare: [{
				Fotodokumentation: [{
					iDoc: "singleFormIDoc"
					"X-FormId": "fotodokumentation"
				}]
			}]
		}] }] }]
	})
}
-- out/eval/stats --
Leaks:  77
Freed:  162
Reused: 129
Allocs: 110
Retain: 0

Unifications: 171
Conjuncts:    284
Disjuncts:    16

NumCloseIDs: 57

ConjunctInfos:       243
MaxConjunctInfos:    5
MaxReqSets:          14
MaxRedirect:         3
-- out/todo.txt --
v3 uses structure sharing (~) for nested _json fields, v2 expands them; field ordering also differs.
#PatientViewSection: v3 uses ~(#PvSection), v2 expands it.
