/**
 * Copyright 2013-2026 the original author or authors from the JHipster project.
 *
 * This file is part of the JHipster project, see https://www.jhipster.tech/
 * for more information.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { builtInJDLApplicationConfig } from "../../jhipster/application-options.js";
import JDLApplicationDefinition from "./built-in-options/jdl-application-definition.js";
import { buildApplicationTokens } from "./built-in-options/tokens/application-tokens.js";
import { deploymentTokens } from "./built-in-options/tokens/deployment-tokens.js";
import JDLParser from "./parsing/jdl-parser.js";
import { buildTokens, createJDLLexer } from "./parsing/lexer/lexer.js";
import { checkConfigKeys, checkTokens } from "./parsing/self-checks/parsing-system-checker.js";
const mergeDefinition = (definition, defaultDefinition) => {
    return {
        validatorConfig: {
            ...defaultDefinition.validatorConfig,
            ...definition.validatorConfig,
        },
        optionsValues: {
            ...defaultDefinition.optionsValues,
            ...definition.optionsValues,
        },
        optionsTypes: {
            ...defaultDefinition.optionsTypes,
            ...definition.optionsTypes,
        },
        quotedOptionNames: [...defaultDefinition.quotedOptionNames, ...definition.quotedOptionNames],
        tokenConfigs: [...defaultDefinition.tokenConfigs, ...definition.tokenConfigs],
    };
};
export const createRuntime = (definition) => {
    const newDefinition = definition ? mergeDefinition(definition, builtInJDLApplicationConfig) : builtInJDLApplicationConfig;
    const propertyValidations = newDefinition.validatorConfig;
    const applicationDefinition = new JDLApplicationDefinition({
        optionValues: newDefinition.optionsValues,
        optionTypes: newDefinition.optionsTypes,
        quotedOptionNames: newDefinition.quotedOptionNames,
    });
    let tokens;
    let lexer;
    let parser;
    return {
        get tokens() {
            if (!tokens) {
                const applicationTokens = buildApplicationTokens(newDefinition.tokenConfigs);
                tokens = buildTokens({ applicationTokens, deploymentTokens });
                checkConfigKeys(tokens, Object.keys(propertyValidations));
            }
            return tokens;
        },
        get lexer() {
            if (!lexer) {
                lexer = createJDLLexer(this.tokens);
            }
            return lexer;
        },
        get parser() {
            if (!parser) {
                parser = new JDLParser(this.tokens);
                parser.parse();
                const rules = parser.getGAstProductions();
                checkTokens(Object.values(this.tokens), Object.values(rules));
            }
            return parser;
        },
        applicationDefinition,
        propertyValidations,
    };
};
