"use strict";
/**
 * Quantum Swarm Orchestrator Client
 *
 * A JavaScript client library for interacting with the Quantum Swarm Orchestrator API.
 * This library provides methods for registering agents, creating workflows,
 * contributing insights, building consensus, and more.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuantumSwarmClient = void 0;
const api_js_1 = require("../api.js");
/**
 * Client for interacting with the Quantum Swarm Orchestrator API
 */
class QuantumSwarmClient {
    /**
     * Creates a new QuantumSwarmClient
     * @param config Configuration options for the client
     */
    constructor(config) {
        this.config = {
            drupalBaseUrl: config?.drupalBaseUrl || '',
            apiBasePath: config?.apiBasePath || '/api/quantum-swarm',
            headers: {
                'Content-Type': 'application/json',
                ...config?.headers,
            },
            timeout: config?.timeout || 30000,
        };
    }
    /**
     * Registers a new agent with the quantum swarm orchestrator
     * @param agent The agent to register
     */
    async registerAgent(agent) {
        try {
            const response = await api_js_1.api.post(`${this.config.apiBasePath}/register-agent`, agent);
            return this.parseResponse(response);
        }
        catch (error) {
            throw this.handleError(error, 'Failed to register agent');
        }
    }
    /**
     * Creates a new cognitive workflow that orchestrates multiple quantum thought processes
     * @param name Name of the workflow
     * @param description Description of the workflow
     * @param agentIds Specific agent IDs to include, or leave empty for automatic selection
     * @param specializationAreas Areas of specialization needed for this workflow
     */
    async createCognitiveWorkflow(name, description, agentIds = [], specializationAreas = []) {
        try {
            const response = await api_js_1.api.post(`${this.config.apiBasePath}/create-workflow`, { name, description, agentIds, specializationAreas });
            return this.parseResponse(response);
        }
        catch (error) {
            throw this.handleError(error, 'Failed to create cognitive workflow');
        }
    }
    /**
     * Contributes an insight from an agent to a cognitive workflow
     * @param workflowId The ID of the workflow
     * @param agentId The ID of the contributing agent
     * @param insightType The type of insight
     * @param insightContent The content of the insight
     * @param confidence The confidence level for this insight (0-1)
     */
    async contributeInsight(workflowId, agentId, insightType, insightContent, confidence = 0.7) {
        try {
            const response = await api_js_1.api.post(`${this.config.apiBasePath}/contribute-insight`, { workflowId, agentId, insightType, insightContent, confidence });
            return this.parseResponse(response);
        }
        catch (error) {
            throw this.handleError(error, 'Failed to contribute insight');
        }
    }
    /**
     * Initiates a consensus-building process within a cognitive workflow
     * @param workflowId The ID of the workflow
     * @param topic The topic to reach consensus on
     * @param options Consensus configuration options
     */
    async initiateConsensus(workflowId, topic, options = {}) {
        try {
            const response = await api_js_1.api.post(`${this.config.apiBasePath}/initiate-consensus`, { workflowId, topic, options });
            return this.parseResponse(response);
        }
        catch (error) {
            throw this.handleError(error, 'Failed to initiate consensus');
        }
    }
    /**
     * Specializes an agent for specific types of quantum thought processes
     * @param agentId The ID of the agent to specialize
     * @param specializations Specialization areas and scores (0-1)
     */
    async specializeAgent(agentId, specializations) {
        try {
            const response = await api_js_1.api.post(`${this.config.apiBasePath}/specialize-agent`, { agentId, specializations });
            return this.parseResponse(response);
        }
        catch (error) {
            throw this.handleError(error, 'Failed to specialize agent');
        }
    }
    /**
     * Gets the status of a cognitive workflow
     * @param workflowId The ID of the workflow
     */
    async getWorkflowStatus(workflowId) {
        try {
            const response = await api_js_1.api.get(`${this.config.apiBasePath}/workflow/${workflowId}/status`);
            return this.parseResponse(response);
        }
        catch (error) {
            if (error.response?.status === 404) {
                return null;
            }
            throw this.handleError(error, 'Failed to get workflow status');
        }
    }
    /**
     * Gets all insights from a cognitive workflow
     * @param workflowId The ID of the workflow
     */
    async getWorkflowInsights(workflowId) {
        try {
            const response = await api_js_1.api.get(`${this.config.apiBasePath}/workflow/${workflowId}/insights`);
            return this.parseResponse(response);
        }
        catch (error) {
            throw this.handleError(error, 'Failed to get workflow insights');
        }
    }
    /**
     * Gets entanglement metrics for the quantum swarm
     */
    async getEntanglementMetrics() {
        try {
            const response = await api_js_1.api.get(`${this.config.apiBasePath}/entanglement/metrics`);
            return this.parseResponse(response);
        }
        catch (error) {
            throw this.handleError(error, 'Failed to get entanglement metrics');
        }
    }
    /**
     * Helper method to parse API responses
     */
    parseResponse(response) {
        if (!response || !response.data) {
            throw new Error('Invalid API response');
        }
        // Parse date strings into Date objects
        return this.parseDates(response.data);
    }
    /**
     * Helper method to parse date strings in the response
     */
    parseDates(obj) {
        if (obj === null || obj === undefined) {
            return obj;
        }
        if (Array.isArray(obj)) {
            return obj.map(item => this.parseDates(item));
        }
        if (typeof obj === 'object') {
            const newObj = {};
            for (const key in obj) {
                if (Object.prototype.hasOwnProperty.call(obj, key)) {
                    // Check if value is a date string (ISO format)
                    if (typeof obj[key] === 'string' &&
                        /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.\d{3})?Z$/.test(obj[key])) {
                        newObj[key] = new Date(obj[key]);
                    }
                    else {
                        newObj[key] = this.parseDates(obj[key]);
                    }
                }
            }
            return newObj;
        }
        return obj;
    }
    /**
     * Helper method to handle errors
     */
    handleError(error, defaultMessage) {
        if (error.response?.data?.error) {
            return new Error(`${defaultMessage}: ${error.response.data.error}`);
        }
        if (error.message) {
            return new Error(`${defaultMessage}: ${error.message}`);
        }
        return new Error(defaultMessage);
    }
}
exports.QuantumSwarmClient = QuantumSwarmClient;
exports.default = QuantumSwarmClient;
//# sourceMappingURL=index.js.map