

import 'jest';
import { ExtHostDocumentsAndEditorsShape, IDocumentsAndEditorsDelta } from '@workbench-stack/extension-api/lib/protocol/common.protocol';
import { TestConfigurationService } from '@workbench-stack/server-core';
import { ModelService } from '../../services/model-service';
import { EditorService } from '../../services/editor-service';
import * as monaco from '@vscode/monaco-editor/esm/vs/editor/editor.api';
import { ITextModel, ITextBufferFactory, EndOfLineSequence } from '@vscode/monaco-editor/esm/vs/editor/common/model';
import { first, map, take, tap, distinctUntilChanged, pairwise } from 'rxjs/operators';
import { BehaviorSubject, from, combineLatest, Scheduler } from 'rxjs';
import { DocumentAndEditorState, TextEditorSnapshot } from './client-document-and-editors-helpers';
import { TestScheduler } from 'rxjs/testing';
import { MainThreadDocumentsAndEditors } from './client-document-and-editors';
const scheduler = () => new TestScheduler((a, b) => expect(a).toEqual(b))

describe('ClientThreadDocumentsAndEditors', () => {
    document.body.innerHTML =
        '<div>' +
        '  <div id="test" />' +
        '</div>';

    let deltas: IDocumentsAndEditorsDelta[] = [];
    // function myCreateTestCodeEditor(model: ITextModel | undefined): TestCodeEditor {
    //     return createTestCodeEditor({
    //         model: model,

    //     })
    // }


    // beforeEach(() => {
    //     deltas.length = 0;
    //     const configService = new TestConfigurationService();
    //     configService.setUserConfiguration('editor', {})
    // });


    test('documentAndEditor', async () => {
        const editorService = new EditorService();
        const modelService = new ModelService();

        const delta = combineLatest(editorService.listCodeEditors(), modelService.getModels())
            .pipe(
                map(([editors, models]) => {
                    let documents: Set<ITextModel> = new Set();
                    models.map(m => {
                        documents.add(m);
                    });
                    let textEditors: Map<string, TextEditorSnapshot> = new Map();
                    editors.map(ed => {
                        textEditors.set(ed.id, new TextEditorSnapshot(ed.editor as any));
                    });
                    const activeEditor = editorService.activeEditors()[0] ? editorService.activeEditors()[0].getId() : null;
                    const newState = new DocumentAndEditorState(documents, textEditors, activeEditor);
                    return newState;
                }),
                pairwise(), // to compare previous state with current state
                map(([a, b]) => DocumentAndEditorState.compute(a, b)),
                tap(x => console.log(x)),
            );
        delta.subscribe(data => data);

        const modelUri = monaco.Uri.parse('a://b/foo.json'); // a made up unique URI for our model
        const model = monaco.editor.createModel('', 'json', modelUri);
        // Create the actual Monaco editor.
        const editor = monaco.editor.create(document.getElementById('test'), {
            lineNumbers: 'on',
            model,
        });
        const editor2 = monaco.editor.create(document.getElementById('test'), {
            lineNumbers: 'on',
            model,
        });
        // add to service
        editorService.addEditor(editor as any);
        modelService.addModel(model as any);

        editorService.addEditor(editor2 as any);
        editorService.changeActiveEditor(editor2.getId());
        // scheduler().run(({ cold, expectObservable }) => {


        //     expectObservable(
        //         delta.pipe(m => {
        //             console.log('---delta output', m);
        //             return m;
        //         }),
        //     );

        // });
        // await delta.pipe(
        //     take(1),
        // ).toPromise();
    });

    describe('ExtHostDocumentsAndEditors', () => {

    
    })
});
