"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SupportSettingsService = void 0;
class SupportSettingsService {
    settingsRepository;
    logger;
    constructor(settingsRepository, logger) {
        this.settingsRepository = settingsRepository;
        this.logger = logger;
    }
    async getSettings() {
        const settings = await this.settingsRepository.get();
        if (settings)
            return settings;
        const mongooseRepo = this.settingsRepository;
        if (typeof mongooseRepo.getOrCreate === "function") {
            return mongooseRepo.getOrCreate();
        }
        throw new Error("Support settings could not be loaded or created.");
    }
    async updateSettings(input) {
        const updated = await this.settingsRepository.update(input);
        if (updated) {
            this.logger.info("Support settings updated.");
        }
        return updated;
    }
    async getWelcomeMessage(firstName) {
        const settings = await this.getSettings();
        return settings.welcomeMessage.replace(/{{firstName}}/g, firstName ?? "there");
    }
    async getAutoReply(message) {
        const settings = await this.getSettings();
        const normalized = message.toLowerCase();
        const reply = settings.autoReplies.find((r) => r.enabled && normalized.includes(r.trigger.toLowerCase()));
        return reply?.response;
    }
    async getCannedResponses() {
        const settings = await this.getSettings();
        return settings.cannedResponses.filter((c) => c.enabled);
    }
}
exports.SupportSettingsService = SupportSettingsService;
