'use strict';

import {chunkArray} from "./utils";

let liveBarn = function() {
    return {
        getLiveBarnSurfaceList: ({fulltextPhrase, comingSoon, online, countries, sports, surfaceStatus, feedModes, cities, provinces, venues, ids, size, startKey, sort} = {}) => {
            return chunkArray(ids, 50)
                .reduce((promiseChain, idsChunk) => {
                    return promiseChain.then(chainResponse => {
                        const apiParams = {
                            ...(fulltextPhrase && {fulltextPhrase}),
                            ...(comingSoon && {comingSoon}),
                            ...(online && {online}),
                            ...(countries?.length && {countries: countries.join(',')}),
                            ...(sports?.length && {sports: sports.join(',')}),
                            ...(surfaceStatus?.length && {surfaceStatus: surfaceStatus.join(',')}),
                            ...(feedModes?.length && {feedModes: feedModes.join(',')}),
                            ...(cities?.length && {cities: cities.join(',')}),
                            ...(provinces?.length && {provinces: provinces.join(',')}),
                            ...(venues?.length && {venues: venues.join(',')}),
                            ...(idsChunk?.length && {ids: idsChunk.join(',')}),
                            ...(size && !ids?.length && {size}),
                            ...(startKey && {startKey}),
                            ...(sort && {sort}),
                        };
                        return this.__privates.auth.__client
                            .then(client => client.apis.livebarn.getLiveBarnSurfaceList(apiParams))
                            .then(response => ids?.length ? ({
								data: {
									items: [...chainResponse.data.items, ...response.data.items],
									size: chainResponse.data.size + response.data.size,
									startKey: null,
								},
							}) : response);
                    });
                }, Promise.resolve({data: {items: [], size: 0, startKey: null}}));
        },
        getLiveBarnSurfaceById: (id) => {
            return this.__privates.auth.__client
                .then(client => client.apis.livebarn.getLiveBarnSurfaceById({id}));
        },
        getLiveBarnVenueList: ({fulltextPhrase, comingSoon, online, hasActive, hasStream, countries, sports, surfaceStatus, feedModes, cities, provinces, venueStatus, postalCode, uuid, surfaces, ids, size, startKey, sort} = {}) => {
            return chunkArray(ids, 30)
                .reduce((promiseChain, idsChunk) => {
                    return promiseChain.then(chainResponse => {
                        const apiParams = {
                            ...(fulltextPhrase && {fulltextPhrase}),
                            ...(comingSoon !== undefined && {comingSoon}),
                            ...(online !== undefined && {online}),
                            ...(hasActive !== undefined && {hasActive}),
                            ...(hasStream !== undefined && {hasStream}),
                            ...(countries?.length && {countries: countries.join(',')}),
                            ...(sports?.length && {sports: sports.join(',')}),
                            ...(surfaceStatus?.length && {surfaceStatus: surfaceStatus.join(',')}),
                            ...(feedModes?.length && {feedModes: feedModes.join(',')}),
                            ...(cities?.length && {cities: cities.join(',')}),
                            ...(provinces?.length && {provinces: provinces.join(',')}),
                            ...(venueStatus?.length && {venueStatus: venueStatus.join(',')}),
                            ...(postalCode?.length && {postalCode: postalCode.join(',')}),
                            ...(uuid?.length && {uuid: uuid.join(',')}),
                            ...(surfaces?.length && {surfaces: surfaces.join(',')}),
                            ...(idsChunk?.length && {ids: idsChunk.join(',')}),
                            ...(size && !ids?.length && {size}),
                            ...(startKey && {startKey}),
                            ...(sort && {sort}),
                        };
                        return this.__privates.auth.__client
                            .then(client => client.apis.livebarn.getLiveBarnVenueList(apiParams))
                            .then(response => ids?.length ? ({
								data: {
									items: [...chainResponse.data.items, ...response.data.items],
									size: chainResponse.data.size + response.data.size,
									startKey: null,
								},
							}) : response);
                    });
                }, Promise.resolve({data: {items: [], size: 0, startKey: null}}));
        },
        getLiveBarnVenueById: (id) => {
            return this.__privates.auth.__client
                .then(client => client.apis.livebarn.getLiveBarnVenueById({id}));
        },
    };
};

export default liveBarn;
