import QtQuick
import Quickshell
import ZShell.Config
import ZShell.Llm
import qs.Components
import qs.Services

Column {
	id: root

	required property var blocks

	spacing: Tokens.spacing.small

	Repeater {
		id: blockRep

		model: ScriptModel {
			values: root.blocks
			objectProp: "id"
		}
		delegate: DelegateChooser {
			role: "type"

			DelegateChoice {
				roleValue: LlmMarkdown.Type.Code

				delegate: CodeBlockView {
					required property var modelData

					language: modelData.language
					code: modelData.code
					anchors.right: parent.right
					anchors.margins: Tokens.spacing.small
					anchors.left: parent.left
				}
			}

			DelegateChoice {
				roleValue: LlmMarkdown.Type.Math

				delegate: MathBlockView {
					required property var modelData

					latex: modelData.latex
					anchors.right: parent.right
					anchors.left: parent.left
					anchors.margins: Tokens.padding.medium
				}
			}

			DelegateChoice {
				roleValue: LlmMarkdown.Type.Heading

				delegate: TextEditBase {
					required property var modelData

					color: Colors.palette.m3onSurface
					anchors.right: parent.right
					anchors.left: parent.left
					anchors.margins: Tokens.padding.medium
					text: modelData.text
					textFormat: Text.MarkdownText
					readOnly: true
					font.bold: true
					font.pointSize: {
						if (modelData.level <= 1)
							return Tokens.font.size.large;
						if (modelData.level === 2)
							return Tokens.font.size.normal;
						return Tokens.font.size.smaller;
					}
					wrapMode: Text.WrapAtWordBoundaryOrAnywhere
				}
			}

			DelegateChoice {
				roleValue: LlmMarkdown.Type.Text

				delegate: BubbleEdit {
					required property var modelData

					anchors.right: parent.right
					anchors.left: parent.left
					anchors.margins: Tokens.padding.medium
					text: modelData.text
				}
			}
		}
	}
}
