"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
const typescript_template_language_service_decorator_1 = require("typescript-template-language-service-decorator");
const language_service_1 = __importDefault(require("./language-service"));
const get_value_from_expression_1 = __importDefault(require("./lib/get-value-from-expression"));
const configuration_1 = require("./configuration");
const get_spans_1 = __importDefault(require("./lib/get-spans"));
const logger_1 = __importDefault(require("./logger"));
class MySqlPlugin {
    constructor(typescript) {
        this.config = new configuration_1.Configuration();
        this.typescript = typescript;
    }
    create(info) {
        this.config.update(info.config);
        const logger = new logger_1.default(info.project, this.config);
        const templateSettings = this.getTemplateSettings(this.config, info.project, logger);
        const service = new language_service_1.default({
            host: info.serverHost,
            config: this.config,
            logger
        });
        const plugin = typescript_template_language_service_decorator_1.decorateWithTemplateLanguageService(this.typescript, info.languageService, info.project, service, templateSettings, {
            logger
        });
        return plugin;
    }
    onConfigurationChanged(config) {
        this.config.update(config);
    }
    getTemplateSettings(config, project, logger) {
        return {
            get tags() {
                return config.tags;
            },
            enableForStringWithSubstitutions: true,
            getSubstitutions: (contents, _, node) => {
                var _a;
                const program = project.getLanguageService().getProgram();
                const checker = (_a = program) === null || _a === void 0 ? void 0 : _a.getTypeChecker();
                const spans = get_spans_1.default(node);
                const parts = [];
                let lastIndex = 0;
                for (const span of spans) {
                    parts.push(contents.slice(lastIndex, span.start));
                    const expression = span.expression;
                    if (checker) {
                        const value = get_value_from_expression_1.default(expression, checker, logger);
                        parts.push(value);
                    }
                    else {
                        const value = 'x'.repeat(span.end - span.start - 2);
                        parts.push(`'${value}'`);
                    }
                    lastIndex = span.end;
                }
                parts.push(contents.slice(lastIndex));
                return parts.join('');
            }
        };
    }
}
module.exports = (modules) => {
    return new MySqlPlugin(modules.typescript);
};
//# sourceMappingURL=index.js.map