import { from, of, Observable, Subscription } from 'rxjs';
import { ITextModel } from '@vscode/monaco-editor/esm/vs/editor/common/model';
import { URI } from '@vscode/monaco-editor/esm/vs/base/common/uri';
import { ResourceModelCollection } from './resource-model-collection';
import { ModelService } from './model-service';
import { catchError, take, switchMap, map, mergeMap, first, race } from 'rxjs/operators';
import { StaticServices } from '@vscode/monaco-editor/esm/vs/editor/standalone/browser/standaloneServices';
import 'jest';
import { combineLatestOrDefault } from '@workbench-stack/extension-api';


describe('Workbench - ResourceModelCollection', () => {
    test('resolve resource', async function () {
        const modelService = new ModelService();

        const collection = new ResourceModelCollection();
        const testContent = 'Hello Test';
        collection.registerProvider('test', {
            // tslint:disable-next-line:no-shadowed-variable
            provideTextContent: function (resource: URI): Observable<ITextModel> {
                if (resource.scheme === 'test') {

                    let modelContent = testContent;
                    let languageSelection = StaticServices.modeService.get().create('json');
                    setTimeout(function () { return Promise.resolve(modelService.createModel(modelContent, languageSelection.languageIdentifier.language, resource)); }, 10)

                }
                return of(null);
            },
        });


        collection.registerProvider('test', {
            // tslint:disable-next-line:no-shadowed-variable
            provideTextContent: function (resource: URI): Observable<ITextModel> {
                return of(null);
            },
        });

        collection.registerProvider('test', {
            // tslint:disable-next-line:no-shadowed-variable
            provideTextContent: function (resource: URI): Observable<ITextModel> {
                if (resource.scheme === 'test') {
                    let modelContent = testContent;
                    let languageSelection = StaticServices.modeService.get().create('json');
                    return of(modelService.createModel(modelContent, languageSelection.languageIdentifier.language, resource1));
                }
                return of(null);
            },
        });
        let resource = URI.from({ scheme: 'test', authority: null!, path: 'thePath' });
        let resource1 = URI.from({ scheme: 'test', authority: null!, path: 'thePath1' });
        let resource2 = URI.from({ scheme: 'test', authority: null!, path: 'thePath2' });

        // const val = await collection.resolvedTextModelContent(resource.toString())
        //     .pipe(take(1))
        //     .toPromise();
        // console.log('val--', val.map(y => console.log(y.uri.toString())));

        collection.resolvedTextModelContent(resource.toString())
        .subscribe(val => val.map(l => console.log(l.uri.toString())));
    });


});
