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

#include "chrome/browser/profiles/batch_upload/batch_upload_service_factory.h"

#include "chrome/browser/profiles/batch_upload/batch_upload_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_selections.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/ui/profiles/batch_upload_ui_delegate.h"

BatchUploadServiceFactory::BatchUploadServiceFactory()
    : ProfileKeyedServiceFactory("BatchUpload") {
  DependsOn(IdentityManagerFactory::GetInstance());
  DependsOn(SyncServiceFactory::GetInstance());
}

BatchUploadServiceFactory::~BatchUploadServiceFactory() = default;

// static
BatchUploadService* BatchUploadServiceFactory::GetForProfile(Profile* profile,
                                                             bool create) {
  return static_cast<BatchUploadService*>(
      GetInstance()->GetServiceForBrowserContext(profile, create));
}

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

std::unique_ptr<KeyedService>
BatchUploadServiceFactory::BuildServiceInstanceForBrowserContext(
    content::BrowserContext* context) const {
  Profile* profile = Profile::FromBrowserContext(context);

  // BatchUploadService depends on SyncService. If SyncService is not available,
  // BatchUploadService should not be created.
  syncer::SyncService* sync_service =
      SyncServiceFactory::GetForProfile(profile);
  if (!sync_service) {
    return nullptr;
  }

  return std::make_unique<BatchUploadService>(
      IdentityManagerFactory::GetForProfile(profile), sync_service,
      profile->GetPrefs(), std::make_unique<BatchUploadUIDelegate>());
}
