import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import * as i0 from "@angular/core";
export class PromptManagerService {
    constructor() {
        this.customPrompts$ = new BehaviorSubject([]);
        this.nextPromptId = 1;
        this.customPrompts = this.customPrompts$.asObservable();
        // Load saved prompts from localStorage
        this.loadSavedPrompts();
    }
    loadSavedPrompts() {
        const saved = localStorage.getItem('customPrompts');
        if (saved) {
            try {
                const prompts = JSON.parse(saved);
                this.customPrompts$.next(prompts);
                // Update nextPromptId based on loaded prompts
                if (prompts.length > 0) {
                    const maxId = Math.max(...prompts.map((p) => parseInt(p.id.replace('custom_', ''))));
                    this.nextPromptId = maxId + 1;
                }
            }
            catch (e) {
                console.error('Failed to load saved prompts:', e);
            }
        }
    }
    savePrompts() {
        console.log('Saving prompts to localStorage:', this.customPrompts$.value);
        localStorage.setItem('customPrompts', JSON.stringify(this.customPrompts$.value));
    }
    createPrompt(name, properties) {
        const promptId = `custom_${this.nextPromptId++}`;
        const newPrompt = {
            id: promptId,
            name: name,
            value: promptId, // Use the same value as id for consistency
            properties: properties,
            isCustom: true,
            createdAt: new Date(),
            modifiedAt: new Date()
        };
        const prompts = [...this.customPrompts$.value, newPrompt];
        this.customPrompts$.next(prompts);
        this.savePrompts();
        return newPrompt;
    }
    updatePrompt(promptId, updates) {
        const prompts = this.customPrompts$.value.map(p => p.id === promptId
            ? { ...p, ...updates, modifiedAt: new Date() }
            : p);
        this.customPrompts$.next(prompts);
        this.savePrompts();
    }
    deletePrompt(promptId) {
        const prompts = this.customPrompts$.value.filter(p => p.id !== promptId);
        this.customPrompts$.next(prompts);
        this.savePrompts();
    }
    getPrompt(promptId) {
        return this.customPrompts$.value.find(p => p.id === promptId);
    }
    duplicatePrompt(promptId, newName) {
        const original = this.getPrompt(promptId);
        if (!original)
            return null;
        return this.createPrompt(newName, JSON.parse(JSON.stringify(original.properties)) // Deep clone properties
        );
    }
    // Get all prompts including built-in and custom
    getAllPromptOptions(builtInPrompts) {
        const customPromptsAsOptions = this.customPrompts$.value.map(cp => ({
            id: cp.id,
            name: cp.name + ' (Custom)',
            value: cp.value,
            properties: cp.properties
        }));
        return [...builtInPrompts, ...customPromptsAsOptions];
    }
    static { this.ɵfac = function PromptManagerService_Factory(t) { return new (t || PromptManagerService)(); }; }
    static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: PromptManagerService, factory: PromptManagerService.ɵfac, providedIn: 'root' }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(PromptManagerService, [{
        type: Injectable,
        args: [{
                providedIn: 'root'
            }]
    }], () => [], null); })();
//# sourceMappingURL=prompt-manager.service.js.map