/*
 * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
 */

#include <keel/keel.hpp>

namespace keel {

static ox::Error init(
		Context &ctx,
		ox::UPtr<ox::FileSystem> &&fs,
		ox::StringViewCR appName,
		DuplicateSet *duplicateSet) noexcept {
	ctx.appName = appName;
	std::ignore = duplicateSet ?
		setRomFs(ctx, std::move(fs), *duplicateSet) :
		setRomFs(ctx, std::move(fs));
#ifndef OX_BARE_METAL
	auto const &mods = modules();
	for (auto &mod : mods) {
		// register type converters
		for (auto const c : mod->converters()) {
			ctx.converters.emplace_back(c);
		}
		// register pack transforms
		for (auto const c : mod->packTransforms()) {
			ctx.packTransforms.emplace_back(c);
		}
	}
#endif
	return {};
}

ox::Error init(
		Context &ctx,
		ox::UPtr<ox::FileSystem> &&fs,
		ox::StringViewCR appName,
		DuplicateSet &duplicateSet) noexcept {
	return init(ctx, std::move(fs), appName, &duplicateSet);
}

ox::Result<ox::UPtr<Context>> init(
	ox::UPtr<ox::FileSystem> &&fs,
	ox::StringViewCR appName,
	DuplicateSet &duplicateSet) noexcept {
	auto ctx = ox::make_unique<Context>();
	OX_RETURN_ERROR(keel::init(*ctx, std::move(fs), appName, &duplicateSet));
	return ctx;
}

ox::Error init(
		Context &ctx,
		ox::UPtr<ox::FileSystem> &&fs,
		ox::StringViewCR appName) noexcept {
	return init(ctx, std::move(fs), appName, nullptr);
}

ox::Result<ox::UPtr<Context>> init(ox::UPtr<ox::FileSystem> &&fs, ox::StringViewCR appName) noexcept {
	auto ctx = ox::make_unique<Context>();
	OX_RETURN_ERROR(keel::init(*ctx, std::move(fs), appName));
	return ctx;
}

}
