import { fontList } from "../cache.js";

/**
 * parse/preload a font face
 * @param {loader.Asset} data - asset data
 * @param {Function} [onload] - function to be called when the asset is loaded
 * @param {Function} [onerror] - function to be called in case of error
 * @returns {number} the amount of corresponding resource parsed/preloaded
 * @ignore
 * @example
 * preloadFontFace([
 *     { name: "'kenpixel'", type: "fontface", src: "data/font/kenvector_future.woff2" }
 * ]);
 */
export function preloadFontFace(data, onload, onerror) {
	const fontFaceSet =
		typeof globalThis.document !== "undefined"
			? globalThis.document.fonts
			: undefined;

	// FontFace constructor expects src in `url(...)` or `local()` format
	// only wrap plain paths in url(); leave url(), local(), and data URIs as-is
	if (!data.src.startsWith("url(") && !data.src.startsWith("local(")) {
		data.src = "url(" + data.src + ")";
	}

	if (typeof fontFaceSet !== "undefined") {
		// create a new font face
		const font = new FontFace(data.name, data.src);
		// loading promise
		font.load().then(
			() => {
				// add the font to the cache
				fontList[data.name] = font;
				// add the font to the document
				fontFaceSet.add(font);
				// onloaded callback
				if (typeof onload === "function") {
					onload();
				}
			},
			(error) => {
				// rejected
				if (typeof onerror === "function") {
					onerror(error);
				}
			},
		);
	} else {
		if (typeof onerror === "function") {
			onerror();
		}
	}

	return 1;
}
