

import { map } from 'rxjs/operators';
import { ReplaySubject, Subject, Subscription, Observable } from 'rxjs';
import { ContributableViewContainer, Connection } from '@workbench-stack/extension-api';
import { handleRequests } from '@workbench-stack/extension-api';
import { TextDocumentLocationProviderIDRegistry, PanelViewWithComponent, PanelViewData } from '@workbench-stack/extension-api';

import { SubscriptionMap } from '../common';
import { ViewProviderRegistry } from '../../services/view-service';
import { IEditorInput } from '@workbench-stack/core';
import { IEditorWindowsService } from '../../services/windows-service';

// editors$,
// lastEditorId$,
// openingFiles$,
// editingFiles$,
// activeEditorId$,

// content$: combineLatest(openingFiles$, editingFiles$, activeEditorId$)
//  .pipe(
//      map(([ openingFiles, editingFiles, activeEditorId ]) => getFileContent(openingFiles, editingFiles, activeEditorId))
//  ),

/** @internal */
export class ClientEditorWindows {
    private subscriptions = new Subscription();

    constructor(
        connection: Connection,
        private windowService: IEditorWindowsService,
    ) {
        handleRequests(connection, 'editor-windows', this);
    }

    public $openWindow(content: IEditorInput): number {
        const nextEditorId = this.windowService.lastEditorId() + 1;
        this.windowService.openEditorWindow({ ...content, editorId: nextEditorId });

        return nextEditorId;
    }

    public $closeWindow(content: IEditorInput) {
        // TODO: close editor by ID
        this.windowService.closeEditorWindow(content);
    }

    public $updateWindow(content: IEditorInput) {
        // // TODO: update editor by ID and part content
        this.windowService.updateEditorWindow(content);
    }

    public $selectWindow(content: IEditorInput) {
        // TODO: activate editor by ID
        this.windowService.selectEditorWindow(content);
    }

    public unsubscribe(): void {
        this.subscriptions.unsubscribe();
    }
}
