import { describe, expect, it } from "vitest";

import {
  getKeyboardAdaptEnvironment,
  shouldEnableKeyboardAdaptation,
} from "../environment";
import type { KeyboardAdaptEnvironment } from "../types";

function makeEnvironment(
  overrides: Partial<KeyboardAdaptEnvironment> = {},
): KeyboardAdaptEnvironment {
  return {
    isAndroid: true,
    isIOS: false,
    inAppBrowserName: "facebook",
    isXiaomiFamilyDevice: true,
    isOppoFamilyDevice: false,
    isFacebookInAppBrowser: true,
    isTargetAndroidFacebookDevice: true,
    ...overrides,
  };
}

describe("shouldEnableKeyboardAdaptation", () => {
  it("enables for Xiaomi Android Facebook in-app browser fallback", () => {
    expect(shouldEnableKeyboardAdaptation(makeEnvironment())).toBe(true);
  });

  it("enables for OPPO Android Facebook in-app browser fallback", () => {
    expect(
      shouldEnableKeyboardAdaptation(
        makeEnvironment({
          isXiaomiFamilyDevice: false,
          isOppoFamilyDevice: true,
          isTargetAndroidFacebookDevice: true,
        }),
      ),
    ).toBe(true);
  });

  it("disables for non-target platforms, browsers, and devices", () => {
    expect(
      shouldEnableKeyboardAdaptation(
        makeEnvironment({
          isAndroid: false,
          isIOS: true,
          isXiaomiFamilyDevice: false,
          isOppoFamilyDevice: false,
          isTargetAndroidFacebookDevice: false,
        }),
      ),
    ).toBe(false);

    expect(
      shouldEnableKeyboardAdaptation(
        makeEnvironment({
          inAppBrowserName: "instagram",
          isFacebookInAppBrowser: false,
          isTargetAndroidFacebookDevice: false,
        }),
      ),
    ).toBe(false);

    expect(
      shouldEnableKeyboardAdaptation(
        makeEnvironment({
          isXiaomiFamilyDevice: false,
          isOppoFamilyDevice: false,
          isTargetAndroidFacebookDevice: false,
        }),
      ),
    ).toBe(false);
  });

  it("disables for iOS Facebook even when Facebook IAB is detected", () => {
    expect(
      shouldEnableKeyboardAdaptation(
        makeEnvironment({
          isAndroid: false,
          isIOS: true,
          inAppBrowserName: "facebook",
          isXiaomiFamilyDevice: false,
          isOppoFamilyDevice: false,
          isFacebookInAppBrowser: true,
          isTargetAndroidFacebookDevice: false,
        }),
      ),
    ).toBe(false);
  });

  it("does not trust target flag without the Android target device fields", () => {
    expect(
      shouldEnableKeyboardAdaptation(
        makeEnvironment({
          isAndroid: false,
          isIOS: true,
          isFacebookInAppBrowser: true,
          isXiaomiFamilyDevice: true,
          isTargetAndroidFacebookDevice: true,
        }),
      ),
    ).toBe(false);
  });

  it("detects iPhone Facebook in-app browser as non-target environment", () => {
    const environment = getKeyboardAdaptEnvironment(
      "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) " +
        "AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 " +
        "[FBAN/FBIOS;FBAV/466.0.0.49.109;FBBV/605000000;FBDV/iPhone15,2]",
    );

    expect(environment).toMatchObject({
      isAndroid: false,
      isIOS: true,
      isFacebookInAppBrowser: true,
      isTargetAndroidFacebookDevice: false,
    });
    expect(shouldEnableKeyboardAdaptation(environment)).toBe(false);
  });
});
