package table

// Data is the interface that wraps the basic methods of a table model.
type Data interface {
	// At returns the contents of the cell at the given index.
	At(row, cell int) string

	// Rows returns the number of rows in the table.
	Rows() int

	// Columns returns the number of columns in the table.
	Columns() int
}

// StringData is a string-based implementation of the Data interface.
type StringData struct {
	rows    [][]string
	columns int
}

// NewStringData creates a new StringData with the given number of columns.
func ( ...[]string) *StringData {
	 := StringData{columns: 0}

	for ,  := range  {
		.columns = max(.columns, len())
		.rows = append(.rows, )
	}

	return &
}

// Append appends the given row to the table.
func ( *StringData) ( []string) {
	.columns = max(.columns, len())
	.rows = append(.rows, )
}

// At returns the contents of the cell at the given index.
func ( *StringData) (,  int) string {
	if  >= len(.rows) ||  >= len(.rows[]) {
		return ""
	}

	return .rows[][]
}

// Columns returns the number of columns in the table.
func ( *StringData) () int {
	return .columns
}

// Item appends the given row to the table.
func ( *StringData) ( ...string) *StringData {
	.columns = max(.columns, len())
	.rows = append(.rows, )
	return 
}

// Rows returns the number of rows in the table.
func ( *StringData) () int {
	return len(.rows)
}

// Filter applies a filter on some data.
type Filter struct {
	data   Data
	filter func(row int) bool
}

// NewFilter initializes a new Filter.
func ( Data) *Filter {
	return &Filter{data: }
}

// Filter applies the given filter function to the data.
func ( *Filter) ( func( int) bool) *Filter {
	.filter = 
	return 
}

// At returns the row at the given index.
func ( *Filter) (,  int) string {
	 := 0
	for  := range .data.Rows() {
		if .filter() {
			if  ==  {
				return .data.At(, )
			}

			++
		}
	}

	return ""
}

// Columns returns the number of columns in the table.
func ( *Filter) () int {
	return .data.Columns()
}

// Rows returns the number of rows in the table.
func ( *Filter) () int {
	 := 0
	for  := range .data.Rows() {
		if .filter() {
			++
		}
	}

	return 
}

// DataToMatrix is a helper function that converts an object that implements the
// Data interface into a table.
func ( Data) ( [][]string) {
	 := .Rows()
	 := .Columns()
	 = make([][]string, )

	for  := range  {
		[] = make([]string, )

		for  := range  {
			[][] = .At(, )
		}
	}
	return
}