"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var CustomVariableSeedingService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomVariableSeedingService = void 0;
const common_1 = require("@nestjs/common");
const custom_variables_service_1 = require("../custom-variables/custom-variables.service");
const path = require("path");
const glob_1 = require("glob");
let CustomVariableSeedingService = CustomVariableSeedingService_1 = class CustomVariableSeedingService {
    constructor(customVariablesService) {
        this.customVariablesService = customVariablesService;
        this.logger = new common_1.Logger(CustomVariableSeedingService_1.name);
    }
    async seed() {
        const existingVariables = await this.customVariablesService.findAll();
        const existingVariableMap = new Map(existingVariables.map((v) => [v.key, v]));
        this.logger.log('Syncing custom variables from files...');
        const seedFiles = await (0, glob_1.glob)(path.join(__dirname, 'data', 'custom-variables', '*.{ts,js}'));
        if (seedFiles.length === 0) {
            this.logger.log('No custom variable seed files found.');
            return;
        }
        for (const filePath of seedFiles) {
            try {
                const variableModule = await Promise.resolve(`${filePath}`).then(s => require(s));
                const { key, value, enabled, is_freq_used } = variableModule;
                const fileName = path.basename(filePath);
                if (!key || value === undefined) {
                    this.logger.warn(`Skipping malformed seed file: ${fileName}`);
                    continue;
                }
                if (existingVariableMap.has(key)) {
                    this.logger.log(`Variable with key "${key}" already exists, skipping.`);
                }
                else {
                    await this.customVariablesService.create({
                        key,
                        value,
                        enabled: enabled !== false,
                        is_freq_used: is_freq_used === true,
                    });
                    this.logger.log(`Variable "${key}" seeded.`);
                }
            }
            catch (error) {
                this.logger.error(`Failed to seed custom variable from ${filePath}`, error.stack);
            }
        }
    }
};
exports.CustomVariableSeedingService = CustomVariableSeedingService;
exports.CustomVariableSeedingService = CustomVariableSeedingService = CustomVariableSeedingService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [custom_variables_service_1.CustomVariablesService])
], CustomVariableSeedingService);
//# sourceMappingURL=custom-variable-seeding.service.js.map