import { createDefaultSharedCoreModule, createDefaultCoreModule } from '../default-module.mjs';
import { inject } from '../dependency-injection.mjs';
import { LangiumGrammarAstReflection } from '../languages/generated/ast.mjs';
import { EmptyFileSystem } from '../workspace/file-system-provider.mjs';
import './uri-utils.mjs';
import { URI } from '../../../../../vscode-uri@3.0.8/node_modules/vscode-uri/lib/esm/index.mjs';

/******************************************************************************
 * Copyright 2023 TypeFox GmbH
 * This program and the accompanying materials are made available under the
 * terms of the MIT License, which is available in the project root.
 ******************************************************************************/
const minimalGrammarModule = {
    Grammar: () => undefined,
    LanguageMetaData: () => ({
        caseInsensitive: false,
        fileExtensions: ['.langium'],
        languageId: 'langium'
    })
};
const minimalSharedGrammarModule = {
    AstReflection: () => new LangiumGrammarAstReflection()
};
function createMinimalGrammarServices() {
    const shared = inject(createDefaultSharedCoreModule(EmptyFileSystem), minimalSharedGrammarModule);
    const grammar = inject(createDefaultCoreModule({ shared }), minimalGrammarModule);
    shared.ServiceRegistry.register(grammar);
    return grammar;
}
/**
 * Load a Langium grammar for your language from a JSON string. This is used by several services,
 * most notably the parser builder which interprets the grammar to create a parser.
 */
function loadGrammarFromJson(json) {
    var _a;
    const services = createMinimalGrammarServices();
    const astNode = services.serializer.JsonSerializer.deserialize(json);
    services.shared.workspace.LangiumDocumentFactory.fromModel(astNode, URI.parse(`memory://${(_a = astNode.name) !== null && _a !== void 0 ? _a : 'grammar'}.langium`));
    return astNode;
}

export { loadGrammarFromJson };
