package form

import (
	"net/http"
	"strings"
)

type Cursor struct {
	PerPage    int
	Pagination bool
	Name       string
	Val        string
	Colspan    int
}

func (cursor *Cursor) FillValue(r *http.Request) {
	cursor.Val = strings.TrimSpace(r.FormValue(cursor.Name))
	cursor.Pagination = cursor.Val != ""
}

func (cursor *Cursor) Params() []string {
	return strings.Split(cursor.Val, ";")
}

func BuildCursor[K interface{}](cursor *Cursor, elems []K, build func(K) []string) []K {
	if len(elems) <= cursor.PerPage {
		cursor.Val = ""
		return elems
	}
	elems = elems[:cursor.PerPage]
	cursor.Val = strings.Join(build(elems[cursor.PerPage-1]), ";")
	return elems
}
