
//
// Copyright (C) 2024-2026 celenity
//
// This file is part of Phoenix.
//
// Phoenix is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// Phoenix is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with Phoenix. If not, see https://www.gnu.org/licenses/.
//

// This file contains Phoenix's "user" prefs, as it's not really possible to set those with the standard `.js` pref files
// See `phoenix-unified.js` (or `phoenix.js`/`phoenix-desktop.js` if you're looking at this locally...) for info on these prefs/why they're included.

// Determine our platform
/// See: https://codeberg.org/celenity/Phoenix/issues/231 - in the future, we'll likely use these to ship a single, unified .cfg
/// file for all platforms
defaultPref("browser.phoenix.platform", "unknown");
defaultPref("browser.phoenix.platform.current", "unknown");
defaultPref("browser.phoenix.platformSet", false);

try {
    // First, check the value of PHOENIX_HOST_PLATFORM
    if (getenv("PHOENIX_HOST_PLATFORM") == "android") {
        PHOENIX_PLATFORM = 'android'
    } else if (getenv("PHOENIX_HOST_PLATFORM") == "linux") {
        PHOENIX_PLATFORM = 'linux'
    // For now, we don't really need to distinguish between OS X and OS X Intel
    } else if (getenv("PHOENIX_HOST_PLATFORM") == "osx" || getenv("PHOENIX_HOST_PLATFORM") == "osx-intel") {
        PHOENIX_PLATFORM = 'osx'
    } else if (getenv("PHOENIX_HOST_PLATFORM") == "windows") {
        PHOENIX_PLATFORM = 'windows'

    // Now, fall-back to system env variables...

    // If any of the following env variables are defined, we know we're on Android:
    // (For general ANDROID_ vars, see ex.: https://android.googlesource.com/platform/system/core/+/refs/heads/main/rootdir/init.environ.rc.in)
    // ANDROID_ART_ROOT
    // ANDROID_ASSETS
    // ANDROID_BOOTLOGO
    // ANDROID_DATA
    // ANDROID_I18N_ROOT
    // ANDROID_ROOT
    // ANDROID_STORAGE
    // ANDROID_TZDATA_ROOT
    // MOZ_ANDROID_CPU_ABI
    // MOZ_ANDROID_CRASH_HANDLER
    // MOZ_ANDROID_LIBDIR
    // MOZ_ANDROID_LIBDIR_OVERRIDE
    // MOZ_ANDROID_PACKAGE_NAME
    // MOZ_ANDROID_USER_SERIAL_NUMBER
    } else if (getenv("ANDROID_ART_ROOT") || getenv("ANDROID_ASSETS") || getenv("ANDROID_BOOTLOGO") || getenv("ANDROID_DATA") || getenv("ANDROID_I18N_ROOT") || getenv("ANDROID_ROOT") || getenv("ANDROID_STORAGE") || getenv("ANDROID_TZDATA_ROOT") || getenv("MOZ_ANDROID_CPU_ABI") || getenv("MOZ_ANDROID_CRASH_HANDLER") || getenv("MOZ_ANDROID_LIBDIR") || getenv("MOZ_ANDROID_LIBDIR_OVERRIDE") || getenv("MOZ_ANDROID_PACKAGE_NAME") || getenv("MOZ_ANDROID_USER_SERIAL_NUMBER")) {
        PHOENIX_PLATFORM = 'android'
    // If any of the following env variables are defined, we know we're on OS X:
    // __CFBundleIdentifier
    // MOZ_APP_NO_DOCK
    // MOZ_NO_GLOBAL_MOUSE_MONITOR
    // XPC_FLAGS
    // XPC_SERVICE_NAME
    } else if (getenv("__CFBundleIdentifier") || getenv("MOZ_APP_NO_DOCK") || getenv("MOZ_NO_GLOBAL_MOUSE_MONITOR") || getenv("XPC_FLAGS") || getenv("XPC_SERVICE_NAME")) {
        PHOENIX_PLATFORM = 'osx'
    // If any of the following env variables are defined, we know we're on Windows:
    // MOZ_ENABLE_WIN32K
    // USERPROFILE
    // XRE_NO_DLL_READAHEAD
    // XRE_NO_WINDOWS_CRASH_DIALOG
    } else if (getenv("MOZ_ENABLE_WIN32K") || getenv("USERPROFILE") || getenv("XRE_NO_DLL_READAHEAD") || getenv("XRE_NO_WINDOWS_CRASH_DIALOG")) {
        PHOENIX_PLATFORM = 'windows'
    // If any of the following env variables are defined, we know we're on Linux:
    // DESKTOP_SESSION
    // GDK_BACKEND
    // GDMSESSION
    // GNOME_DESKTOP_SESSION_ID
    // GTK_USE_PORTAL
    // KDE_FULL_SESSION
    // LXQT_SESSION_CONFIG
    // MATE_DESKTOP_SESSION_ID
    // MOZ_DISABLE_WAYLAND_PROXY
    // MOZ_GDK_DISPLAY
    // MOZ_USE_XINPUT2
    // MOZ_X_SYNC
    // MOZ_X11_EGL
    // SNAP
    // SNAP_DESKTOP_RUNTIME
    // SNAP_INSTANCE_NAME
    // SNAP_NAME
    // SNAP_REAL_HOME
    // SYSTEMD_EXEC_PID
    // WAYLAND_DISPLAY
    // WAYLAND_PROXY_LOG
    // XDG_ACTIVATION_TOKEN
    // XDG_CACHE_HOME
    // XDG_CONFIG_DIRS
    // XDG_CONFIG_HOME
    // XDG_CURRENT_DESKTOP
    // XDG_DATA_DIRS
    // XDG_DATA_HOME
    // XDG_MENU_PREFIX
    // XDG_RUNTIME_DIR
    // XDG_SESSION_CLASS
    // XDG_SESSION_DESKTOP
    // XDG_SESSION_TYPE
    } else if (getenv("DESKTOP_SESSION") || getenv("GDK_BACKEND") || getenv("GDMSESSION") || getenv("GNOME_DESKTOP_SESSION_ID") || getenv("GTK_USE_PORTAL") || getenv("KDE_FULL_SESSION") || getenv("LXQT_SESSION_CONFIG") || getenv("MATE_DESKTOP_SESSION_ID") || getenv("MOZ_DISABLE_WAYLAND_PROXY") || getenv("MOZ_GDK_DISPLAY") || getenv("MOZ_USE_XINPUT2") || getenv("MOZ_X_SYNC") || getenv("MOZ_X11_EGL") || getenv("SNAP") || getenv("SNAP_DESKTOP_RUNTIME") || getenv("SNAP_INSTANCE_NAME") || getenv("SNAP_NAME") || getenv("SNAP_REAL_HOME") || getenv("SYSTEMD_EXEC_PID") || getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_PROXY_LOG") || getenv("XDG_ACTIVATION_TOKEN") || getenv("XDG_CACHE_HOME") || getenv("XDG_CONFIG_DIRS") || getenv("XDG_CONFIG_HOME") || getenv("XDG_CURRENT_DESKTOP") || getenv("XDG_DATA_DIRS") || getenv("XDG_DATA_HOME") || getenv("XDG_MENU_PREFIX") || getenv("XDG_RUNTIME_DIR") || getenv("XDG_SESSION_CLASS") || getenv("XDG_SESSION_DESKTOP") || getenv("XDG_SESSION_TYPE")) {
        PHOENIX_PLATFORM = 'linux'
    // At this point, we should really have our OS, but in case we don't for some reason, check prefs
    // In practice, these shouldn't be used
    } else if (getPref("apz.android.chrome_fling_physics.friction") || getPref("apz.android.chrome_fling_physics.inflexion") || getPref("apz.android.chrome_fling_physics.stop_threshold") || getPref("network.dns.native_https_timeout_android")) {
        PHOENIX_PLATFORM = 'android'
    } else if (getPref("browser.low_commit_space_threshold_percent") || getPref("media.ffmpeg.vaapi.force-surface-zero-copy") || getPref("widget.gtk.file-manager-show-items-timeout-ms")) {
        PHOENIX_PLATFORM = 'linux'
    } else if (getPref("network.dns.native_https_timeout_mac_msec") || getPref("widget.macos.shift-by-menubar-on-fullscreen") || getPref("widget.macos.automatic.text_substitution_fetch_length")) {
        PHOENIX_PLATFORM = 'osx'
    } else if (getPref("accessibility.windows.suppress-after-clipboard-copy") || getPref("accessibility.windows.suppress-for-snap-layout")) {
        PHOENIX_PLATFORM = 'windows'
    } else {
        PHOENIX_PLATFORM = 'unknown'
    }
} catch (e) {
    PHOENIX_PLATFORM = 'unknown'
};

