//* Copyright 2026 The Dawn & Tint Authors
//*
//* Redistribution and use in source and binary forms, with or without
//* modification, are permitted provided that the following conditions are met:
//*
//* 1. Redistributions of source code must retain the above copyright notice, this
//*    list of conditions and the following disclaimer.
//*
//* 2. Redistributions in binary form must reproduce the above copyright notice,
//*    this list of conditions and the following disclaimer in the documentation
//*    and/or other materials provided with the distribution.
//*
//* 3. Neither the name of the copyright holder nor the names of its
//*    contributors may be used to endorse or promote products derived from
//*    this software without specific prior written permission.
//*
//* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
//* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
//* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
//* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
//* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

//* TODO(https://crbug.com/526537254): It might be possible to merge this with the
//* dawn_platform.h in native once we finish migrating both native and wire.

{% set PREFIX = metadata.proc_table_prefix.upper() %}
#ifndef DAWNWIRE_CLIENT_{{PREFIX}}_PLATFORM_AUTOGEN_H_
#define DAWNWIRE_CLIENT_{{PREFIX}}_PLATFORM_AUTOGEN_H_

{% set api = metadata.api.lower() %}
#include "dawn/{{api}}_cpp.h"

{% set namespace = metadata.namespace %}
#include "dawn/wire/client/{{namespace}}_structs_autogen.h"

namespace dawn::wire::client {
    {% for type in by_category["object"] %}
        {% if type.name.CamelCase() in client_special_objects %}
            class {{type.name.CamelCase()}};
        {% else %}
            struct {{type.name.CamelCase()}};
        {% endif %}
    {% endfor %}
}

{% macro render_structure_conversions(name) -%}
    inline const {{as_cType(name)}}* ToAPI(const {{as_cppType(name)}}* rhs) {
        return reinterpret_cast<const {{as_cType(name)}}*>(rhs);
    }
    inline {{as_cType(name)}}* ToAPI({{as_cppType(name)}}* rhs) {
        return reinterpret_cast<{{as_cType(name)}}*>(rhs);
    }
    inline const {{as_cppType(name)}}* FromAPI(const {{as_cType(name)}}* rhs) {
        return reinterpret_cast<const {{as_cppType(name)}}*>(rhs);
    }
    inline {{as_cppType(name)}}* FromAPI({{as_cType(name)}}* rhs) {
        return reinterpret_cast<{{as_cppType(name)}}*>(rhs);
    }
    inline const {{as_cppType(name)}}& FromAPI(const {{as_cType(name)}}& rhs) {
        return *reinterpret_cast<const {{as_cppType(name)}}*>(&rhs);
    }
    inline {{as_cppType(name)}}& FromAPI({{as_cType(name)}}& rhs) {
        return *reinterpret_cast<{{as_cppType(name)}}*>(&rhs);
    }
{%- endmacro %}

namespace dawn::wire::client {

    {% set ChainedStructName = Name("chained struct") %}
    {{render_structure_conversions(ChainedStructName)|indent}}

    {% for type in by_category["structure"] if type.name.get() != "string view" %}
        {{render_structure_conversions(type.name)|indent}}

    {% endfor %}

    {% for type in by_category["object"] %}
        {% set Type = type.name.CamelCase() %}
        inline WGPU{{Type}} ToAPI({{Type}}* rhs) {
            return reinterpret_cast<WGPU{{Type}}>(rhs);
        }

        inline {{Type}}* FromAPI(WGPU{{Type}} rhs) {
            return reinterpret_cast<{{Type}}*>(rhs);
        }
        inline const {{Type}}* const* FromAPI(const WGPU{{Type}}* rhs) {
            return reinterpret_cast<const {{Type}}* const*>(rhs);
        }
        inline {{Type}}* const* FromAPI(WGPU{{Type}}* rhs) {
            return reinterpret_cast<{{Type}}* const*>(rhs);
        }
    {% endfor %}

    //* Special structs that we want to allow copies for since they are trivial.
    inline WGPUFuture ToAPI(Future rhs) {
        return {rhs.id};
    }
    inline Future FromAPI(WGPUFuture rhs) {
        return Future{rhs.id};
    }
    inline WGPUStringView ToAPI(StringView rhs) {
        return {rhs.data, rhs.length};
    }
    inline StringView FromAPI(WGPUStringView rhs) {
        return StringView(rhs);
    }

    {% for type in by_category["enum"] + by_category["bitmask"] %}
        inline {{as_cType(type.name)}} ToAPI({{namespace}}::{{as_cppType(type.name)}} rhs) {
            return static_cast<{{as_cType(type.name)}}>(rhs);
        }
    {% endfor %}

    {% for type in by_category["enum"] %}
        inline {{namespace}}::{{as_cppType(type.name)}} FromAPI({{as_cType(type.name)}} rhs) {
            return static_cast<{{namespace}}::{{as_cppType(type.name)}}>(rhs);
        }
    {% endfor %}

}  // namespace dawn::wire::client

#endif  // DAWNWIRE_CLIENT_{{PREFIX}}_PLATFORM_AUTOGEN_H_
