package dev.fyloz.plannervio.ui.i18n

import java.util.*

/**
 * A mutable ResourceBundle containing one or more others ResourceBundle.
 *
 * @param initBundles The bundles contained by the merged bundle
 */
class MergedResourceBundle(vararg initBundles: ResourceBundle) : ResourceBundle() {
    private val bundles: MutableList<ResourceBundle>

    init {
        bundles = initBundles.toMutableList()
    }

    /**
     * Adds a bundle to contained bundles.
     *
     * @param bundle The bundle to add
     */
    operator fun plus(bundle: ResourceBundle) {
        bundles + bundle
    }

    /**
     * Removes a bundle from contained bundles.
     *
     * @param bundle The bundle to remove
     */
    operator fun minus(bundle: ResourceBundle) {
        bundles - bundle
    }

    override fun handleGetObject(key: String): Any? {
        return bundles.firstOrNull { it.containsKey(key) }?.getObject(key)
    }

    override fun getKeys(): Enumeration<String> {
        return Collections.enumeration(bundles.flatMap { Collections.list(it.keys) })
    }

}
