# Copyright (C) 2025 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause

if(NOT QT_NO_CREATE_TARGETS)
    get_target_property(_@target@_protobuf_wellknowntypes @QT_CMAKE_EXPORT_NAMESPACE@::@target@
        _qt_internal_protobuf_wellknown_types)
    set(_@target@_proto_external_include_dirs "")
    set(_@target@_types_not_found "")
    set(_@target@_include_lookup_dirs
        "${protobuf_ROOT}"
        "${Protobuf_ROOT}"
        "$ENV{protobuf_ROOT}"
        "$ENV{Protobuf_ROOT}"
    )

    foreach(extra_protobuf_dir_path "${protobuf_DIR}" "${Protobuf_DIR}" "$ENV{protobuf_DIR}"
        "$ENV{Protobuf_DIR}")
        if(extra_protobuf_dir_path)
            list(APPEND _@target@_include_lookup_dirs
                "${extra_protobuf_dir_path}/../.." "${extra_protobuf_dir_path}")
        endif()
        unset(extra_protobuf_dir_path)
    endforeach()

    foreach(type IN LISTS _@target@_protobuf_wellknowntypes)
        unset(_@target@_proto_file_dir)
        find_path(_@target@_proto_file_dir
            NAMES "${type}"
            PATHS ${_@target@_include_lookup_dirs}
            PATH_SUFFIXES include
            NO_CACHE
        )
        if(_@target@_proto_file_dir)
            if(NOT _@target@_proto_file_dir IN_LIST _@target@_proto_external_include_dirs)
                list(APPEND _@target@_proto_external_include_dirs "${_@target@_proto_file_dir}")
            endif()
        else()
            list(APPEND _@target@_types_not_found "${type}")
        endif()
    endforeach()
    unset(type)

    if(NOT _@target@_types_not_found)
        include(
            "${CMAKE_CURRENT_LIST_DIR}/@QT_CMAKE_EXPORT_NAMESPACE@@target@ProtobufProperties.cmake")
    else()
        set(@INSTALL_CMAKE_NAMESPACE@@target@_FOUND FALSE)
        list(JOIN _@target@_types_not_found "\n    " _@target@_types_not_found_string)
        string(JOIN "" @INSTALL_CMAKE_NAMESPACE@@target@_NOT_FOUND_MESSAGE
            "@INSTALL_CMAKE_NAMESPACE@@target@ lacks the following protobuf schemas:"
            "\n    ${_@target@_types_not_found_string}\n"
            "Please check your protobuf installation and ensure the missing schemas are in the"
            " CMake find paths. If you use a custom protobuf installation point to its location"
            " in protobuf_ROOT, Protobuf_ROOT, protobuf_DIR or Protobuf_DIR environment or"
            " cache variables.")
        unset(_@target@_types_not_found_string)
    endif()

    unset(_@target@_include_lookup_dirs)
    unset(_@target@_types_not_found)
    unset(_@target@_proto_external_include_dirs)
    unset(_@target@_protobuf_wellknowntypes)
endif()
