import { ServiceBubble } from '../../../types/service-bubble-class.js';
import { CredentialType } from '@bubblelab/shared-schemas';
import { HubSpotParamsSchema, HubSpotResultSchema, } from './hubspot.schema.js';
/**
 * HubSpot CRM Service Bubble
 *
 * Comprehensive HubSpot CRM integration for managing contacts, companies, deals, and tickets.
 *
 * Features:
 * - Full CRUD operations for all CRM object types
 * - Advanced search with filter groups (AND/OR logic)
 * - Property-based data management
 * - Pagination support for large datasets
 *
 * Use cases:
 * - Lead management and contact synchronization
 * - Company and deal pipeline tracking
 * - Support ticket management
 * - CRM data enrichment and automation
 *
 * Security Features:
 * - OAuth 2.0 authentication with HubSpot
 * - Scoped access permissions
 * - Secure credential handling
 */
export class HubSpotBubble extends ServiceBubble {
    static type = 'service';
    static service = 'hubspot';
    static authType = 'oauth';
    static bubbleName = 'hubspot';
    static schema = HubSpotParamsSchema;
    static resultSchema = HubSpotResultSchema;
    static shortDescription = 'HubSpot CRM integration for contacts, companies, deals, and tickets';
    static longDescription = `
    HubSpot CRM service integration for comprehensive customer relationship management.

    Features:
    - Create, read, update, and search contacts, companies, deals, and tickets
    - Advanced search with filter groups supporting AND/OR logic
    - Flexible property-based data management
    - Pagination for handling large datasets

    Use cases:
    - Lead management and contact synchronization
    - Company tracking and deal pipeline management
    - Support ticket creation and tracking
    - CRM data enrichment and workflow automation

    Security Features:
    - OAuth 2.0 authentication with HubSpot
    - Scoped access permissions for CRM operations
    - Secure credential handling and validation
  `;
    static alias = 'crm';
    constructor(params = {
        operation: 'get_record',
        object_type: 'contacts',
        record_id: '',
    }, context) {
        super(params, context);
    }
    async testCredential() {
        const credential = this.chooseCredential();
        if (!credential) {
            throw new Error('HubSpot credentials are required');
        }
        try {
            const response = await fetch('https://api.hubapi.com/crm/v3/objects/contacts?limit=1', {
                headers: {
                    Authorization: `Bearer ${credential}`,
                },
            });
            return response.ok;
        }
        catch {
            return false;
        }
    }
    chooseCredential() {
        const { credentials } = this.params;
        if (!credentials || typeof credentials !== 'object') {
            throw new Error('No HubSpot credentials provided');
        }
        return credentials[CredentialType.HUBSPOT_CRED];
    }
    async makeHubSpotApiRequest(endpoint, method = 'GET', body) {
        const credential = this.chooseCredential();
        if (!credential) {
            throw new Error('HubSpot credentials are required');
        }
        const url = endpoint.startsWith('https://')
            ? endpoint
            : `https://api.hubapi.com${endpoint}`;
        const requestInit = {
            method,
            headers: {
                Authorization: `Bearer ${credential}`,
                'Content-Type': 'application/json',
            },
        };
        if (body && method !== 'GET') {
            requestInit.body = JSON.stringify(body);
        }
        const response = await fetch(url, requestInit);
        if (!response.ok) {
            const errorText = await response.text();
            throw new Error(`HubSpot API error (${response.status}): ${errorText}`);
        }
        const contentType = response.headers.get('content-type');
        if (contentType && contentType.includes('application/json')) {
            return await response.json();
        }
        return await response.text();
    }
    async performAction(context) {
        void context;
        const { operation } = this.params;
        try {
            const result = await (async () => {
                const parsedParams = this.params;
                switch (operation) {
                    case 'create_record':
                        return await this.createRecord(parsedParams);
                    case 'get_record':
                        return await this.getRecord(parsedParams);
                    case 'update_record':
                        return await this.updateRecord(parsedParams);
                    case 'search_records':
                        return await this.searchRecords(parsedParams);
                    default:
                        throw new Error(`Unsupported operation: ${operation}`);
                }
            })();
            return result;
        }
        catch (error) {
            return {
                operation,
                success: false,
                error: error instanceof Error ? error.message : 'Unknown error occurred',
            };
        }
    }
    async createRecord(params) {
        const { object_type, properties } = params;
        const response = await this.makeHubSpotApiRequest(`/crm/v3/objects/${object_type}`, 'POST', { properties });
        return {
            operation: 'create_record',
            success: true,
            record: {
                id: response.id,
                properties: response.properties,
                createdAt: response.createdAt,
                updatedAt: response.updatedAt,
                archived: response.archived,
            },
            error: '',
        };
    }
    async getRecord(params) {
        const { object_type, record_id, properties } = params;
        const queryParams = new URLSearchParams();
        if (properties && properties.length > 0) {
            queryParams.set('properties', properties.join(','));
        }
        const queryString = queryParams.toString();
        const endpoint = `/crm/v3/objects/${object_type}/${record_id}${queryString ? `?${queryString}` : ''}`;
        const response = await this.makeHubSpotApiRequest(endpoint, 'GET');
        return {
            operation: 'get_record',
            success: true,
            record: {
                id: response.id,
                properties: response.properties,
                createdAt: response.createdAt,
                updatedAt: response.updatedAt,
                archived: response.archived,
            },
            error: '',
        };
    }
    async updateRecord(params) {
        const { object_type, record_id, properties } = params;
        const response = await this.makeHubSpotApiRequest(`/crm/v3/objects/${object_type}/${record_id}`, 'PATCH', { properties });
        return {
            operation: 'update_record',
            success: true,
            record: {
                id: response.id,
                properties: response.properties,
                createdAt: response.createdAt,
                updatedAt: response.updatedAt,
                archived: response.archived,
            },
            error: '',
        };
    }
    async searchRecords(params) {
        const { object_type, filter_groups, properties, limit, after } = params;
        const body = {
            filterGroups: filter_groups.map((group) => ({
                filters: group.filters.map((filter) => {
                    const f = {
                        propertyName: filter.propertyName,
                        operator: filter.operator,
                    };
                    if (filter.value !== undefined)
                        f.value = filter.value;
                    if (filter.highValue !== undefined)
                        f.highValue = filter.highValue;
                    if (filter.values !== undefined)
                        f.values = filter.values;
                    return f;
                }),
            })),
            limit: limit || 10,
        };
        if (properties && properties.length > 0) {
            body.properties = properties;
        }
        if (after) {
            body.after = after;
        }
        const response = await this.makeHubSpotApiRequest(`/crm/v3/objects/${object_type}/search`, 'POST', body);
        return {
            operation: 'search_records',
            success: true,
            results: (response.results || []).map((r) => ({
                id: r.id,
                properties: r.properties,
                createdAt: r.createdAt,
                updatedAt: r.updatedAt,
                archived: r.archived,
            })),
            total: response.total,
            paging: response.paging,
            error: '',
        };
    }
}
//# sourceMappingURL=hubspot.js.map