// 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/supervised_user/model/supervised_user_service_factory.h"

#import "base/check_deref.h"
#import "base/no_destructor.h"
#import "components/prefs/pref_service.h"
#import "components/supervised_user/core/browser/device_parental_controls.h"
#import "components/supervised_user/core/browser/supervised_user_service.h"
#import "components/variations/service/variations_service.h"
#import "ios/chrome/browser/first_run/model/first_run.h"
#import "ios/chrome/browser/shared/model/application_context/application_context.h"
#import "ios/chrome/browser/shared/model/profile/profile_ios.h"
#import "ios/chrome/browser/signin/model/identity_manager_factory.h"
#import "ios/chrome/browser/supervised_user/model/supervised_user_service_platform_delegate.h"
#import "ios/chrome/common/channel_info.h"
#import "url/gurl.h"

namespace supervised_user {

// static
SupervisedUserService* SupervisedUserServiceFactory::GetForProfile(
    ProfileIOS* profile) {
  return GetInstance()->GetServiceForProfileAs<SupervisedUserService>(
      profile, /*create=*/true);
}

// static
SupervisedUserServiceFactory* SupervisedUserServiceFactory::GetInstance() {
  static base::NoDestructor<SupervisedUserServiceFactory> instance;
  return instance.get();
}

SupervisedUserServiceFactory::SupervisedUserServiceFactory()
    : ProfileKeyedServiceFactoryIOS("SupervisedUserService") {
  DependsOn(IdentityManagerFactory::GetInstance());
}

std::unique_ptr<KeyedService>
SupervisedUserServiceFactory::BuildServiceInstanceFor(
    ProfileIOS* profile) const {
  std::unique_ptr<SupervisedUserServicePlatformDelegate> platform_delegate =
      std::make_unique<SupervisedUserServicePlatformDelegate>(profile);
  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile);
  scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
      profile->GetSharedURLLoaderFactory();
  return std::make_unique<SupervisedUserService>(
      identity_manager, url_loader_factory, CHECK_DEREF(profile->GetPrefs()),
      std::move(platform_delegate),
      GetApplicationContext()->GetDeviceParentalControls());
}

}  // namespace supervised_user
