package com.github.nullptroma.wallenc.ui.resources

import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalResources
import androidx.compose.ui.res.stringResource

@Composable
fun composableUiStringResolver(): UiStringResolver {
    val resources = LocalResources.current
    return remember(resources) {
        object : UiStringResolver {
            override fun invoke(id: Int, vararg formatArgs: Any): String =
                if (formatArgs.isEmpty()) {
                    resources.getString(id)
                } else {
                    resources.getString(id, *formatArgs)
                }

            override fun plurals(id: Int, quantity: Int, vararg formatArgs: Any): String =
                if (formatArgs.isEmpty()) {
                    resources.getQuantityString(id, quantity)
                } else {
                    resources.getQuantityString(id, quantity, *formatArgs)
                }
        }
    }
}

@Composable
fun resolveString(@StringRes id: Int, vararg formatArgs: Any): String =
    if (formatArgs.isEmpty()) {
        stringResource(id)
    } else {
        stringResource(id, *formatArgs)
    }
