import { BehaviorSubject, NextObserver, Subscribable, Observable, of } from 'rxjs';
import { map, distinctUntilChanged, tap, mergeMap } from 'rxjs/operators';
import { ApolloClient } from 'apollo-client';
import { isEqual } from 'apollo-utilities';
import { ofType, ActionsObservable } from 'redux-observable';
import { EXTENSION_ACTION_TYPES } from '../constants';

export function getLocations(apolloClient: ApolloClient<any>, state$: Observable<any>): Observable<any> {
    return state$
        .pipe(
            map(state => ({ files: state.ext_documentPositionParams })),
            distinctUntilChanged((a, b) => isEqual(a, b)),
            map(x => x),
        );
}


export const openDocumentEpic: (action$: ActionsObservable<any>, state$: Observable<any>, { apolloClient }: { apolloClient: ApolloClient<any> }) => any
    = (action$, state$, { apolloClient }) => action$
        .pipe(
            ofType(EXTENSION_ACTION_TYPES.OPEN_DOCUMENT_EPIC),
            tap(() => console.log('action of type ', EXTENSION_ACTION_TYPES.OPEN_DOCUMENT_EPIC)),
            mergeMap(() => of({
                type: EXTENSION_ACTION_TYPES.OPEN_DOCUMENT,
            })),
        );