pref("browser.phoenix.platform.current", `${PHOENIX_PLATFORM}`);
phoenixPlatformPrefCurrentValue = getPref("browser.phoenix.platform.current");

if (getPref("browser.phoenix.platformSet") == false || getPref("browser.phoenix.platform") == "unknown") {
    pref("browser.phoenix.platform", `${phoenixPlatformPrefCurrentValue}`);
    pref("browser.phoenix.platformSet", true); 
};

/// On first run, we need to reset the value of `extensions.quarantinedDomains.list`, in order for our custom list of quarantined domains to take effect
// (This is necessary because, for some reason, on Firefox Desktop, Mozilla sets their default list as a user pref/value)
// After first run though, we don't want to reset it, so that ex. users can customize the list if desired
// Similar applies for `browser.search.region` - that pref gets set as a `user` value, so we need to reset it
// Smooth scrolling (`general.smoothScroll`) is a similar weird case - we do attempt to enable it by default, but due to us disabling UI animations,
// Firefox disables it anyways - so, to work around that, we can set it as a user pref on first launch
defaultPref("browser.phoenix.firstRun.complete", false);

if (getPref("browser.phoenix.firstRun.complete") == false) {
    clearPref("browser.search.region");
    clearPref("extensions.quarantinedDomains.list"); // [NO-ANDROID] [NO-MAIL]
    pref("general.smoothScroll", true);
    pref("browser.phoenix.firstRun.complete", true);
};

