// Copyright 2026 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/themes/cross_device/theme_translation.h"

#include "build/build_config.h"
#include "build/buildflag.h"

namespace themes {

#if BUILDFLAG(IS_ANDROID)
DeviceThemeInfo<sync_pb::ThemeAndroidSpecifics> TranslateDesktop(
    const sync_pb::ThemeSpecifics& desktop_specifics) {
  DeviceThemeInfo<sync_pb::ThemeAndroidSpecifics> info;
  // ThemeSpecifics does not contain the source device's actual desktop OS type
  // (e.g., Mac, Windows, Linux, ChromeOS), and DeviceInfo::OsType does not
  // have a generic desktop enum value. Set kWindows as a placeholder so that
  // CrossDeviceThemeTracker::OsTypeToDataType maps it to syncer::THEMES.
  // CrossDeviceThemeTracker::ResolveDeviceInfo uses this data type to match the
  // sync entity against DeviceInfoTracker and overwrites info.os_type with the
  // actual desktop OS once resolved.
  info.os_type = syncer::DeviceInfo::OsType::kWindows;

  bool is_customized = false;
  if (desktop_specifics.use_custom_theme()) {
    is_customized = true;
  }
  if (desktop_specifics.has_ntp_background()) {
    is_customized = true;
    *info.theme.mutable_ntp_background() = desktop_specifics.ntp_background();
  }

  // Handle color.
  if (desktop_specifics.has_user_color_theme()) {
    is_customized = true;
    *info.theme.mutable_user_color_theme() =
        desktop_specifics.user_color_theme();
  } else if (desktop_specifics.has_autogenerated_color_theme()) {
    is_customized = true;
    // Map autogenerated theme to user_color_theme. TONAL_SPOT is the default
    // scheme variant:
    // https://source.chromium.org/chromium/chromium/src/+/main:ui/color/scheme_variant.mojom;l=9;drc=a01a5eab7613e20650a8373255ca07deb804489f
    info.theme.mutable_user_color_theme()->set_color(
        desktop_specifics.autogenerated_color_theme().color());
    info.theme.mutable_user_color_theme()->set_browser_color_variant(
        sync_pb::UserColorTheme::TONAL_SPOT);
  }

  info.theme.set_use_custom_theme(is_customized);
  return info;
}

DeviceThemeInfo<sync_pb::ThemeAndroidSpecifics> TranslateIos(
    const sync_pb::ThemeIosSpecifics& ios_specifics) {
  DeviceThemeInfo<sync_pb::ThemeAndroidSpecifics> info;
  info.os_type = syncer::DeviceInfo::OsType::kIOS;

  bool is_customized = false;
  if (ios_specifics.has_user_color_theme()) {
    is_customized = true;
    *info.theme.mutable_user_color_theme() = ios_specifics.user_color_theme();
  }
  if (ios_specifics.has_ntp_background()) {
    is_customized = true;
    *info.theme.mutable_ntp_background() = ios_specifics.ntp_background();
  }

  info.theme.set_use_custom_theme(is_customized);
  return info;
}
#else   // !BUILDFLAG(IS_ANDROID)
DeviceThemeInfo<sync_pb::ThemeSpecifics> TranslateAndroid(
    const sync_pb::ThemeAndroidSpecifics& android_specifics) {
  DeviceThemeInfo<sync_pb::ThemeSpecifics> info;
  info.os_type = syncer::DeviceInfo::OsType::kAndroid;

  if (android_specifics.has_use_custom_theme()) {
    info.theme.set_use_custom_theme(android_specifics.use_custom_theme());
  }
  if (android_specifics.has_ntp_background()) {
    *info.theme.mutable_ntp_background() = android_specifics.ntp_background();
  }
  if (android_specifics.has_user_color_theme()) {
    *info.theme.mutable_user_color_theme() =
        android_specifics.user_color_theme();
  }
  return info;
}

DeviceThemeInfo<sync_pb::ThemeSpecifics> TranslateIos(
    const sync_pb::ThemeIosSpecifics& ios_specifics) {
  DeviceThemeInfo<sync_pb::ThemeSpecifics> info;
  info.os_type = syncer::DeviceInfo::OsType::kIOS;

  if (ios_specifics.has_user_color_theme()) {
    *info.theme.mutable_user_color_theme() = ios_specifics.user_color_theme();
  }
  if (ios_specifics.has_ntp_background()) {
    *info.theme.mutable_ntp_background() = ios_specifics.ntp_background();
  }
  return info;
}
#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace themes
