// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "textmodifier.h"

#include <qmljs/parser/qmljsastvisitor_p.h>
#include <qmljs/qmljsutils.h>

#include <QStack>
#include <QString>

namespace QmlDesigner {
namespace Internal {

class QMLRewriter: protected QmlJS::AST::Visitor
{
public:
    using ASTPath = QStack<QmlJS::AST::Node *>;

public:
    QMLRewriter(QmlDesigner::TextModifier &textModifier);

    virtual bool operator()(QmlJS::AST::UiProgram *ast);

    static void dump(const ASTPath &path);

protected:
    using QmlJS::AST::Visitor::visit;

    void throwRecursionDepthError() override;

    virtual void replace(int offset, int length, const QString &text);
    virtual void move(const QmlDesigner::TextModifier::MoveInfo &moveInfo);

    QString textBetween(int startPosition, int endPosition) const;
    QString textAt(const QmlJS::SourceLocation &location) const;

    int indentDepth() const;
    unsigned calculateIndentDepth(const QmlJS::SourceLocation &position) const;
    QString addIndentation(const QString &text, unsigned depth);

    static QmlJS::SourceLocation calculateLocation(QmlJS::AST::UiQualifiedId *id);
    static bool isMissingSemicolon(QmlJS::AST::UiObjectMember *member);
    static bool isMissingSemicolon(QmlJS::AST::Statement *stmt);

    QmlDesigner::TextModifier *textModifier() const
    { return m_textModifier; }

    bool includeSurroundingWhitespace(int &start, int &end) const;
    void includeLeadingEmptyLine(int &start) const;

    static QmlJS::AST::UiObjectMemberList *searchMemberToInsertAfter(
        QmlJS::AST::UiObjectMemberList *members, Utils::span<const PropertyNameView> propertyOrder);
    static QmlJS::AST::UiObjectMemberList *searchMemberToInsertAfter(
        QmlJS::AST::UiObjectMemberList *members,
        PropertyNameView propertyName,
        Utils::span<const PropertyNameView> propertyOrder);
    static QmlJS::AST::UiObjectMemberList *searchChildrenToInsertAfter(
        QmlJS::AST::UiObjectMemberList *members,
        Utils::span<const PropertyNameView> propertyOrder,
        int pos = -1);

protected:
    bool didRewriting() const
    { return m_didRewriting; }

    void setDidRewriting(bool didRewriting)
    { m_didRewriting = didRewriting; }

private:
    QmlDesigner::TextModifier *m_textModifier;
    bool m_didRewriting;
};

} // namespace Internal
} // namespace QmlDesigner
