import { graphql, get } from '../api';
import {
    releaseFields,
} from '../fragments';
import { curateTag } from '../curate';

function initTagsActions(store, _router) {
    async function fetchTagBySlug(tagSlug, limit = 100) {
        const { tagBySlug } = await graphql(`
            query Tag(
                $tagSlug:String!
                $limit:Int = 1000,
                $after:Date = "1900-01-01",
                $before:Date = "2100-01-01",
                $orderBy: [ReleasesTagsOrderBy!],
                $exclude: [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(
                        filter: {
                            release: {
                                date: {
                                    lessThan: $before,
                                    greaterThan: $after,
                                },
                                releasesTags: {
                                    none: {
                                        tag: {
                                            slug: {
                                                in: $exclude
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        first: $limit,
                        orderBy: $orderBy,
                    ) {
                        release {
                            ${releaseFields}
                        }
                    }
                }
            }
        `, {
            tagSlug,
            limit,
            after: store.getters.after,
            before: store.getters.before,
            orderBy: store.state.ui.range === 'upcoming' ? 'RELEASE_BY_RELEASE_ID__DATE_ASC' : 'RELEASE_BY_RELEASE_ID__DATE_DESC',
            exclude: store.state.ui.filter,
        });

        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;
