package terminfo

import (
	
)

const (
	// maxFileLength is the max file length.
	maxFileLength = 4096
	// magic is the file magic for terminfo files.
	magic = 0o432
	// magicExtended is the file magic for terminfo files with the extended
	// number format.
	magicExtended = 0o1036
)

// header fields.
const (
	fieldMagic = iota
	fieldNameSize
	fieldBoolCount
	fieldNumCount
	fieldStringCount
	fieldTableSize
)

// header extended fields.
const (
	fieldExtBoolCount = iota
	fieldExtNumCount
	fieldExtStringCount
	fieldExtOffsetCount
	fieldExtTableSize
)

// hasInvalidCaps determines if the capabilities in h are invalid.
func ( []int) bool {
	return [fieldBoolCount] > CapCountBool ||
		[fieldNumCount] > CapCountNum ||
		[fieldStringCount] > CapCountString
}

// capLength returns the total length of the capabilities in bytes.
func ( []int) int {
	return [fieldNameSize] +
		[fieldBoolCount] +
		([fieldNameSize]+[fieldBoolCount])%2 + // account for word align
		[fieldNumCount]*2 +
		[fieldStringCount]*2 +
		[fieldTableSize]
}

// hasInvalidExtOffset determines if the extended offset field is valid.
func ( []int) bool {
	return [fieldExtBoolCount]+
		[fieldExtNumCount]+
		[fieldExtStringCount]*2 != [fieldExtOffsetCount]
}

// extCapLength returns the total length of extended capabilities in bytes.
func ( []int,  int) int {
	return [fieldExtBoolCount] +
		[fieldExtBoolCount]%2 + // account for word align
		[fieldExtNumCount]*(/8) +
		[fieldExtOffsetCount]*2 +
		[fieldExtTableSize]
}

// findNull finds the position of null in buf.
func ( []byte,  int) int {
	for ;  < len(); ++ {
		if [] == 0 {
			return 
		}
	}
	return -1
}

// readStrings decodes n strings from string data table buf using the indexes in idx.
func ( []int,  []byte,  int) (map[int][]byte, int, error) {
	var  int
	 := make(map[int][]byte)
	for  := 0;  < ; ++ {
		 := []
		if  < 0 {
			continue
		}
		if  := findNull(, );  != -1 {
			[],  = [:], +1
		} else {
			return nil, 0, ErrInvalidStringTable
		}
	}
	return , , nil
}

// decoder holds state info while decoding a terminfo file.
type decoder struct {
	buf []byte
	pos int
	n   int
}

// readBytes reads the next n bytes of buf, incrementing pos by n.
func ( *decoder) ( int) ([]byte, error) {
	if .n < .pos+ {
		return nil, ErrUnexpectedFileEnd
	}
	, .pos = .pos, .pos+
	return .buf[:.pos], nil
}

// readInts reads n number of ints with width w.
func ( *decoder) (,  int) ([]int, error) {
	 /= 8
	 :=  * 
	,  := .readBytes()
	if  != nil {
		return nil, 
	}
	// align
	.pos += .pos % 2
	 := make([]int, )
	for ,  := 0, 0;  < ; ,  = +, +1 {
		switch  {
		case 1:
			[] = int([])
		case 2:
			[] = int(int16([+1])<<8 | int16([]))
		case 4:
			[] = int([+3])<<24 | int([+2])<<16 | int([+1])<<8 | int([])
		}
	}
	return , nil
}

// readBools reads the next n bools.
func ( *decoder) ( int) (map[int]bool, map[int]bool, error) {
	,  := .readInts(, 8)
	if  != nil {
		return nil, nil, 
	}
	// process
	,  := make(map[int]bool), make(map[int]bool)
	for ,  := range  {
		[] =  == 1
		if int8() == -2 {
			[] = true
		}
	}
	return , , nil
}

// readNums reads the next n nums.
func ( *decoder) (,  int) (map[int]int, map[int]bool, error) {
	,  := .readInts(, )
	if  != nil {
		return nil, nil, 
	}
	// process
	,  := make(map[int]int), make(map[int]bool)
	for  := 0;  < ; ++ {
		[] = []
		if [] == -2 {
			[] = true
		}
	}
	return , , nil
}

// readStringTable reads the string data for n strings and the accompanying data
// table of length sz.
func ( *decoder) (,  int) ([][]byte, []int, error) {
	,  := .readInts(, 16)
	if  != nil {
		return nil, nil, 
	}
	// read string data table
	,  := .readBytes()
	if  != nil {
		return nil, nil, 
	}
	// align
	.pos += .pos % 2
	// process
	 := make([][]byte, )
	var  []int
	for  := 0;  < ; ++ {
		 := []
		if  == -2 {
			 = append(, )
		} else if  >= 0 {
			if  := findNull(, );  != -1 {
				[] = [:]
			} else {
				return nil, nil, ErrInvalidStringTable
			}
		}
	}
	return , , nil
}

// readStrings reads the next n strings and processes the string data table of
// length sz.
func ( *decoder) (,  int) (map[int][]byte, map[int]bool, error) {
	, ,  := .readStringTable(, )
	if  != nil {
		return nil, nil, 
	}
	 := make(map[int][]byte)
	for ,  := range  {
		if  == AcsChars {
			 = canonicalizeAscChars()
		}
		[] = 
	}
	 := make(map[int]bool, len())
	for ,  := range  {
		[] = true
	}
	return , , nil
}

// canonicalizeAscChars reorders chars to be unique, in order.
//
// see repair_ascc in ncurses-6.3/progs/dump_entry.c
func ( []byte) []byte {
	var  []byte
	 := make(map[byte]byte, len()/2)
	for  := 0;  < len();  += 2 {
		if ,  := [[]]; ! {
			,  := [], [+1]
			// log.Printf(">>> a: %d %c, b: %d %c", a, a, b, b)
			, [] = append(, ), 
		}
	}
	sort.Slice(, func(,  int) bool {
		return [] < []
	})
	 := make([]byte, 2*len())
	for  := 0;  < len(); ++ {
		[*2], [*2+1] = [], [[]]
	}
	return 
}