/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2026 Hurry Curry! Contributors

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/

use crate::load_ini;
use anyhow::Result;
use serde_json::to_string;
use std::{
    collections::BTreeMap,
    fs::File,
    io::Write,
    path::{Path, PathBuf},
};

pub fn export_json_pack(output: &Path, inputs: &[PathBuf]) -> Result<()> {
    let mut o = BTreeMap::<String, BTreeMap<String, String>>::new();
    for f in inputs {
        let locale = f
            .file_name()
            .unwrap()
            .to_str()
            .unwrap()
            .split_once(".")
            .unwrap()
            .0;
        for (k, v) in load_ini(f)? {
            o.entry(k).or_default().insert(locale.to_owned(), v);
        }
    }
    File::create(output)?.write_all(to_string(&o)?.as_bytes())?;
    Ok(())
}
