// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Autogenerated from Pigeon (v27.3.2), do not edit directly.
// See also: https://pub.dev/packages/pigeon

#undef _HAS_EXCEPTIONS

#include "messages.g.h"

#include <flutter/basic_message_channel.h>
#include <flutter/binary_messenger.h>
#include <flutter/encodable_value.h>
#include <flutter/standard_message_codec.h>

#include <cmath>
#include <limits>
#include <map>
#include <optional>
#include <sstream>
#include <string>

namespace url_launcher_windows {
using ::flutter::BasicMessageChannel;
using ::flutter::CustomEncodableValue;
using ::flutter::EncodableList;
using ::flutter::EncodableMap;
using ::flutter::EncodableValue;

FlutterError CreateConnectionError(const std::string channel_name) {
  return FlutterError(
      "channel-error",
      "Unable to establish connection on channel: '" + channel_name + "'.",
      EncodableValue(""));
}

namespace {
template <typename T>
bool PigeonInternalDeepEquals(const T& a, const T& b);

bool PigeonInternalDeepEquals(const double& a, const double& b);

template <typename T>
bool PigeonInternalDeepEquals(const std::vector<T>& a, const std::vector<T>& b);

template <typename K, typename V>
bool PigeonInternalDeepEquals(const std::map<K, V>& a, const std::map<K, V>& b);

template <typename T>
bool PigeonInternalDeepEquals(const std::optional<T>& a,
                              const std::optional<T>& b);

template <typename T>
bool PigeonInternalDeepEquals(const std::unique_ptr<T>& a,
                              const std::unique_ptr<T>& b);

bool PigeonInternalDeepEquals(const ::flutter::EncodableValue& a,
                              const ::flutter::EncodableValue& b);

template <typename T>
bool PigeonInternalDeepEquals(const T& a, const T& b) {
  return a == b;
}

template <typename T>
bool PigeonInternalDeepEquals(const std::vector<T>& a,
                              const std::vector<T>& b) {
  if (a.size() != b.size()) {
    return false;
  }
  for (size_t i = 0; i < a.size(); ++i) {
    if (!PigeonInternalDeepEquals(a[i], b[i])) {
      return false;
    }
  }
  return true;
}

template <typename K, typename V>
bool PigeonInternalDeepEquals(const std::map<K, V>& a,
                              const std::map<K, V>& b) {
  if (a.size() != b.size()) {
    return false;
  }
  for (const auto& kv : a) {
    bool found = false;
    for (const auto& b_kv : b) {
      if (PigeonInternalDeepEquals(kv.first, b_kv.first)) {
        if (PigeonInternalDeepEquals(kv.second, b_kv.second)) {
          found = true;
          break;
        } else {
          return false;
        }
      }
    }
    if (!found) {
      return false;
    }
  }
  return true;
}

bool PigeonInternalDeepEquals(const double& a, const double& b) {
  // Normalize -0.0 to 0.0 and handle NaN equality.
  return (a == b) || (std::isnan(a) && std::isnan(b));
}

template <typename T>
bool PigeonInternalDeepEquals(const std::optional<T>& a,
                              const std::optional<T>& b) {
  if (!a && !b) {
    return true;
  }
  if (!a || !b) {
    return false;
  }
  return PigeonInternalDeepEquals(*a, *b);
}

template <typename T>
bool PigeonInternalDeepEquals(const std::unique_ptr<T>& a,
                              const std::unique_ptr<T>& b) {
  if (a.get() == b.get()) {
    return true;
  }
  if (!a || !b) {
    return false;
  }
  return PigeonInternalDeepEquals(*a, *b);
}

bool PigeonInternalDeepEquals(const ::flutter::EncodableValue& a,
                              const ::flutter::EncodableValue& b) {
  if (a.index() != b.index()) {
    return false;
  }
  if (const double* da = std::get_if<double>(&a)) {
    return PigeonInternalDeepEquals(*da, std::get<double>(b));
  } else if (const ::flutter::EncodableList* la =
                 std::get_if<::flutter::EncodableList>(&a)) {
    return PigeonInternalDeepEquals(*la, std::get<::flutter::EncodableList>(b));
  } else if (const ::flutter::EncodableMap* ma =
                 std::get_if<::flutter::EncodableMap>(&a)) {
    return PigeonInternalDeepEquals(*ma, std::get<::flutter::EncodableMap>(b));
  }
  return a == b;
}

template <typename T>
size_t PigeonInternalDeepHash(const T& v);

size_t PigeonInternalDeepHash(const double& v);

template <typename T>
size_t PigeonInternalDeepHash(const std::vector<T>& v);

template <typename K, typename V>
size_t PigeonInternalDeepHash(const std::map<K, V>& v);

template <typename T>
size_t PigeonInternalDeepHash(const std::optional<T>& v);

template <typename T>
size_t PigeonInternalDeepHash(const std::unique_ptr<T>& v);

size_t PigeonInternalDeepHash(const ::flutter::EncodableValue& v);

template <typename T>
size_t PigeonInternalDeepHash(const T& v) {
  return std::hash<T>()(v);
}

template <typename T>
size_t PigeonInternalDeepHash(const std::vector<T>& v) {
  size_t result = 1;
  for (const auto& item : v) {
    result = result * 31 + PigeonInternalDeepHash(item);
  }
  return result;
}

template <typename K, typename V>
size_t PigeonInternalDeepHash(const std::map<K, V>& v) {
  size_t result = 0;
  for (const auto& kv : v) {
    result += ((PigeonInternalDeepHash(kv.first) * 31) ^
               PigeonInternalDeepHash(kv.second));
  }
  return result;
}

size_t PigeonInternalDeepHash(const double& v) {
  if (std::isnan(v)) {
    // Normalize NaN to a consistent hash.
    return std::hash<double>()(std::numeric_limits<double>::quiet_NaN());
  }
  if (v == 0.0) {
    // Normalize -0.0 to 0.0 so they have the same hash code.
    return std::hash<double>()(0.0);
  }
  return std::hash<double>()(v);
}

template <typename T>
size_t PigeonInternalDeepHash(const std::optional<T>& v) {
  return v ? PigeonInternalDeepHash(*v) : 0;
}

template <typename T>
size_t PigeonInternalDeepHash(const std::unique_ptr<T>& v) {
  return v ? PigeonInternalDeepHash(*v) : 0;
}

size_t PigeonInternalDeepHash(const ::flutter::EncodableValue& v) {
  size_t result = v.index();
  if (const double* dv = std::get_if<double>(&v)) {
    result = result * 31 + PigeonInternalDeepHash(*dv);
  } else if (const ::flutter::EncodableList* lv =
                 std::get_if<::flutter::EncodableList>(&v)) {
    result = result * 31 + PigeonInternalDeepHash(*lv);
  } else if (const ::flutter::EncodableMap* mv =
                 std::get_if<::flutter::EncodableMap>(&v)) {
    result = result * 31 + PigeonInternalDeepHash(*mv);
  } else {
    std::visit(
        [&result](const auto& val) {
          using T = std::decay_t<decltype(val)>;
          if constexpr (!std::is_same_v<T, double> &&
                        !std::is_same_v<T, ::flutter::EncodableList> &&
                        !std::is_same_v<T, ::flutter::EncodableMap> &&
                        !std::is_same_v<T, std::monostate> &&
                        !std::is_same_v<T, ::flutter::CustomEncodableValue>) {
            result = result * 31 + PigeonInternalDeepHash(val);
          }
        },
        v);
  }
  return result;
}

template <typename T>
std::string PigeonInternalToString(const T& v);

std::string PigeonInternalToString(const bool& v);

template <typename T>
std::string PigeonInternalToString(const std::vector<T>& v);

template <typename K, typename V>
std::string PigeonInternalToString(const std::map<K, V>& v);

template <typename T>
std::string PigeonInternalToString(const std::optional<T>& v);

template <typename T>
std::string PigeonInternalToString(const std::unique_ptr<T>& v);

std::string PigeonInternalToString(const ::flutter::EncodableValue& v);

template <typename T>
std::string PigeonInternalToString(const T& v) {
  std::stringstream ss;
  if constexpr (std::is_enum_v<T>) {
    ss << static_cast<int>(v);
  } else {
    ss << v;
  }
  return ss.str();
}

std::string PigeonInternalToString(const bool& v) {
  return v ? "true" : "false";
}

template <typename T>
std::string PigeonInternalToString(const std::vector<T>& v) {
  std::stringstream ss;
  ss << "[";
  for (size_t i = 0; i < v.size(); ++i) {
    if (i > 0) {
      ss << ", ";
    }
    ss << PigeonInternalToString(v[i]);
  }
  ss << "]";
  return ss.str();
}

template <typename K, typename V>
std::string PigeonInternalToString(const std::map<K, V>& v) {
  std::stringstream ss;
  ss << "{";
  bool first = true;
  for (const auto& kv : v) {
    if (!first) {
      ss << ", ";
    }
    first = false;
    ss << PigeonInternalToString(kv.first) << ": "
       << PigeonInternalToString(kv.second);
  }
  ss << "}";
  return ss.str();
}

template <typename T>
std::string PigeonInternalToString(const std::optional<T>& v) {
  return v ? PigeonInternalToString(*v) : "null";
}

template <typename T>
std::string PigeonInternalToString(const std::unique_ptr<T>& v) {
  return v ? PigeonInternalToString(*v) : "null";
}

std::string PigeonInternalToString(const ::flutter::EncodableValue& v) {
  return std::visit(
      [](const auto& val) {
        using T = std::decay_t<decltype(val)>;
        if constexpr (std::is_same_v<T, std::monostate>) {
          return std::string("null");
        } else if constexpr (std::is_same_v<T, bool>) {
          return val ? std::string("true") : std::string("false");
        } else if constexpr (std::is_same_v<T, std::string>) {
          return "\"" + val + "\"";
        } else if constexpr (std::is_same_v<T,
                                            ::flutter::CustomEncodableValue>) {
          return std::string("[custom]");
        } else {
          return PigeonInternalToString(val);
        }
      },
      v);
}

}  // namespace

PigeonInternalCodecSerializer::PigeonInternalCodecSerializer() {}

EncodableValue PigeonInternalCodecSerializer::ReadValueOfType(
    uint8_t type, ::flutter::ByteStreamReader* stream) const {
  return ::flutter::StandardCodecSerializer::ReadValueOfType(type, stream);
}

void PigeonInternalCodecSerializer::WriteValue(
    const EncodableValue& value, ::flutter::ByteStreamWriter* stream) const {
  ::flutter::StandardCodecSerializer::WriteValue(value, stream);
}

/// The codec used by UrlLauncherApi.
const ::flutter::StandardMessageCodec& UrlLauncherApi::GetCodec() {
  return ::flutter::StandardMessageCodec::GetInstance(
      &PigeonInternalCodecSerializer::GetInstance());
}

// Sets up an instance of `UrlLauncherApi` to handle messages through the
// `binary_messenger`.
void UrlLauncherApi::SetUp(::flutter::BinaryMessenger* binary_messenger,
                           UrlLauncherApi* api) {
  UrlLauncherApi::SetUp(binary_messenger, api, "");
}

void UrlLauncherApi::SetUp(::flutter::BinaryMessenger* binary_messenger,
                           UrlLauncherApi* api,
                           const std::string& message_channel_suffix) {
  const std::string prepended_suffix =
      message_channel_suffix.length() > 0
          ? std::string(".") + message_channel_suffix
          : "";
  {
    BasicMessageChannel<> channel(
        binary_messenger,
        "dev.flutter.pigeon.url_launcher_windows.UrlLauncherApi.canLaunchUrl" +
            prepended_suffix,
        &GetCodec());
    if (api != nullptr) {
      channel.SetMessageHandler(
          [api](const EncodableValue& message,
                const ::flutter::MessageReply<EncodableValue>& reply) {
            try {
              const auto& args = std::get<EncodableList>(message);
              const auto& encodable_url_arg = args.at(0);
              if (encodable_url_arg.IsNull()) {
                reply(WrapError("url_arg unexpectedly null."));
                return;
              }
              const auto& url_arg = std::get<std::string>(encodable_url_arg);
              ErrorOr<bool> output = api->CanLaunchUrl(url_arg);
              if (output.has_error()) {
                reply(WrapError(output.error()));
                return;
              }
              EncodableList wrapped;
              wrapped.push_back(EncodableValue(std::move(output).TakeValue()));
              reply(EncodableValue(std::move(wrapped)));
            } catch (const std::exception& exception) {
              reply(WrapError(exception.what()));
            }
          });
    } else {
      channel.SetMessageHandler(nullptr);
    }
  }
  {
    BasicMessageChannel<> channel(
        binary_messenger,
        "dev.flutter.pigeon.url_launcher_windows.UrlLauncherApi.launchUrl" +
            prepended_suffix,
        &GetCodec());
    if (api != nullptr) {
      channel.SetMessageHandler(
          [api](const EncodableValue& message,
                const ::flutter::MessageReply<EncodableValue>& reply) {
            try {
              const auto& args = std::get<EncodableList>(message);
              const auto& encodable_url_arg = args.at(0);
              if (encodable_url_arg.IsNull()) {
                reply(WrapError("url_arg unexpectedly null."));
                return;
              }
              const auto& url_arg = std::get<std::string>(encodable_url_arg);
              ErrorOr<bool> output = api->LaunchUrl(url_arg);
              if (output.has_error()) {
                reply(WrapError(output.error()));
                return;
              }
              EncodableList wrapped;
              wrapped.push_back(EncodableValue(std::move(output).TakeValue()));
              reply(EncodableValue(std::move(wrapped)));
            } catch (const std::exception& exception) {
              reply(WrapError(exception.what()));
            }
          });
    } else {
      channel.SetMessageHandler(nullptr);
    }
  }
}

EncodableValue UrlLauncherApi::WrapError(std::string_view error_message) {
  return EncodableValue(
      EncodableList{EncodableValue(std::string(error_message)),
                    EncodableValue("Error"), EncodableValue()});
}

EncodableValue UrlLauncherApi::WrapError(const FlutterError& error) {
  return EncodableValue(EncodableList{EncodableValue(error.code()),
                                      EncodableValue(error.message()),
                                      error.details()});
}

}  // namespace url_launcher_windows
