import { BrowserDetector, PlatformDetector } from "@/utils";

import type { KeyboardAdaptEnvironment } from "./types";

export function getKeyboardAdaptEnvironment(
  ua: string | null = null,
): KeyboardAdaptEnvironment {
  const platform = PlatformDetector.getPlatformInfo(ua);
  const browser = BrowserDetector.getBrowserInfo(ua);
  const isXiaomiFamilyDevice = PlatformDetector.isXiaomiFamilyDevice(ua);
  const isOppoFamilyDevice = PlatformDetector.isOppoFamilyDevice(ua);
  const isFacebookInAppBrowser = BrowserDetector.isFacebookInAppBrowser(ua);
  const isTargetAndroidFacebookDevice =
    platform.isAndroid &&
    !platform.isIOS &&
    (isXiaomiFamilyDevice || isOppoFamilyDevice) &&
    isFacebookInAppBrowser;

  return {
    isAndroid: platform.isAndroid,
    isIOS: platform.isIOS,
    inAppBrowserName: browser.inAppBrowserName,
    isXiaomiFamilyDevice,
    isOppoFamilyDevice,
    isFacebookInAppBrowser,
    isTargetAndroidFacebookDevice,
  };
}

export function shouldEnableKeyboardAdaptation(
  environment: KeyboardAdaptEnvironment,
): boolean {
  return (
    environment.isAndroid &&
    !environment.isIOS &&
    environment.isFacebookInAppBrowser &&
    (environment.isXiaomiFamilyDevice || environment.isOppoFamilyDevice)
  );
}
