"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.storageProvider = void 0;
const logger_1 = require("../config/logger");
/**
 * Singleton registry para o storage provider ativo.
 * O plugin UnifiedStorageProvider se registra aqui durante o activate().
 * O plugin uploads chama uploadFile() para enviar ao storage registrado.
 */
class StorageProviderRegistry {
    constructor() {
        this.provider = null;
        this.healthy = false;
        this.lastHealthStatus = {
            healthy: false,
            configured: false,
            reason: 'not_configured',
            message: 'Storage não inicializado.',
        };
    }
    register(provider) {
        this.provider = provider;
        this.lastHealthStatus = { healthy: true, configured: true, reason: 'ok', message: 'Wasabi inicializado.' };
        logger_1.logger.info('[StorageProvider] Provider registrado');
    }
    isRegistered() {
        return this.provider !== null;
    }
    isHealthy() {
        return this.healthy;
    }
    getLastHealthStatus() {
        return this.lastHealthStatus;
    }
    setHealthStatus(status) {
        this.lastHealthStatus = status;
        this.healthy = status.healthy;
    }
    async uploadFile(payload) {
        if (!this.provider) {
            throw new Error('Nenhum storage provider registrado. Ative o plugin UnifiedStorageProvider.');
        }
        return this.provider.upload(payload);
    }
    /** Atualiza o status de saúde — chamado periodicamente pelo UnifiedStorageProvider */
    async updateHealth() {
        if (!this.provider) {
            this.healthy = false;
            this.lastHealthStatus = {
                healthy: false,
                configured: false,
                reason: 'not_configured',
                message: 'Storage não configurado. Acesse Admin > Plugins > UnifiedStorageProvider.',
            };
            return;
        }
        try {
            const status = await this.provider.checkDetailedHealth();
            this.healthy = status.healthy;
            this.lastHealthStatus = status;
        }
        catch {
            this.healthy = false;
            this.lastHealthStatus = {
                healthy: false,
                configured: true,
                reason: 'unknown',
                message: 'Erro desconhecido ao verificar o storage.',
            };
        }
    }
    async getBuffer(path) {
        if (!this.provider)
            return null;
        return this.provider.getBuffer(path);
    }
    async deletePrefix(prefix) {
        if (!this.provider) {
            logger_1.logger.warn('[StorageProvider] deletePrefix chamado mas nenhum provider está registrado');
            return { deleted: 0 };
        }
        return this.provider.deletePrefix(prefix);
    }
    unregister() {
        this.provider = null;
        this.healthy = false;
        logger_1.logger.info('[StorageProvider] Provider removido');
    }
}
exports.storageProvider = new StorageProviderRegistry();
//# sourceMappingURL=StorageProvider.js.map