"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 CustomSnippetSeedingService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomSnippetSeedingService = void 0;
const common_1 = require("@nestjs/common");
const custom_snippets_service_1 = require("../custom-snippets/custom-snippets.service");
const path = require("path");
const glob_1 = require("glob");
let CustomSnippetSeedingService = CustomSnippetSeedingService_1 = class CustomSnippetSeedingService {
    constructor(customSnippetsService) {
        this.customSnippetsService = customSnippetsService;
        this.logger = new common_1.Logger(CustomSnippetSeedingService_1.name);
    }
    async seed() {
        const existingSnippets = await this.customSnippetsService.findAll();
        const existingSnippetsMap = new Map();
        existingSnippets.forEach((s) => existingSnippetsMap.set(s.prefix, s));
        this.logger.log('Syncing custom snippets from files...');
        const seedFiles = await (0, glob_1.glob)(path.join(__dirname, 'data', 'custom-snippets', '*.{ts,js}'));
        if (seedFiles.length === 0) {
            this.logger.warn('No custom snippet seed files found.');
            return;
        }
        for (const filePath of seedFiles) {
            try {
                const snippetModule = await Promise.resolve(`${filePath}`).then(s => require(s));
                const { prefix, description, body } = snippetModule;
                const fileName = path.basename(filePath);
                if (!prefix || !description || !body || body.length === 0) {
                    this.logger.warn(`Skipping malformed seed file: ${fileName}`);
                    continue;
                }
                const existingSnippet = existingSnippetsMap.get(prefix);
                if (existingSnippet) {
                    this.logger.log(`Snippet with prefix "${prefix}" already exists, skipping.`);
                }
                else {
                    await this.customSnippetsService.create({
                        prefix,
                        description,
                        body,
                    });
                    this.logger.log(`Snippet "${prefix}" seeded.`);
                }
            }
            catch (error) {
                this.logger.error(`Failed to seed custom snippet from ${filePath}`, error.stack);
            }
        }
    }
};
exports.CustomSnippetSeedingService = CustomSnippetSeedingService;
exports.CustomSnippetSeedingService = CustomSnippetSeedingService = CustomSnippetSeedingService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [custom_snippets_service_1.CustomSnippetsService])
], CustomSnippetSeedingService);
//# sourceMappingURL=custom-snippet-seeding.service.js.map