// To load assets asynchronously

import { Asset } from "expo-asset";

import { LOG } from "@logger";

const log = LOG.extend("assetsCache");

const PATH_TO_ASSETS_FOLDER = "../../assets";

const IMAGE_TO_CACHE = [
	require("../../assets/beasy_icon.png"),
	require(`${PATH_TO_ASSETS_FOLDER}/beasy_splash.png`),
	require(`${PATH_TO_ASSETS_FOLDER}/beasy_default.png`),
	require(`${PATH_TO_ASSETS_FOLDER}/beasy_background.png`),
];

const cacheImages = () => {
	return IMAGE_TO_CACHE.map((image) => {
		log.verbose("cacheImages |", image);
		return Asset.fromModule(image).downloadAsync();
	});
};

export const cacheAssetsAsync = () => {
	return Promise.all([...cacheImages()]);
};

export const loadAssetsAsync = async () => {
	try {
		await cacheAssetsAsync();
	} catch (error) {
		log.error("loadAssetsAsync |", error);
	}
};
