function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
import Dexie from 'dexie';
/**
 * Simplified database for Notes workspace
 * - One workspace per API (not multiple projects)
 * - Notes are files for brainstorming (code, diagrams, markdown)
 */ class NotesDatabase extends Dexie {
    constructor(){
        super('brainfish-notes'), _define_property(this, "workspaces", void 0), _define_property(this, "notes", void 0), _define_property(this, "editorStates", void 0);
        this.version(1).stores({
            // Single workspace per API
            workspaces: 'id, apiSpecUrl, updatedAt',
            // Notes/files in workspace
            notes: 'id, workspaceId, [workspaceId+path], updatedAt',
            // Editor state per workspace
            editorStates: 'workspaceId'
        });
    }
}
// Singleton database instance
export const db = new NotesDatabase();
/**
 * Generate a workspace ID from API spec URL
 */ export function getWorkspaceId(apiSpecUrl) {
    // Simple hash for consistent ID
    let hash = 0;
    for(let i = 0; i < apiSpecUrl.length; i++){
        const char = apiSpecUrl.charCodeAt(i);
        hash = (hash << 5) - hash + char;
        hash = hash & hash; // Convert to 32-bit integer
    }
    return `ws-${Math.abs(hash).toString(16)}`;
}
/**
 * Database operations - simplified for notes workspace
 */ export const dbOperations = {
    // Workspace operations (auto-created, one per API)
    async getOrCreateWorkspace (apiSpecUrl, name) {
        const id = getWorkspaceId(apiSpecUrl);
        let workspace = await db.workspaces.get(id);
        if (!workspace) {
            workspace = {
                id,
                name,
                apiSpecUrl,
                createdAt: new Date(),
                updatedAt: new Date()
            };
            await db.workspaces.add(workspace);
        }
        return workspace;
    },
    async getWorkspace (id) {
        return db.workspaces.get(id);
    },
    async updateWorkspace (id, updates) {
        await db.workspaces.update(id, {
            ...updates,
            updatedAt: new Date()
        });
    },
    // Note/file operations
    async createNote (workspaceId, path, content = '') {
        const note = {
            id: crypto.randomUUID(),
            workspaceId,
            path,
            content,
            createdAt: new Date(),
            updatedAt: new Date()
        };
        await db.notes.add(note);
        // Update workspace timestamp
        await db.workspaces.update(workspaceId, {
            updatedAt: new Date()
        });
        return note;
    },
    async getNote (workspaceId, path) {
        return db.notes.where('[workspaceId+path]').equals([
            workspaceId,
            path
        ]).first();
    },
    async listNotes (workspaceId) {
        return db.notes.where('workspaceId').equals(workspaceId).toArray();
    },
    async updateNote (workspaceId, path, content) {
        await db.notes.where('[workspaceId+path]').equals([
            workspaceId,
            path
        ]).modify({
            content,
            updatedAt: new Date()
        });
        await db.workspaces.update(workspaceId, {
            updatedAt: new Date()
        });
    },
    async deleteNote (workspaceId, path) {
        await db.notes.where('[workspaceId+path]').equals([
            workspaceId,
            path
        ]).delete();
        await db.workspaces.update(workspaceId, {
            updatedAt: new Date()
        });
    },
    async renameNote (workspaceId, oldPath, newPath) {
        await db.notes.where('[workspaceId+path]').equals([
            workspaceId,
            oldPath
        ]).modify({
            path: newPath,
            updatedAt: new Date()
        });
        await db.workspaces.update(workspaceId, {
            updatedAt: new Date()
        });
    },
    // Editor state operations
    async getEditorState (workspaceId) {
        return db.editorStates.get(workspaceId);
    },
    async saveEditorState (state) {
        await db.editorStates.put(state);
    },
    // Clear all notes in workspace (reset)
    async clearWorkspace (workspaceId) {
        await db.notes.where('workspaceId').equals(workspaceId).delete();
        await db.editorStates.delete(workspaceId);
    }
};
