"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
// --- Mock data store ---
const store = new Map();
vitest_1.vi.mock('../data-store/index.js', () => ({
    dataStore: {
        saveTemplate: vitest_1.vi.fn(async (t) => { store.set(t.id, structuredClone(t)); }),
        getTemplate: vitest_1.vi.fn(async (id) => {
            const v = store.get(id);
            return v ? structuredClone(v) : null;
        }),
        listTemplates: vitest_1.vi.fn(async () => Array.from(store.values()).map(v => structuredClone(v))),
        deleteTemplate: vitest_1.vi.fn(async (id) => store.delete(id)),
    },
}));
const templates_js_1 = require("./templates.js");
let manager;
(0, vitest_1.beforeEach)(() => {
    store.clear();
    manager = new templates_js_1.TemplateManager();
});
(0, vitest_1.describe)('DEFAULT_TEMPLATES', () => {
    (0, vitest_1.it)('should have 12 predefined templates', () => {
        (0, vitest_1.expect)(templates_js_1.DEFAULT_TEMPLATES).toHaveLength(12);
    });
    (0, vitest_1.it)('should include all required Turkish scene names', () => {
        const names = templates_js_1.DEFAULT_TEMPLATES.map(t => t.name);
        for (const expected of [
            'Ev Ortamı', 'Yatak Odası', 'Sahil', 'Park Yürüyüşü',
            'Spor Salonu', 'Yoga Seansı', 'Kafe Molası', 'Restoran Akşamı',
            'Ofis', 'İş Toplantısı', 'Gece Kulübü', 'Rooftop Bar',
        ]) {
            (0, vitest_1.expect)(names).toContain(expected);
        }
    });
    (0, vitest_1.it)('should cover all 6 categories with at least 2 templates each', () => {
        const categoryCounts = new Map();
        for (const t of templates_js_1.DEFAULT_TEMPLATES) {
            categoryCounts.set(t.category, (categoryCounts.get(t.category) || 0) + 1);
        }
        for (const cat of ['indoor', 'outdoor', 'sport', 'social', 'professional', 'nightlife']) {
            (0, vitest_1.expect)(categoryCounts.get(cat)).toBeGreaterThanOrEqual(2);
        }
    });
    (0, vitest_1.it)('should have placeholder tokens in promptTemplate', () => {
        for (const t of templates_js_1.DEFAULT_TEMPLATES) {
            (0, vitest_1.expect)(t.promptTemplate).toContain('{subject}');
            (0, vitest_1.expect)(t.promptTemplate).toContain('{clothing}');
            (0, vitest_1.expect)(t.promptTemplate).toContain('{camera}');
        }
    });
    (0, vitest_1.it)('should have English prompt templates', () => {
        for (const t of templates_js_1.DEFAULT_TEMPLATES) {
            (0, vitest_1.expect)(t.promptTemplate).toMatch(/\{subject\}/);
        }
    });
});
(0, vitest_1.describe)('TemplateManager.initDefaults', () => {
    (0, vitest_1.it)('should save all default templates when store is empty', async () => {
        await manager.initDefaults();
        (0, vitest_1.expect)(store.size).toBe(12);
    });
    (0, vitest_1.it)('should not overwrite existing templates', async () => {
        const modified = { ...templates_js_1.DEFAULT_TEMPLATES[0], name: 'Custom Template' };
        const id = templates_js_1.DEFAULT_TEMPLATES[0].name.toLowerCase().replace(/\s+/g, '-');
        const now = new Date().toISOString();
        const fullTemplate = { ...modified, id, createdAt: now, updatedAt: now };
        store.set(fullTemplate.id, fullTemplate);
        await manager.initDefaults();
        const saved = store.get(id);
        (0, vitest_1.expect)(saved.name).toBe('Custom Template');
        (0, vitest_1.expect)(store.size).toBe(1);
    });
});
(0, vitest_1.describe)('TemplateManager.getTemplate', () => {
    (0, vitest_1.it)('should return template by id', async () => {
        store.set('t1', { id: 't1', name: 'Test', category: 'indoor', promptTemplate: '{subject}', environment: { lighting: 'soft', timeOfDay: 'day' }, defaultAspectRatio: '1:1', createdAt: '', updatedAt: '' });
        const result = await manager.getTemplate('t1');
        (0, vitest_1.expect)(result).not.toBeNull();
        (0, vitest_1.expect)(result.name).toBe('Test');
    });
    (0, vitest_1.it)('should return null for non-existent id', async () => {
        const result = await manager.getTemplate('nonexistent');
        (0, vitest_1.expect)(result).toBeNull();
    });
});
(0, vitest_1.describe)('TemplateManager.listTemplates', () => {
    (0, vitest_1.beforeEach)(async () => {
        await manager.initDefaults();
    });
    (0, vitest_1.it)('should return all templates when no category filter', async () => {
        const all = await manager.listTemplates();
        (0, vitest_1.expect)(all).toHaveLength(12);
    });
    (0, vitest_1.it)('should filter by category', async () => {
        const outdoor = await manager.listTemplates('outdoor');
        (0, vitest_1.expect)(outdoor.length).toBe(2);
        for (const t of outdoor) {
            (0, vitest_1.expect)(t.category).toBe('outdoor');
        }
    });
    (0, vitest_1.it)('should return empty array for non-matching category', async () => {
        const result = await manager.listTemplates('nonexistent');
        (0, vitest_1.expect)(result).toHaveLength(0);
    });
});
(0, vitest_1.describe)('TemplateManager.createTemplate', () => {
    (0, vitest_1.it)('should create a template with generated id and timestamps', async () => {
        const created = await manager.createTemplate({
            name: 'Rooftop',
            category: 'social',
            promptTemplate: '{subject} on a rooftop, {clothing}, city skyline, {camera}',
            environment: { lighting: 'twilight', timeOfDay: 'evening' },
            defaultAspectRatio: '16:9',
        });
        (0, vitest_1.expect)(created.id).toBeTruthy();
        (0, vitest_1.expect)(created.createdAt).toBeTruthy();
        (0, vitest_1.expect)(created.updatedAt).toBeTruthy();
        (0, vitest_1.expect)(created.name).toBe('Rooftop');
        (0, vitest_1.expect)(store.has(created.id)).toBe(true);
    });
});
(0, vitest_1.describe)('TemplateManager.updateTemplate', () => {
    (0, vitest_1.it)('should update fields and set new updatedAt', async () => {
        await manager.initDefaults();
        const original = Array.from(store.values())[0];
        // Set updatedAt to a past time to ensure the update produces a different timestamp
        const pastTime = '2020-01-01T00:00:00.000Z';
        store.set(original.id, { ...original, updatedAt: pastTime });
        const updated = await manager.updateTemplate(original.id, { name: 'Updated Name' });
        (0, vitest_1.expect)(updated.name).toBe('Updated Name');
        (0, vitest_1.expect)(updated.id).toBe(original.id);
        (0, vitest_1.expect)(updated.createdAt).toBe(original.createdAt);
        (0, vitest_1.expect)(updated.updatedAt).not.toBe(pastTime);
    });
    (0, vitest_1.it)('should throw for non-existent template', async () => {
        await (0, vitest_1.expect)(manager.updateTemplate('nope', { name: 'X' })).rejects.toThrow('Template not found');
    });
    (0, vitest_1.it)('should not allow overriding id or createdAt', async () => {
        await manager.initDefaults();
        const original = Array.from(store.values())[0];
        const updated = await manager.updateTemplate(original.id, {
            id: 'hacked-id',
            createdAt: '1999-01-01T00:00:00Z',
        });
        (0, vitest_1.expect)(updated.id).toBe(original.id);
        (0, vitest_1.expect)(updated.createdAt).toBe(original.createdAt);
    });
});
(0, vitest_1.describe)('TemplateManager.deleteTemplate', () => {
    (0, vitest_1.it)('should delete an existing template', async () => {
        await manager.initDefaults();
        const firstId = Array.from(store.keys())[0];
        const result = await manager.deleteTemplate(firstId);
        (0, vitest_1.expect)(result).toBe(true);
        (0, vitest_1.expect)(store.has(firstId)).toBe(false);
    });
});
(0, vitest_1.describe)('TemplateManager.fillTemplate', () => {
    (0, vitest_1.it)('should replace all placeholders', () => {
        const template = {
            id: 't1', name: 'Test', category: 'outdoor',
            promptTemplate: '{subject}, at the beach, {clothing}, {camera}',
            environment: { lighting: 'sun', timeOfDay: 'day' },
            defaultAspectRatio: '1:1', createdAt: '', updatedAt: '',
        };
        const result = manager.fillTemplate(template, {
            subject: 'a young woman with brown hair',
            clothing: 'wearing a red dress',
            camera: 'shot on Canon R5 85mm f/1.4',
        });
        (0, vitest_1.expect)(result).toBe('a young woman with brown hair, at the beach, wearing a red dress, shot on Canon R5 85mm f/1.4');
    });
    (0, vitest_1.it)('should leave unmatched placeholders as-is', () => {
        const template = {
            id: 't1', name: 'Test', category: 'outdoor',
            promptTemplate: '{subject}, {clothing}, {unknown}',
            environment: { lighting: 'sun', timeOfDay: 'day' },
            defaultAspectRatio: '1:1', createdAt: '', updatedAt: '',
        };
        const result = manager.fillTemplate(template, { subject: 'person' });
        (0, vitest_1.expect)(result).toBe('person, {clothing}, {unknown}');
    });
    (0, vitest_1.it)('should handle empty vars', () => {
        const template = {
            id: 't1', name: 'Test', category: 'outdoor',
            promptTemplate: '{subject} at park',
            environment: { lighting: 'sun', timeOfDay: 'day' },
            defaultAspectRatio: '1:1', createdAt: '', updatedAt: '',
        };
        const result = manager.fillTemplate(template, {});
        (0, vitest_1.expect)(result).toBe('{subject} at park');
    });
});
(0, vitest_1.describe)('templateManager singleton', () => {
    (0, vitest_1.it)('should be an instance of TemplateManager', () => {
        (0, vitest_1.expect)(templates_js_1.templateManager).toBeInstanceOf(templates_js_1.TemplateManager);
    });
});
//# sourceMappingURL=templates.test.js.map