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

#import "ios/chrome/browser/bubble/model/utils.h"

#import <algorithm>

#import "components/segmentation_platform/embedder/default_model/device_switcher_model.h"
#import "components/segmentation_platform/embedder/default_model/device_switcher_result_dispatcher.h"
#import "components/segmentation_platform/public/result.h"
#import "ios/chrome/browser/shared/model/utils/first_run_util.h"
#import "ios/chrome/browser/shared/public/features/system_flags.h"

namespace {

constexpr base::TimeDelta kNewUserFirstRunRecency = base::Days(60);

bool IsUserSafariSwitcher(
    segmentation_platform::DeviceSwitcherResultDispatcher* dispatcher) {
  if (!dispatcher) {
    return false;
  }

  bool is_new_chrome_user_forced =
      experimental_flags::GetSegmentForForcedDeviceSwitcherExperience() ==
      segmentation_platform::DeviceSwitcherModel::kSyncedAndFirstDeviceLabel;
  if (is_new_chrome_user_forced) {
    return true;
  }
  segmentation_platform::ClassificationResult result =
      dispatcher->GetCachedClassificationResult();
  // Use the device switcher classification result to determine the user is new
  // to Chrome across all devices and platforms.
  if (result.status != segmentation_platform::PredictionStatus::kSucceeded) {
    return false;
  }
  return std::ranges::contains(
      result.ordered_labels,
      segmentation_platform::DeviceSwitcherModel::kSyncedAndFirstDeviceLabel);
}
}  // namespace

bool IsUserNewSafariSwitcher(
    segmentation_platform::DeviceSwitcherResultDispatcher* dispatcher) {
  return IsFirstRunRecent(kNewUserFirstRunRecency) &&
         IsUserSafariSwitcher(dispatcher);
}
