import type { DefaultSharedModuleContext, LangiumServices, LangiumSharedServices, Module, PartialLangiumServices } from 'langium';
import { createDefaultModule, createDefaultSharedModule, inject } from 'langium';
import { JstqlGeneratedModule, JstqlGeneratedSharedModule } from './generated/module.js';
import { JstqlValidator, registerValidationChecks } from './jstql-validator.js';

/**
 * Declaration of custom services - add your own service classes here.
 */
export type JstqlAddedServices = {
    validation: {
        JstqlValidator: JstqlValidator
    }
}

/**
 * Union of Langium default services and your custom services - use this as constructor parameter
 * of custom service classes.
 */
export type JstqlServices = LangiumServices & JstqlAddedServices

/**
 * Dependency injection module that overrides Langium default services and contributes the
 * declared custom services. The Langium defaults can be partially specified to override only
 * selected services, while the custom services must be fully specified.
 */
export const JstqlModule: Module<JstqlServices, PartialLangiumServices & JstqlAddedServices> = {
    validation: {
        JstqlValidator: () => new JstqlValidator()
    }
};

/**
 * Create the full set of services required by Langium.
 *
 * First inject the shared services by merging two modules:
 *  - Langium default shared services
 *  - Services generated by langium-cli
 *
 * Then inject the language-specific services by merging three modules:
 *  - Langium default language-specific services
 *  - Services generated by langium-cli
 *  - Services specified in this file
 *
 * @param context Optional module context with the LSP connection
 * @returns An object wrapping the shared services and the language-specific services
 */
export function createJstqlServices(context: DefaultSharedModuleContext): {
    shared: LangiumSharedServices,
    Jstql: JstqlServices
} {
    const shared = inject(
        createDefaultSharedModule(context),
        JstqlGeneratedSharedModule
    );
    const Jstql = inject(
        createDefaultModule({ shared }),
        JstqlGeneratedModule,
        JstqlModule
    );
    shared.ServiceRegistry.register(Jstql);
    registerValidationChecks(Jstql);
    return { shared, Jstql };
}
