// Build a map of all card front images using webpack's require.context
import CARD_BACK from '../assets/card-back.png'

// require all png files in cards-front directory
const req = require.context('../assets/cards-front', false, /\.png$/)

const cardMap = {}
req.keys().forEach((key) => {
    // key is like './5R.png' -> strip './' and '.png'
    const code = key.replace('./', '').replace('.png', '')
    try {
        const resolved = req(key)
        cardMap[code] = resolved && resolved.default ? resolved.default : resolved
    } catch (e) {
        cardMap[code] = CARD_BACK
    }
})

export const getCardImage = (code) => {
    if (!code) return CARD_BACK
    return cardMap[code] || CARD_BACK
}

export default {
    getCardImage,
}
