//! Embedded precomputed assets and the lookup that serves them.

use crate::kind::OutputKind;
use crate::version::EmojiVersion;

/// The eight assets for one version, indexed by [`OutputKind::ALL`] order.
type AssetSet = [&'static str; 8];

macro_rules! version_assets {
    ($dir:literal) => {
        [
            include_str!(concat!("../assets/", $dir, "/index.txt")),
            include_str!(concat!("../assets/", $dir, "/index-strings.txt")),
            include_str!(concat!("../assets/", $dir, "/css.txt")),
            include_str!(concat!("../assets/", $dir, "/javascript.txt")),
            include_str!(concat!("../assets/", $dir, "/javascript-u.txt")),
            include_str!(concat!("../assets/", $dir, "/javascript-v.txt")),
            include_str!(concat!("../assets/", $dir, "/java.txt")),
            include_str!(concat!("../assets/", $dir, "/cpp-re2.txt")),
        ]
    };
}

const V13_0: AssetSet = version_assets!("emoji-13.0");
const V13_1: AssetSet = version_assets!("emoji-13.1");
const V14_0: AssetSet = version_assets!("emoji-14.0");
const V15_0: AssetSet = version_assets!("emoji-15.0");
const V15_1: AssetSet = version_assets!("emoji-15.1");
const V16_0: AssetSet = version_assets!("emoji-16.0");
const V17_0: AssetSet = version_assets!("emoji-17.0");

const fn kind_index(kind: OutputKind) -> usize {
    match kind {
        OutputKind::Index => 0,
        OutputKind::IndexStrings => 1,
        OutputKind::Css => 2,
        OutputKind::JavaScript => 3,
        OutputKind::JavaScriptU => 4,
        OutputKind::JavaScriptV => 5,
        OutputKind::Java => 6,
        OutputKind::CppRe2 => 7,
    }
}

const fn asset_set(version: EmojiVersion) -> &'static AssetSet {
    match version.resolve() {
        EmojiVersion::V13_0 => &V13_0,
        EmojiVersion::V13_1 => &V13_1,
        EmojiVersion::V14_0 => &V14_0,
        EmojiVersion::V15_0 => &V15_0,
        EmojiVersion::V15_1 => &V15_1,
        EmojiVersion::V16_0 => &V16_0,
        // `resolve` maps `Latest` to the highest version, so this arm is dead.
        // It stays for exhaustiveness and shares the highest version's assets.
        EmojiVersion::V17_0 | EmojiVersion::Latest => &V17_0,
    }
}

/// Return the embedded asset for a version and kind.
///
/// [`EmojiVersion::Latest`] resolves to the highest version, so it returns the
/// same bytes as that version.
#[must_use]
pub const fn output(version: EmojiVersion, kind: OutputKind) -> &'static str {
    asset_set(version)[kind_index(kind)]
}
