// Copyright 2025 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/extensions/browser_window_util.h"

#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface_iterator.h"
#include "components/tabs/public/tab_interface.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ui/browser_window.h"
#endif

static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));

namespace extensions::browser_window_util {

namespace {

bool BrowserMatchesHelper(BrowserWindowInterface& browser,
                          Profile& profile,
                          bool include_incognito_or_parent,
                          bool restrict_to_normal_browsers,
                          bool restrict_to_current_workspace) {
  if (browser.GetProfile() != &profile) {
    if (!include_incognito_or_parent ||
        !profile.IsSameOrParent(browser.GetProfile())) {
      return false;
    }
  }

  if (restrict_to_normal_browsers &&
      browser.GetType() != BrowserWindowInterface::TYPE_NORMAL) {
    return false;
  }

#if BUILDFLAG(IS_CHROMEOS)
  if (restrict_to_current_workspace) {
    BrowserWindow* browser_window = BrowserWindow::FromBrowser(&browser);
    if (!browser_window || !browser_window->IsOnCurrentWorkspace()) {
      return false;
    }
  }
#endif

  return true;
}

}  // namespace

BrowserWindowInterface* GetBrowserForTabContents(
    content::WebContents& tab_contents) {
  tabs::TabInterface* tab =
      tabs::TabInterface::MaybeGetFromContents(&tab_contents);
  return tab ? tab->GetBrowserWindowInterface() : nullptr;
}

BrowserWindowInterface* GetLastActiveBrowserWithProfile(
    Profile& profile,
    bool include_incognito_or_parent) {
  BrowserWindowInterface* last_active_browser = nullptr;
  ForEachCurrentBrowserWindowInterfaceOrderedByActivation(
      [&](BrowserWindowInterface* browser) {
        bool browser_matches =
            BrowserMatchesHelper(*browser, profile, include_incognito_or_parent,
                                 /*restrict_to_normal_browsers=*/false,
                                 /*restrict_to_current_workspace=*/false);
        if (browser_matches) {
          last_active_browser = browser;
          return false;  // Stop iterating.
        }
        return true;  // Continue iterating.
      });

  return last_active_browser;
}

BrowserWindowInterface* GetLastActiveNormalBrowserWithProfile(
    Profile& profile,
    bool include_incognito_or_parent) {
  BrowserWindowInterface* last_active_browser = nullptr;
  ForEachCurrentBrowserWindowInterfaceOrderedByActivation(
      [&](BrowserWindowInterface* browser) {
        bool browser_matches =
            BrowserMatchesHelper(*browser, profile, include_incognito_or_parent,
                                 /*restrict_to_normal_browsers=*/true,
                                 /*restrict_to_current_workspace=*/true);
        if (browser_matches) {
          last_active_browser = browser;
          return false;  // Stop iterating.
        }
        return true;  // Continue iterating.
      });

  return last_active_browser;
}

}  // namespace extensions::browser_window_util
