/**
 * Fetches data from the specified URL.
 * @param {string} url - The URL to fetch the data from.
 * @param {string} responseType - The type of response expected ('json', 'text', 'blob', 'arrayBuffer').
 * @param {Object} [settings] - custom settings to apply to the request (@link https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
 * @returns {Promise} A promise that resolves with the fetched data or rejects with an error.
 * @ignore
 * @example
 * fetchData('https://api.example.com/data', 'json')
 *     .then(data => {
 *         // Handle the fetched JSON data
 *     })
 *     .catch(error => {
 *         // Handle the error
 *     });
 */
export function fetchData(url, responseType, settings = {}) {
	return fetch(url, {
		method: "GET",
		// internally nocache is a string with a generated random number
		cache: settings.nocache === "" ? "no-cache" : "reload",
		credentials: settings.withCredentials === true ? "include" : "omit",
		// see setting.crossorigin, "anonymous" is used for cross-origin requests
		mode: settings.crossOrigin === "anonymous" ? "cors" : "no-cors",
	}).then((response) => {
		// status = 0 when file protocol is used, or cross-domain origin
		if (!response.ok && response.status !== 0) {
			throw new Error(`Network response was not ok: ${response.statusText}`);
		}

		switch (responseType) {
			case "json":
				return response.json();
			case "text":
				return response.text();
			case "blob":
				return response.blob();
			case "arrayBuffer":
				return response.arrayBuffer();
			default:
				throw new Error("Invalid response type: " + responseType);
		}
	});
}
