Using arrays with awk

I'm a little stuck and would be grateful of some advice!

I have three files, two of which contain reference data that I want to add to a line of output in the third file. I can't seem to get awk to print array contents as I would expect.

The input files are:

# Input file
AAA,OAA,0313
AAB,OAB,0314

# Ref file1
NAA,123
NAB,456

# Ref file2
0313,123
081A,456
0314,789

I need to match a line in Ref File2 based on the last field of the main input file. I then need to match the last field in Ref File2 to Ref File1.
The output should then be:

AAA,OAA,0313,123,NAA
AAB,OAB,0314,456,NAB

Currently my code doesn't quite work as the last field of my output is empty. I have tried numerous field numbers and $NF for the f1 array, but can't seem to get any output. My code is:

FILE1=reffile1
FILE2=reffile2
FILE3=inputfile
awk -F"," -v "file1=$FILE1" -v "file2=$FILE2" '
FILENAME == file1 {
        f1[$2] = $0
        next
}
FILENAME == file2 {
        f2[$1] = $2
        next
}
{
        if ( $NF in f2 ) {
                if ( f2[$NF] in f1 ) {   ## I'm not sure why $NF works here and $2 does not, but I seem to get the output I want..
                        print $0","f2[$NF]","f1[$1]  # This line does not produce the relevant output from the f1 array (I want field 1 to print)
                }
        }
}' $FILE1 $FILE2 $FILE3

Can anyone suggest why this is not working and how the above can be modified to work. I seem to be 95% of the way there, I just would like to understand why this is not working and how output is taken from an array.

Thanks.

awk -F, -v file1=mac1.txt -v file2=mac2.txt -f mac.awk OFS =, mac1.txt mac2.txt macI.txt
mac.awk:

FILENAME==file1 { f1[$2]=$1;next}
FILENAME==file2 { f2[$1]=$2;next}
$NF in f2 && f2[$NF] in f1 { $0=$0 OFS f2[$NF] OFS f1[f2[$NF]]}
1

Thanks for the response.

I forgot to mention I did also try f2[$2]=$1 and it still didn't work.

I'm not able to test this out at the moment and I sort of follow your suggestion:

$NF in f2 && f2[$NF] in f1 { $0=$0 OFS f2[$NF] OFS f1[f2[$NF]]}
1

Would you (or anyone else) mind commenting on why the above works and what I had written didn't, so I can understand where I was going wrong?
Thanks.