package tprint

import (
	"strings"

	"git.tordarus.net/tordarus/slices"
)

func FormatHeaderTable(header string, table *Table) string {
	return formatHeaderTableStr(header, table.String())
}

func FormatHeaderTableNoHead(header string, table *Table) string {
	return formatHeaderTableStr(header, table.StringNoHead())
}

// TODO formatHeaderTableStr is a quick hack
// a better solution would be to support nested tables.
// in that case, a header table is just a table with a singe col, single header row and single data row

func formatHeaderTableStr(header string, tab string) string {
	b := new(strings.Builder)

	splits := strings.Split(tab, "\n")
	tabwidth := strLen(splits[0])

	b.WriteRune('┏')
	b.WriteString(strings.Repeat("━", tabwidth-2))
	b.WriteString("┓\n┃")
	b.WriteString(padStringCenter(header, ' ', tabwidth-2))
	b.WriteString("┃\n")

	b.WriteRune('┣')
	b.WriteString(splits[0][3 : len(splits[0])-3])
	b.WriteString("┫\n")

	slices.Each(splits[1:], func(s string) {
		b.WriteString(s)
		b.WriteRune('\n')
	})

	ret := b.String()
	return ret[:len(ret)-1]
}