import { URI } from '@vscode/monaco-editor/esm/vs/base/common/uri';
import * as network from '@vscode/monaco-editor/esm/vs/base/common/network';
import { ResourceModelCollection, ITextModelContentProvider } from './resource-model-collection';
import { IModelService } from '@workbench-stack/core';
import { injectable, inject } from 'inversify';
@injectable()
export class TextModelResolverService {


    constructor(
        private untitledEditorService,
        private resourceModelCollection: ResourceModelCollection,
        private modelService: IModelService,
    ) {

    }
    public createModelReference(resource: URI) {

        // Untitled Schema: go through cahced input
        if (resource.scheme === network.Schemas.untitled) {
            return this.untitledEditorService.loadOrCreate({ resource });
        }

        // InMemory Schema: go through filesStat service cache
        // get the files from apollo cache
        if (resource.scheme === network.Schemas.inMemory) {
            const cachedModel = this.modelService.getModel(resource);

            if (!cachedModel) {
                return Promise.reject(new Error('Cant resolve inmemory resource'));
            }
            return cachedModel;
        }

        this.resourceModelCollection.resolvedTextModelContent(resource.toString());
    }

    public registerTextModelContentProvider(scheme: string, provider: ITextModelContentProvider) {
        return this.resourceModelCollection.registerProvider(scheme, provider);
    }
}
