package door

import (
	"strconv"
	"strings"

	"golang.org/x/text/width"
)

// https://siongui.github.io/2016/03/23/go-utf8-string-width/
func UnicodeWidth(r rune) int {
	p := width.LookupRune(r)
	if p.Kind() == width.EastAsianWide {
		return 2
	}
	return 1
}

func ArrayDelete[T any](stack *[]T, pos int) (T, bool) {
	var result T
	/*
	   https://stackoverflow.com/questions/33834742/remove-and-adding-elements-to-array-in-go-lang
	   https://github.com/golang/go/wiki/SliceTricks
	*/
	if pos < 0 || pos > len(*stack) {
		return result, false
	}
	result = (*stack)[pos]
	copy((*stack)[pos:], (*stack)[pos+1:])
	// var temp T
	// (*stack)[len(*stack)-1] = temp
	*stack = (*stack)[:len(*stack)-1]
	return result, true
}

func ArrayPop[T any](stack *[]T, count int) bool {
	/*
	   https://stackoverflow.com/questions/33834742/remove-and-adding-elements-to-array-in-go-lang
	   https://github.com/golang/go/wiki/SliceTricks
	*/
	if count < 0 || count > len(*stack) {
		return false
	}

	copy((*stack)[0:], (*stack)[count:])
	// var temp T
	// (*stack)[len(*stack)-1] = temp
	*stack = (*stack)[:len(*stack)-count]
	return true
}

func SplitToInt(input string, sep string) []int {
	var result []int
	for _, number := range strings.Split(input, sep) {
		v, err := strconv.Atoi(number)
		if err == nil {
			result = append(result, v)
		}
	}
	return result
}