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

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

		const { network, connection: { releases, totalCount } } = await graphql(`
            query Network(
                $networkSlug: String!
                $limit:Int = 10,
                $offset:Int = 0,
                $after:Date = "1900-01-01",
                $before:Date = "2100-01-01",
                $afterTime:Datetime = "1900-01-01",
                $beforeTime:Datetime = "2100-01-01",
                $orderBy: [ReleasesOrderBy!]
                $exclude: [String!]
            ) {
				network: entityBySlugAndType(slug: $networkSlug, type: "network") {
                    id
                    name
                    slug
                    url
					sites: childEntities(
                        orderBy: [PRIORITY_DESC, NAME_ASC],
                    ) {
                        id
                        name
                        slug
                        url
						type
                        priority
                    }
                    parent {
                        id
                        name
                        slug
						type
                        url
                    }
                }
				connection: releasesConnection(
					first: $limit
					offset: $offset
					orderBy: $orderBy
					filter: {
						entity: {
							or: [
								{ parent: { slug: { equalTo: $networkSlug } } },
								{ parent: { parent: { slug: { equalTo: $networkSlug } } } }
							]
						}
						effectiveDate: {
							lessThan: $before,
							greaterThan: $after
						}
						releasesTagsConnection: {
							none: {
								tag: {
									slug: {
										in: $exclude
									}
								}
							}
						}
					}
				) {
					releases: nodes {
						${releaseFields}
					}
					totalCount
				}
            }
        `, {
			networkSlug,
			limit,
			offset: Math.max(0, (pageNumber - 1)) * limit,
			after,
			before,
			orderBy,
			afterTime: store.getters.after,
			beforeTime: store.getters.before,
			exclude: store.state.ui.filter,
		});

		return {
			network: curateEntity(network, releases),
			totalCount,
		};
	}

	async function fetchNetworks({ _commit }) {
		const { networks } = await graphql(`
            query Networks {
				networks: entities(
					orderBy: NAME_ASC
					filter: {
						type: {
							equalTo: "network"
						}
					}
				) {
                    id
                    name
                    slug
					type
                    url
                }
            }
        `);

		return networks.map((network) => curateEntity(network));
	}

	return {
		fetchNetworkBySlug,
		fetchNetworks,
	};
}

export default initNetworksActions;