#include "LatexSymbols.h"

#include "UnicodeUtils.h"

std::unordered_map<std::string, std::wstring> LatexSymbolLookup::mSymbols = { 
	{"Psi", L"\u03A8"},
	{"alpha", L"\u03B1"},
	{"beta", L"\u03B2"},
	{"gamma", L"\u03B3"},
	{"psi", L"\u03C8"},
	{"langle", L"\u27E8"},
	{"rangle", L"\u27E9"},
};

std::unordered_map<LatexMathSymbol::Tag, std::wstring> LatexSymbolLookup::mLeftSymbols = {
	{LatexMathSymbol::Tag::BRA, L"\u27E8"},
	{LatexMathSymbol::Tag::KET, L"\u007C"}
};

std::unordered_map<LatexMathSymbol::Tag, std::wstring> LatexSymbolLookup::mRightSymbols = {
	{LatexMathSymbol::Tag::BRA, L"\u007C"},
	{LatexMathSymbol::Tag::KET, L"\u27E9"}
};

std::unordered_map<std::string, LatexMathSymbol::Tag> LatexSymbolLookup::mTags = {
	{"frac", LatexMathSymbol::Tag::FRAC},
	{"bra", LatexMathSymbol::Tag::BRA},
	{"ket", LatexMathSymbol::Tag::KET},
};

std::optional<std::string> LatexSymbolLookup::getLeftSymbolUtf8(LatexMathSymbol::Tag tag)
{
	if (auto entry = getLeftSymbolUtf16(tag); entry)
	{
		return UnicodeUtils::utf16ToUtf8String(*entry);
	}
	else
	{
		return std::nullopt;
	}
}

std::optional<std::string> LatexSymbolLookup::getRightSymbolUtf8(LatexMathSymbol::Tag tag)
{
	if (auto entry = getRightSymbolUtf16(tag); entry)
	{
		return UnicodeUtils::utf16ToUtf8String(*entry);
	}
	else
	{
		return std::nullopt;
	}
}

std::optional<std::string> LatexSymbolLookup::getSymbolUtf8(const std::string& tag)
{
	if (auto entry = getSymbolUtf16(tag); entry)
	{
		return UnicodeUtils::utf16ToUtf8String(*entry);
	}
	else
	{
		return std::nullopt;
	}
}

std::optional<std::wstring> LatexSymbolLookup::getLeftSymbolUtf16(LatexMathSymbol::Tag tag)
{
	if (auto iter = mLeftSymbols.find(tag); iter != mLeftSymbols.end())
	{
		return iter->second;
	}
	else
	{
		return std::nullopt;
	}
}

std::optional<std::wstring> LatexSymbolLookup::getRightSymbolUtf16(LatexMathSymbol::Tag tag)
{
	if (auto iter = mRightSymbols.find(tag); iter != mRightSymbols.end())
	{
		return iter->second;
	}
	else
	{
		return std::nullopt;
	}

}

std::optional<std::wstring> LatexSymbolLookup::getSymbolUtf16(const std::string& tag)
{
	if (auto iter = mSymbols.find(tag); iter != mSymbols.end())
	{
		return iter->second;
	}
	else 
	{
		return std::nullopt;
	}
}

std::optional<LatexMathSymbol::Tag> LatexSymbolLookup::getKnownTag(const std::string& tag)
{
	if (auto iter = mTags.find(tag); iter != mTags.end())
	{
		return iter->second;
	}
	else
	{
		return std::nullopt;
	}
}