package common

import (
	"strings"

	"github.com/junegunn/fzf/src/algo"
	"github.com/junegunn/fzf/src/util"
)

func init() {
	algo.Init("default")
}

func FuzzyScore(input, target string, exact bool) (int32, []int32, int32) {
	chars := util.ToChars([]byte(target))

	var res algo.Result
	var pos *[]int

	if exact {
		runes := algo.NormalizeRunes([]rune(input))
		res, pos = algo.ExactMatchNaive(true, true, true, &chars, runes, true, nil)
	} else {
		runes := algo.NormalizeRunes([]rune(strings.ToLower(input)))
		res, pos = algo.FuzzyMatchV2(false, true, true, &chars, runes, true, nil)
	}

	var int32Slice []int32

	if pos != nil {
		intSlice := *pos
		int32Slice = make([]int32, len(intSlice))

		for i, v := range intSlice {
			int32Slice[i] = int32(v)
		}
	} else {
		int32Slice = make([]int32, 0)
	}

	if res.Start > -1 {
		res.Score = res.Score - res.Start
	}

	return int32(res.Score), int32Slice, int32(res.Start)
}
