/**
 * Copyright © Volker Schukai and all contributing authors, {{copyRightYear}}. All rights reserved.
 * Node module: @schukai/monster
 *
 * This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
 * The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
 *
 * For those who do not wish to adhere to the AGPLv3, a commercial license is available.
 * Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
 * For more information about purchasing a commercial license, please contact Volker Schukai.
 *
 * SPDX-License-Identifier: AGPL-3.0
 */

const ENV_AWS_LAMBDA = "aws-lambda";
/**
 * @type {string}
 */
const ENV_GOOGLE_FUNCTIONS = "google-functions";
/**
 * @type {string}
 */
const ENV_ELECTRON = "electron";
/**
 * @type {string}
 */
const ENV_NODE = "node";
/**
 * @type {string}
 */
const ENV_BROWSER = "browser";
/**
 * @type {string}
 */
const ENV_WEB_WORKER = "web-worker";
/**
 * @type {string}
 */
const ENV_DENO = "deno";
/**
 * @type {string}
 */
const ENV_UNKNOWN = "unknown";

/**
 * Detects and returns the current runtime environment.
 *
 * - 'aws-lambda': AWS Lambda environment
 * - 'google-functions': Google Cloud Functions environment
 * - 'electron': Electron environment
 * - 'node': Node.js environment
 * - 'browser': Browser environment
 * - 'web-worker': Web Worker environment
 * - 'deno': Deno environment
 * - 'react-native': React Native environment
 * - 'unknown': Unknown environment
 *
 * @since 3.34.0
 * @return {string} The detected runtime environment. Possible values are:
 */
function detectRuntimeEnvironment() {
	// AWS Lambda environment
	if (
		typeof process !== "undefined" &&
		process.env != null &&
		process.env.AWS_LAMBDA_FUNCTION_NAME
	) {
		return ENV_AWS_LAMBDA;
	}

	// Google Cloud Functions environment
	if (
		typeof process !== "undefined" &&
		process.env != null &&
		process.env.FUNCTION_NAME
	) {
		return ENV_GOOGLE_FUNCTIONS;
	}

	// Node.js environment
	if (
		typeof process !== "undefined" &&
		process.versions != null &&
		process.versions.node != null
	) {
		// Electron environment
		if (process.versions.electron != null) {
			return ENV_ELECTRON;
		}
		return ENV_NODE;
	}

	// Browser environment
	if (
		typeof window !== "undefined" &&
		typeof window.document !== "undefined" &&
		typeof navigator !== "undefined" &&
		typeof navigator.userAgent === "string"
	) {
		// Web Worker environment
		if (typeof self === "object" && typeof importScripts === "function") {
			return ENV_WEB_WORKER;
		}
		return ENV_BROWSER;
	}

	// Deno environment
	if (typeof Deno !== "undefined") {
		return ENV_DENO;
	}

	// Unknown environment
	return ENV_UNKNOWN;
}

export {
	ENV_AWS_LAMBDA,
	ENV_GOOGLE_FUNCTIONS,
	ENV_ELECTRON,
	ENV_NODE,
	ENV_BROWSER,
	ENV_WEB_WORKER,
	ENV_DENO,
	ENV_UNKNOWN,
	detectRuntimeEnvironment,
};
