console.log('app is running!');
class App {
	$target = null;
	data = [];
	constructor($target) {
		this.$target = $target;
		
		this.data = CatUtil.getObjfromLocal('data') || [];
		this.themeCheckbox = new ThemeCheckbox({
			$target,
		});
		
		this.setLoading = (visible) => this.loader.setState({ visible });
		window.setLoading = this.setLoading; 
		
		this.loader = new Loader({
			$target,
			setLoading: this.setLoading,
			data: {
				visible: false,
			},
		});
		
		this.searchInput = new SearchInput({
			$target,
			onSearch: (keyword) => {
				this.searchResult.images.clear();
				this.onSearch(keyword, false, true);
			},
			updateKeywords: (keyword) => {
				const { keywords } = this.keywords.data;
				keywords.length >= 5 &&
					keywords.indexOf(keyword) == -1 &&
					keywords.shift();
				keywords.indexOf(keyword) == -1 &&
					this.keywords.setState({
						keywords: [...keywords, keyword],
					});
			},
		});
		
		this.searchRandom = new SearchRandom({
			$target,
			onRandom: (isClear) => {
				this.searchResult.images.clear();
				this.onSearch(undefined, true, isClear);
			},
		});
		this.keywords = new Keywords({
			$target,
			data: {
				keywords: ['노르웨이', '먼치킨', '숲', '터키시', '샴'],
			},
			onSearch: (keyword) => {
				this.searchResult.images.clear();
				this.onSearch(keyword, false, true);
			},
		});
		this.banner = new Banner({
			$target,
			data: [],
		});
		this.searchResult = new SearchResult({
			$target,
			initialData: this.data,
			onRandom: () => this.onSearch(undefined, true),
			onClick: async (image) => {
				
				
				const { data } = await api.getCat(image.id);
				console.log('onClick > imageInfo', data);
				this.imageInfo.setState({
					visible: true,
					image: {
						...image,
						...data,
					},
				});
			},
		});
		this.imageInfo = new ImageInfo({
			$target,
			data: {
				visible: false,
				image: null,
			},
		});
		this.getBanner();
		
		console.log('[CREATED]', 'App', this, this['loader'], this.loader);
	}
	onSearch = async (keyword, isRandom = false, isClear = false) => {
		if (isClear) this.searchResult.clearResults();
		this.searchResult.setIsRequesting(true);
		let result = null;
		if (isRandom) {
			result = await api.getRandom();
			console.log('App > onSearch:Random >', result);
		} else {
			result = await api.fetchCats(keyword);
			console.log('App > onSearch >', result);
		}
		this.searchResult.setIsRequesting(false);
		if (result.isErr)
			return alert(result.data.status + ': ' + result.data.message);
		else CatUtil.setObjtoLocal('data', result.data);
		this.setState(result.data);
	};
	getBanner = async () => {
		let tries = 3;
		let data;
		while (tries > 0) {
			const result = await api.getRandom();
			data = result.data;
			console.log(
				'!!! getBanner > tries',
				tries,
				result,
				result && result.data.length
			);
			if (data && data.length != undefined) break;
			tries--;
		}
		this.banner.setState(data);
	};
	setState = (nextData) => {
		
		this.data = nextData; 
		this.searchResult.setState(nextData); 
	};
}