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> = initBundles.toMutableList()

    /** Adds a [bundle] to the contained bundles. */
    operator fun plus(bundle: ResourceBundle) {
        bundles += bundle
    }

    /** Adds [bundles] to the contained bundles. */
    operator fun plus(bundles: Iterable<ResourceBundle>) {
        this.bundles += bundles
    }

    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) })
    }

}
