// Copyright 2020 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/web/common/uikit_ui_util.h"

#import <UIKit/UIKit.h>

#import "base/apple/foundation_util.h"

UIWindow* GetAnyKeyWindow() {
  for (UIScene* scene in UIApplication.sharedApplication.connectedScenes) {
    UIWindowScene* windowScene =
        base::apple::ObjCCastStrict<UIWindowScene>(scene);
    // Find a key window if it exists.
    for (UIWindow* window in windowScene.windows) {
      if (window.keyWindow) {
        return window;
      }
    }
  }

  return nil;
}

UIInterfaceOrientation GetInterfaceOrientation() {
  if (@available(iOS 16.0, *)) {
    return GetAnyKeyWindow().windowScene.effectiveGeometry.interfaceOrientation;
  } else {
    return GetAnyKeyWindow().windowScene.interfaceOrientation;
  }
}
