import { graphql, get } from '../api';
import {
    releasePosterFragment,
    releaseActorsFragment,
    releaseTagsFragment,
    siteFragment,
} from '../fragments';
import { curateTag } from '../curate';

function initTagsActions(store, _router) {
    async function fetchTagBySlug(tagSlug) {
        const { tagBySlug } = await graphql(`
            query Tag($tagSlug:String!) {
                tagBySlug(slug:$tagSlug) {
                    id
                    name
                    slug
                    description
                    group {
                        name
                        slug
                    }
                    poster: tagsPosterByTagId {
                        media {
                            id
                            thumbnail
                            path
                            comment
                        }
                    }
                    photos: tagsPhotos {
                        media {
                            id
                            thumbnail
                            path
                            comment
                        }
                    }
                    releases: releasesTags {
                        release {
                            id
                            title
                            date
                            createdAt
                            url
                            ${releaseActorsFragment}
                            ${releaseTagsFragment}
                            ${releasePosterFragment}
                            ${siteFragment}
                        }
                    }
                }
            }
        `, {
            tagSlug,
        });

        return curateTag(tagBySlug);
    }

    async function fetchTags({ _commit }, {
        tagSlug,
        limit = 100,
        slugs = [],
        _group,
        _priority,
    }) {
        if (tagSlug) {
            return fetchTagBySlug(tagSlug);
        }

        const { tags } = await graphql(`
            query Tags($slugs: [String!] = [], $limit: Int = 100) {
              tags(filter: {slug: {in: $slugs}}, first: $limit) {
                id
                name
                slug
                poster: tagsPosterByTagId {
                    media {
                        thumbnail
                    }
                }
                group {
                    name
                    slug
                }
              }
            }
        `, {
            slugs,
            limit,
        });

        return tags.map(tag => curateTag(tag));
    }

    async function fetchTagReleases({ _commit }, tagId) {
        const releases = await get(`/tags/${tagId}/releases`, {
            filter: store.state.ui.filter,
            after: store.getters.after,
            before: store.getters.before,
        });

        return releases;
    }

    return {
        fetchTags,
        fetchTagReleases,
        fetchTagBySlug,
    };
}

export default initTagsActions;
