package d2scenebuild

import (
	

	
	
	
	libcolor 
	
	
)

const (
	positionedTooltipPadding  = 10
	positionedTooltipRadius   = 4
	positionedTooltipTailSize = 8
)

// buildPositionedTooltip appends visible tooltips after diagram objects so they
// remain above all z-indexed shapes and connections.
func ( *builder) ( d2target.Shape) (*d2scene.Node, error) {
	if .Tooltip == "" || .TooltipPosition == "" {
		return nil, nil
	}
	 := fmt.Sprintf("shape %q positioned tooltip", .ID)
	,  := .markdownFontFamilies(.FontFamily)
	,  := .markdownLayoutRuler()
	if  != nil {
		return nil, fmt.Errorf("scene: %s Markdown ruler: %w", , )
	}
	,  := textmeasure.LayoutMarkdown(.Tooltip, , &, &, d2fonts.FONT_SIZE_M)
	if  != nil {
		return nil, fmt.Errorf("scene: %s Markdown layout: %w", , )
	}
	if len(.Primitives) > maxMarkdownPrimitives {
		return nil, fmt.Errorf("scene: %s Markdown primitive count %d exceeds limit %d", , len(.Primitives), maxMarkdownPrimitives)
	}
	if  := .ctx.Err();  != nil {
		return nil, 
	}
	 := .Width + 2*positionedTooltipPadding
	 := .Height + 2*positionedTooltipPadding
	,  := d2target.CalculateTooltipPosition(
		float64(.Pos.X), float64(.Pos.Y),
		float64(.Width), float64(.Height),
		, , .TooltipPosition,
	)
	if !finite() || !finite() ||  <= 0 ||  <= 0 {
		return nil, invalidField(, "bounds", nil, "must resolve to finite positive dimensions")
	}

	,  := .paint(libcolor.N7, +" background")
	if  != nil {
		return nil, 
	}
	,  := .paint(libcolor.N5, +" border")
	if  != nil {
		return nil, 
	}
	 := &d2scene.Stroke{
		Paint: , Width: 1,
		Cap: d2scene.CapButt, Join: d2scene.JoinMiter, MiterLimit: 4,
	}
	 := d2scene.NewNode(nil)
	.ID = .ID + ":positioned-tooltip"
	 := d2scene.Box{X: , Y: , Width: float64(), Height: float64()}
	 := d2scene.NewNode(d2scene.Rect{
		Box: , RadiusX: positionedTooltipRadius, RadiusY: positionedTooltipRadius,
		Fill: , Stroke: ,
	})
	.ID = .ID + ":background"
	.Children = append(.Children, )

	,  := positionedTooltipTail(.TooltipPosition, , , )
	if  != nil {
		return nil, fmt.Errorf("scene: %s tail: %w", , )
	}
	.ID = .ID + ":tail"
	.Children = append(.Children, )

	,  := .buildMarkdownLabel(
		, .ID+":markdown", .Tooltip, .FontFamily,
		d2fonts.FONT_SIZE_M, .Width, .Height,
		libcolor.N1, "", false, false,
		geo.NewPoint(+positionedTooltipPadding, +positionedTooltipPadding),
	)
	if  != nil {
		return nil, 
	}
	.Children = append(.Children, )
	return , nil
}

func positionedTooltipTail( string,  d2scene.Box,  d2scene.Paint,  *d2scene.Stroke) (*d2scene.Node, error) {
	,  := .X+.Width/2, .Y+.Height
	 := "bottom"
	switch  {
	case "top-left":
		,  = .X+20, .Y+.Height
	case "top-center":
	case "top-right":
		,  = .X+.Width-20, .Y+.Height
	case "center-left":
		, ,  = "right", .X+.Width, .Y+.Height/2
	case "center-right":
		, ,  = "left", .X, .Y+.Height/2
	case "bottom-left":
		, ,  = "top", .X+20, .Y
	case "bottom-center":
		, ,  = "top", .X+.Width/2, .Y
	case "bottom-right":
		, ,  = "top", .X+.Width-20, .Y
	}
	 := float64(positionedTooltipTailSize)
	var  []d2scene.PathCommand
	switch  {
	case "top":
		 = []d2scene.PathCommand{
			d2scene.MoveTo(-/2, ), d2scene.LineTo(+/2, ),
			d2scene.LineTo(, -), d2scene.ClosePath(),
		}
	case "bottom":
		 = []d2scene.PathCommand{
			d2scene.MoveTo(-/2, ), d2scene.LineTo(+/2, ),
			d2scene.LineTo(, +), d2scene.ClosePath(),
		}
	case "left":
		 = []d2scene.PathCommand{
			d2scene.MoveTo(, -/2), d2scene.LineTo(, +/2),
			d2scene.LineTo(-, ), d2scene.ClosePath(),
		}
	case "right":
		 = []d2scene.PathCommand{
			d2scene.MoveTo(, -/2), d2scene.LineTo(, +/2),
			d2scene.LineTo(+, ), d2scene.ClosePath(),
		}
	default:
		return nil, fmt.Errorf("unknown direction %q", )
	}
	return d2scene.NewNode(d2scene.Path{Commands: , Fill: , Stroke: }), nil
}