#ifndef JSONHIGHLIGHTER_H
#define JSONHIGHLIGHTER_H


// #include <QTextEdit>
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QQuickTextDocument>
#include <QObject>
#include <QJsonDocument>
#include <QStack>

class JsonHighlighter : public QSyntaxHighlighter {
    Q_OBJECT
    // 关键：定义 document 属性
    Q_PROPERTY(QQuickTextDocument* document READ document WRITE setDocument NOTIFY documentChanged)
public:
    explicit JsonHighlighter(QTextDocument *parent = nullptr);
    QQuickTextDocument* document() const;
    void setDocument(QQuickTextDocument* doc);
    void detectJsonErrors(const QString& text);
signals:
    void documentChanged();
private:
    QQuickTextDocument* m_document;
protected:
    void highlightBlock(const QString &text) override;
private:
    struct HighlightRule {
        QRegExp pattern;
        QTextCharFormat format;
    };
    QVector<HighlightRule> rules;
    void addRule(const QRegExp &pattern, const QColor &color);
    QTextCharFormat m_errorFormat; // 错误格式（红色波浪线）
};

class JsonProcessor : public QObject {
    Q_OBJECT
public:
    explicit JsonProcessor(QObject *parent = nullptr) : QObject(parent) {}
    Q_INVOKABLE QString formatJson(const QString &rawJson);
    Q_INVOKABLE QVariantMap validateJson(const QString &json);
};

#endif // JSONHIGHLIGHTER_H
