/*
 * Mirai. Copyright (C) 2024 Vyn
 * This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
 * The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
 */

#include "ConfigImpl.h"
#include "nlohmann/json.hpp"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

namespace mirai
{
ConfigImpl::ConfigImpl(const std::string &path) : path_(path)
{
	std::ifstream file(path_);
	if (!file) {
		configJson_ = nlohmann::json::parse(R"(
			{
				"files": []
			}
		)");
		return;
	}
	configJson_ = nlohmann::json::parse(file);
}

std::vector<std::string> ConfigImpl::sources() const
{
	std::vector<std::string> sources;
	assert(configJson_.is_object());
	assert(configJson_["files"].is_array());
	auto jsonSources = configJson_["files"];
	for (const auto &filePath : jsonSources) {
		assert(filePath.is_string());
		sources.push_back(filePath.get<std::string>());
	}
	return sources;
}

std::string ConfigImpl::path() const
{
	return path_;
}

}; // namespace mirai
