package entities

import (
	"errors"
	"fmt"
	"go-nkode/internal/security"
)

func SelectKeyByAttrIdx(interfaceUser []int, passcodeIdxs []int, keypadSize KeypadDimension) ([]int, error) {
	selectedKeys := make([]int, len(passcodeIdxs))
	for idx := range passcodeIdxs {
		attrIdx, err := security.IndexOf[int](interfaceUser, passcodeIdxs[idx])
		if err != nil {
			return nil, err
		}
		keyNumb := attrIdx / keypadSize.AttrsPerKey
		if keyNumb >= keypadSize.NumbOfKeys {
			return nil, errors.New(fmt.Sprintf("index key number: %d out of range 0-%d", keyNumb, keypadSize.NumbOfKeys-1))
		}
		selectedKeys[idx] = keyNumb
	}
	return selectedKeys, nil
}
