import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'

export const subscriptions = {
	namespaced: true,
	state: {
		subscriptions: [],
	},
	mutations: {
		set: (state, subscriptions) => {
			state.subscriptions = subscriptions
		},
	},
	actions: {
		fetch: async (context) => {
			const metrics = await axios.get(generateUrl('/apps/gpoddersync/personal_settings/metrics'))
			const subs = [...metrics.data.subscriptions].sort((a, b) => b.listenedSeconds - a.listenedSeconds)
			context.commit('set', subs.map(sub => sub.url))
		},
	},
}
