package model

import (
	"errors"
	"fmt"
	"go-nkode/util"
)

func SelectKeyByAttrIdx(interfaceUser []int, passcodeIdxs []int, keypadSize KeypadDimension) ([]int, error) {
	selectedKeys := make([]int, len(passcodeIdxs))
	for idx := range passcodeIdxs {
		attrIdx := util.IndexOf[int](interfaceUser, passcodeIdxs[idx])
		keyNumb := attrIdx / keypadSize.AttrsPerKey
		if keyNumb < 0 || 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
}
