<template>
	<div class="mt-2 space-y-2 divide-y">
		<AppUpdateCard
			v-for="(app, index) in appsWithUpdates"
			:key="app.app"
			@click.native.self="toggleApp(app)"
			v-model:app="appsWithUpdates[index]"
			:selected="selectedApps.map(a => a.app).includes(app.app)"
			:uninstall="false"
			:selectable="true"
		/>
		<AppUpdateCard
			v-for="(app, index) in removedApps"
			:key="app.name"
			@click.native.self="toggleApp(app)"
			v-model:app="removedApps[index]"
			:selected="selectedApps.map(a => a.app).includes(app.app)"
			:uninstall="true"
		/>
	</div>
</template>
<script>
import AppUpdateCard from './AppUpdateCard.vue';

export default {
	name: 'BenchAppUpdates',
	props: ['apps', 'removedApps'],
	components: {
		AppUpdateCard
	},
	data() {
		return {
			selectedApps: []
		};
	},
	mounted() {
		// Select all apps by default
		this.selectedApps = this.appsWithUpdates.map(a => {
			return { app: a.app, release: a.next_release };
		});
		this.$emit('update:selectedApps', this.selectedApps);
	},
	methods: {
		toggleApp(app) {
			if (!this.selectedApps.map(a => a.app).includes(app.app)) {
				this.selectedApps.push({ app: app.app, release: app.next_release });
				this.$emit('update:selectedApps', this.selectedApps);
			} else {
				this.selectedApps = this.selectedApps.filter(a => a.app !== app.app);
				this.$emit('update:selectedApps', this.selectedApps);
			}
		}
	},
	computed: {
		appsWithUpdates() {
			return this.apps.filter(app => app.update_available);
		}
	},
	watch: {
		appsWithUpdates: {
			handler(apps) {
				apps.map(app => {
					this.selectedApps
						.filter(a => a.app == app.app)
						.map(a => (a.release = app.next_release));
				});
			},
			deep: true,
			immediate: true
		},
		removedApps: {
			handler(apps) {
				apps.map(app => {
					this.selectedApps
						.filter(a => a.app == app.app)
						.map(a => (a.release = app.next_release));
				});
			},
			deep: true,
			immediate: true
		},
		selectedApps: {
			handler(apps) {
				// Hardcoded for now, need a better way
				// to manage such dependencies (#TODO)
				// If updating JERP, must update Jingrow with it

				let jingrowApp = this.apps.filter(a => a.app == 'jingrow');
				let jingrowUpdateAvailable =
					jingrowApp.length == 1 && jingrowApp[0].update_available;

				if (
					apps.map(a => a.app).includes('jerp') &&
					!apps.map(a => a.app).includes('jingrow') &&
					jingrowUpdateAvailable
				) {
					apps.push({ app: 'jingrow', release: jingrowApp[0].next_release });
				}
			},
			deep: true,
			immediate: true
		}
	}
};
</script>
