import { BaseEngine } from "@memberjunction/core";
import { UUIDsEqual } from "@memberjunction/global";
/**
 * Caches Knowledge Hub metadata: entity documents, vector indexes, vector databases,
 * content sources, content types, content source types, and content file types.
 * Provides helper methods for lookups and filtering. Uses BaseEngine for automatic
 * caching and entity-event auto-refresh.
 */
export class KnowledgeHubMetadataEngine extends BaseEngine {
    constructor() {
        super(...arguments);
        this._entityDocuments = [];
        this._vectorIndexes = [];
        this._contentSources = [];
        this._contentTypes = [];
        this._contentSourceTypes = [];
        this._contentFileTypes = [];
    }
    /**
     * Returns the global instance of the class. This is a singleton class, so there
     * is only one instance of it in the application. Do not directly create new instances
     * of it, always use this method to get the instance.
     */
    static get Instance() {
        return super.getInstance();
    }
    async Config(forceRefresh, contextUser, provider) {
        const c = [
            {
                Type: 'entity',
                EntityName: 'MJ: Entity Documents',
                PropertyName: '_entityDocuments',
                CacheLocal: true
            },
            {
                Type: 'entity',
                EntityName: 'MJ: Vector Indexes',
                PropertyName: '_vectorIndexes',
                CacheLocal: true
            },
            {
                Type: 'entity',
                EntityName: 'MJ: Content Sources',
                PropertyName: '_contentSources',
                CacheLocal: true
            },
            {
                Type: 'entity',
                EntityName: 'MJ: Content Types',
                PropertyName: '_contentTypes',
                CacheLocal: true
            },
            {
                Type: 'entity',
                EntityName: 'MJ: Content Source Types',
                PropertyName: '_contentSourceTypes',
                CacheLocal: true
            },
            {
                Type: 'entity',
                EntityName: 'MJ: Content File Types',
                PropertyName: '_contentFileTypes',
                CacheLocal: true
            }
        ];
        await this.Load(c, provider, forceRefresh, contextUser);
    }
    // ================================================================
    // Cached data getters
    // ================================================================
    /** All entity documents in the system (both active and inactive) */
    get EntityDocuments() {
        return this._entityDocuments;
    }
    /** All vector indexes in the system */
    get VectorIndexes() {
        return this._vectorIndexes;
    }
    /** All content sources */
    get ContentSources() {
        return this._contentSources;
    }
    /** All content types */
    get ContentTypes() {
        return this._contentTypes;
    }
    /** All content source types (Web, RSS, Email, etc.) */
    get ContentSourceTypes() {
        return this._contentSourceTypes;
    }
    /** All content file types (.pdf, .html, etc.) */
    get ContentFileTypes() {
        return this._contentFileTypes;
    }
    // ================================================================
    // Helper methods
    // ================================================================
    /** Returns only entity documents with Status = 'Active' */
    GetActiveEntityDocuments() {
        return this._entityDocuments.filter(d => d.Status === 'Active');
    }
    /** Find an entity document by ID (case-insensitive UUID comparison) */
    GetEntityDocumentById(id) {
        if (!id)
            return undefined;
        return this._entityDocuments.find(d => UUIDsEqual(d.ID, id));
    }
    /** Find all entity documents for a given entity name (case-insensitive) */
    GetEntityDocumentsForEntity(entityName) {
        if (!entityName)
            return [];
        const lower = entityName.trim().toLowerCase();
        return this._entityDocuments.filter(d => d.Entity?.trim().toLowerCase() === lower);
    }
    /** Find a vector index by ID (case-insensitive UUID comparison) */
    GetVectorIndexById(id) {
        if (!id)
            return undefined;
        return this._vectorIndexes.find(v => UUIDsEqual(v.ID, id));
    }
    /**
     * Returns distinct entity names that have active entity documents (for dropdowns).
     * Sorted alphabetically.
     */
    GetEntitiesWithDocuments() {
        const nameSet = new Set();
        for (const doc of this._entityDocuments) {
            if (doc.Status === 'Active' && doc.Entity) {
                nameSet.add(doc.Entity);
            }
        }
        return Array.from(nameSet).sort();
    }
}
//# sourceMappingURL=knowledgeHubMetadata.js.map