document.addEventListener('alpine:init', () => {
	Alpine.store('alphabreed', {
		backUrl: '/',
		pending: false,

		async apiCall(method, url, data) {
			this.pending = true;
			const options = {
				method: method,
				headers: { 'Authorization': localStorage.getItem('alphabreedtoken') }
			};
			if (data) {
				options.body = new FormData();
				for (let key in data)
					if (data.hasOwnProperty(key))
						options.body.append(key, data[key]);
			}
			const response = await fetch(url, options);
			this.pending = false;
			if (!response.ok)
				return false;
			if (response.status == 204)
				return true;
			return await response.json();
		},

		async checkToken() {
			const token = localStorage.getItem('alphabreedtoken');
			if (!token) {
				window.location = '/login/?back='+encodeURIComponent(this.backUrl);
				return false;
			}
			const result = await this.apiCall('POST', '/api/collections/users/auth-refresh');
			if (!result.token) {
				window.location = '/login/?back='+encodeURIComponent(this.backUrl);
				return false;
			}
			localStorage.setItem('alphabreedtoken', result.token);
			localStorage.setItem('alphabreedid', result.record.id);
			return true;
		},

		async createBookmarkCategory(name) {
			const result = await this.apiCall('POST',
				'/api/collections/bookmarkcategories/records',
				{ owner: localStorage.getItem('alphabreedid'), name: name }
			);
			return result.id;
		},

		async updateBookmarkCategory(id, name) {
			await this.apiCall('PATCH',
				'/api/collections/bookmarkcategories/records/'+encodeURIComponent(id),
				{ owner: localStorage.getItem('alphabreedid'), name: name }
			);
		},

		async deleteBookmarkCategory(id) {
			await this.apiCall('DELETE',
				'/api/collections/bookmarkcategories/records/'+encodeURIComponent(id)
			);
		},

		async createBookmark(categoryId, name, url) {
			const result = await this.apiCall('POST',
				'/api/collections/bookmarks/records',
				{
					owner: localStorage.getItem('alphabreedid'),
					category: categoryId,
					name: name,
					url: url
				}
			);
			return result.id;
		},

		async updateBookmark(id, categoryId, name, url) {
			await this.apiCall('PATCH',
				'/api/collections/bookmarks/records/'+encodeURIComponent(id),
				{
					owner: localStorage.getItem('alphabreedid'),
					category: categoryId,
					name: name,
					url: url
				}
			);
		},

		async deleteBookmark(id) {
			await this.apiCall('DELETE',
				'/api/collections/bookmarks/records/'+encodeURIComponent(id)
			);
		},

		async getBookmarkCategories() {
			const result = await this.apiCall('GET',
				'/api/collections/bookmarkcategories/records?perPage=9999&fields=id,name&sort=name'
			);
			return result.items;
		},

		async getBookmarks() {
			const result = await this.apiCall('GET',
				'/api/collections/bookmarks/records?perPage=1000&expand=category&fields=id,name,url,expand.category.id,expand.category.name&sort=name'
			);
			return result.items;
		},

		async getNotes() {
			const owner = localStorage.getItem('alphabreedid');
			const result = await this.apiCall('GET',
				'/api/collections/notes/records/'+encodeURIComponent(owner)+'?fields=notes'
			);
			if (result === false)
				return false;
			return result.notes;
		},

		async createNotes(notes) {
			return await this.apiCall('POST',
				'/api/collections/notes/records',
				{ id: localStorage.getItem('alphabreedid'), notes: notes }
			);
		},

		async updateNotes(notes) {
			const owner = localStorage.getItem('alphabreedid');
			return await this.apiCall('PATCH',
				'/api/collections/notes/records/'+encodeURIComponent(owner),
				{ notes: notes }
			);
		},
	});
});