import { graphql } from '../api';
import { releasesFragment, releaseFragment } from '../fragments';
import { curateRelease } from '../curate';

function initReleasesActions(store, _router) {
    async function fetchReleases({ _commit }, { limit = 100 }) {
        const { releases } = await graphql(`
            query Releases(
                $limit:Int = 1000,
                $after:Date = "1900-01-01",
                $before:Date = "2100-01-01",
                $isNew:[Boolean!] = [true,false]
                $orderBy:[ReleasesOrderBy!],
                $exclude: [String!]
            ) {
                ${releasesFragment}
            }
        `, {
            limit,
            after: store.getters.after,
            before: store.getters.before,
            isNew: store.getters.isNew,
            orderBy: store.state.ui.range === 'upcoming' ? 'DATE_ASC' : 'DATE_DESC',
            exclude: store.state.ui.filter,
        });

        return releases.map(release => curateRelease(release));
    }

    async function searchReleases({ _commit }, { query, limit = 20 }) {
        const res = await graphql(`
            query SearchReleases(
                $query: String!
                $limit: Int = 20
            ) {
                releases: searchReleases(
                    query: $query
                    first: $limit
                ) {
                    release {
                        id
                        title
                        slug
                        date
                        url
                        isNew
                        site {
                            id
                            slug
                            name
                            url
                            network {
                                id
                                slug
                                name
                                url
                            }
                        }
                        actors: releasesActors {
                            actor {
                                id
                                slug
                                name
                            }
                        }
                        tags: releasesTags(orderBy: TAG_BY_TAG_ID__PRIORITY_DESC) {
                            tag {
                                id
                                name
                                slug
                            }
                        }
                        poster: releasesPosterByReleaseId {
                            media {
                                thumbnail
                            }
                        }
                    }
                }
            }
        `, {
            query,
            limit,
        });

        if (!res) return [];

        return res.releases.map(release => curateRelease(release.release));
    }

    async function fetchReleaseById({ _commit }, releaseId) {
        // const release = await get(`/releases/${releaseId}`);

        const { release } = await graphql(`
            query Release($releaseId:Int!) {
                ${releaseFragment}
            }
        `, {
            releaseId: Number(releaseId),
        });

        return curateRelease(release);
    }

    return {
        fetchReleases,
        fetchReleaseById,
        searchReleases,
    };
}

export default initReleasesActions;
