import { graphql } from '../api';
import { releaseFields } from '../fragments';
import { curateEntity, curateRelease } from '../curate';
import getDateRange from '../get-date-range';

function initSitesActions(store, _router) {
	async function fetchSiteBySlug({ _commit }, {
		siteSlug,
		limit = 10,
		pageNumber = 1,
		range = 'latest',
	}) {
		const { before, after, orderBy } = getDateRange(range);

		const { site, connection: { releases, totalCount } } = await graphql(`
            query Site(
                $siteSlug: String!,
                $limit:Int = 100,
                $offset:Int = 0,
                $after:Date = "1900-01-01",
                $before:Date = "2100-01-01",
                $orderBy:[ReleasesOrderBy!]
                $exclude: [String!]
            ) {
				site: entityBySlugAndType(slug: $siteSlug, type: 2) {
                    name
                    slug
                    url
                    tags: entitiesTags {
                        tag {
                            id
                            slug
                            name
                        }
                    }
					network: parent {
                        id
                        name
                        slug
                        url
                    }
                    releasesConnection(
                        filter: {
							effectiveDate: {
								lessThan: $before,
								greaterThan: $after,
							},
							releasesTagsConnection: {
								none: {
									tag: {
										slug: {
											in: $exclude
										}
									}
								}
							}
                        }
                        first: $limit
						offset: $offset
                        orderBy: $orderBy
                    ) {
						releases: nodes {
							${releaseFields}
						}
						totalCount
                    }
                }
				connection: releasesConnection(
					first: $limit
					offset: $offset
					orderBy: $orderBy
					filter: {
						entity: {
							slug: {
								equalTo: $siteSlug
							}
						}
						effectiveDate: {
							lessThan: $before,
							greaterThan: $after
						}
						releasesTagsConnection: {
							none: {
								tag: {
									slug: {
										in: $exclude
									}
								}
							}
						}
					}
				) {
					releases: nodes {
						${releaseFields}
					}
					totalCount
				}
            }
        `, {
			siteSlug,
			limit,
			offset: Math.max(0, (pageNumber - 1)) * limit,
			after,
			before,
			orderBy,
			exclude: store.state.ui.filter,
		});

		return {
			site: curateEntity(site),
			releases: releases.map((release) => curateRelease(release)),
			totalCount,
		};
	}

	async function fetchSites({ _commit }, { limit = 100 }) {
		const { sites } = await graphql(`
            query Sites(
                $actorSlug: String!
                $limit:Int = 100,
                $after:Date = "1900-01-01",
                $before:Date = "2100-01-01",
            ) {
				site: entity {
                    name
                    slug
                    url
                }
            }
        `, {
			limit,
			after: store.getters.after,
			before: store.getters.before,
		});

		return sites;
	}

	return {
		fetchSiteBySlug,
		fetchSites,
	};
}

export default initSitesActions;