pref("browser.urlbar.addons.minKeywordLength", 4); // [NO-ANDROID]
pref("browser.urlbar.flightStatus.minKeywordLength", 4); // [NO-ANDROID]
pref("browser.urlbar.market.minKeywordLength", 4); // [NO-ANDROID]
pref("browser.urlbar.weather.minKeywordLength", 4); // [NO-ANDROID]
pref("browser.urlbar.yelp.minKeywordLength", 4); // [NO-ANDROID]
pref("browser.urlbar.yelpRealtime.minKeywordLength", 4); // [NO-ANDROID]
pref("devtools.debugger.pending-selected-location", "{}"); // [NO-ANDROID]
pref("devtools.debugger.remote-enabled", false);
pref("devtools.netmonitor.requestfilter", ""); // [NO-ANDROID]
pref("nimbus.profileId", "");
pref("xpinstall.enabled", false); // [NO-ANDROID] [NO-MAIL]

// Deregister the Glean add-on ping scheduler
clearPref("app.update.lastUpdateTime.glean-addons-daily");

clearPref("browser.policies.runOncePerModification.removeSearchEngines"); // [NO-ANDROID]
clearPref("browser.safebrowsing.provider.ironfox.lastupdatetime");
clearPref("browser.safebrowsing.provider.ironfox.nextupdatetime");

lockPref("browser.phoenix.applied.userCfg", true);
