#include "util/serializationRegistry.h"

#include <QHash>
#include <QJsonObject>

static QHash<QString, ItemFactory> &registry()
{
    static QHash<QString, ItemFactory> map;
    return map;
}

void registerItemFactory(const QString &type, ItemFactory factory)
{
    registry().insert(type, factory);
}

QGraphicsItem *createItemFromJson(const QJsonObject &obj)
{
    QString type = obj["type"].toString();
    ItemFactory factory = registry().value(type);
    if (factory)
        return factory(obj);
    qWarning("createItemFromJson: unknown type '%s'", qPrintable(type));
    return nullptr;
}
