"""Small QGIS/Qt compatibility helpers (kept dependency-free)."""


def make_field(name, kind):
    """Create a QgsField without tripping the 'QgsField constructor is
    deprecated' warning on QGIS >= 3.38, while staying compatible with older
    QGIS (3.22) and both Qt5 and Qt6.

    QGIS 3.38 deprecated QgsField(name, QVariant.Type) in favour of the
    QMetaType-based constructor, so we use that where it exists and fall back to
    the QVariant form otherwise.

    kind: 'str'/'string', 'int', 'double', 'bool', 'longlong'.
    """
    from qgis.core import QgsField
    k = "str" if kind == "string" else kind
    try:
        from qgis.PyQt.QtCore import QMetaType
        t = getattr(QMetaType, "Type", QMetaType)
        mapping = {"str": t.QString, "int": t.Int, "double": t.Double,
                   "bool": t.Bool, "longlong": t.LongLong}
        return QgsField(name, mapping[k])
    except Exception:
        from qgis.PyQt.QtCore import QVariant
        mapping = {"str": QVariant.String, "int": QVariant.Int,
                   "double": QVariant.Double, "bool": QVariant.Bool,
                   "longlong": QVariant.LongLong}
        return QgsField(name, mapping[k])
