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