import { ServiceBubble } from '../../../types/service-bubble-class.js';
import { CredentialType, decodeCredentialPayload, } from '@bubblelab/shared-schemas';
import { ZendeskParamsSchema, ZendeskResultSchema, } from './zendesk.schema.js';
/**
 * Zendesk Service Bubble
 *
 * Zendesk integration for managing tickets, users, organizations, and help center articles.
 *
 * Features:
 * - Create, retrieve, update, and list tickets
 * - List and read ticket comments / replies
 * - List and retrieve users and organizations
 * - Unified search across all Zendesk resources
 * - Help Center article management
 *
 * Security Features:
 * - OAuth 2.0 authentication with Zendesk
 * - Subdomain-scoped API access
 * - Secure credential handling
 */
export class ZendeskBubble extends ServiceBubble {
    static type = 'service';
    static service = 'zendesk';
    static authType = 'oauth';
    static bubbleName = 'zendesk';
    static schema = ZendeskParamsSchema;
    static resultSchema = ZendeskResultSchema;
    static shortDescription = 'Zendesk integration for tickets, comments, users, and help center';
    static longDescription = `
    Zendesk customer support integration for ticket management and help center.

    Features:
    - Create, retrieve, update, and list support tickets
    - List ticket comments and add replies (public or internal notes)
    - List and retrieve users and organizations
    - Unified search across tickets, users, and organizations
    - Help Center article listing and retrieval

    Use cases:
    - Automated ticket triage and response
    - Customer support workflow automation
    - Help center content management
    - Support analytics and reporting

    Security Features:
    - OAuth 2.0 authentication with Zendesk
    - Subdomain-scoped API access
    - Secure credential handling and validation
  `;
    static alias = 'support';
    constructor(params = {
        operation: 'list_tickets',
    }, context) {
        super(params, context);
    }
    async testCredential() {
        const creds = this.parseCredentials();
        if (!creds) {
            throw new Error('Zendesk credentials are required');
        }
        const response = await fetch(`https://${creds.subdomain}.zendesk.com/api/v2/users/me.json`, {
            headers: {
                Authorization: `Bearer ${creds.accessToken}`,
                Accept: 'application/json',
            },
        });
        if (!response.ok) {
            const text = await response.text();
            throw new Error(`Zendesk API error (${response.status}): ${text}`);
        }
        return true;
    }
    /**
     * Zendesk credential format:
     * Base64-encoded JSON: { accessToken, subdomain }
     * The subdomain identifies which Zendesk instance to access.
     */
    parseCredentials() {
        const { credentials } = this.params;
        if (!credentials || typeof credentials !== 'object') {
            return null;
        }
        const zendeskCredRaw = credentials[CredentialType.ZENDESK_CRED];
        if (!zendeskCredRaw) {
            return null;
        }
        try {
            const parsed = decodeCredentialPayload(zendeskCredRaw);
            if (parsed.accessToken && parsed.subdomain) {
                return {
                    accessToken: parsed.accessToken,
                    subdomain: parsed.subdomain,
                };
            }
        }
        catch {
            // If decoding fails, treat the raw value as an access token (validator path)
            // In this case, we can't make API calls without subdomain
        }
        return null;
    }
    chooseCredential() {
        const creds = this.parseCredentials();
        return creds?.accessToken;
    }
    async makeZendeskApiRequest(endpoint, method = 'GET', body, options) {
        const creds = this.parseCredentials();
        if (!creds) {
            throw new Error('Invalid Zendesk credentials. Expected base64-encoded JSON with { accessToken, subdomain }.');
        }
        const url = endpoint.startsWith('https://')
            ? endpoint
            : `https://${creds.subdomain}.zendesk.com${endpoint}`;
        const headers = {
            Authorization: `Bearer ${creds.accessToken}`,
            Accept: 'application/json',
        };
        const requestInit = { method };
        if (options?.binary) {
            headers['Content-Type'] =
                options.contentType ?? 'application/octet-stream';
            requestInit.body = options.binary;
        }
        else {
            headers['Content-Type'] = 'application/json';
            if (body && method !== 'GET') {
                requestInit.body = JSON.stringify(body);
            }
        }
        requestInit.headers = headers;
        const response = await fetch(url, requestInit);
        if (!response.ok) {
            const errorText = await response.text();
            throw new Error(`Zendesk 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 'list_tickets':
                        return await this.listTickets(parsedParams);
                    case 'get_ticket':
                        return await this.getTicket(parsedParams);
                    case 'create_ticket':
                        return await this.createTicket(parsedParams);
                    case 'update_ticket':
                        return await this.updateTicket(parsedParams);
                    case 'list_ticket_comments':
                        return await this.listTicketComments(parsedParams);
                    case 'list_users':
                        return await this.listUsers(parsedParams);
                    case 'get_user':
                        return await this.getUser(parsedParams);
                    case 'list_organizations':
                        return await this.listOrganizations(parsedParams);
                    case 'get_organization':
                        return await this.getOrganization(parsedParams);
                    case 'search':
                        return await this.search(parsedParams);
                    case 'list_articles':
                        return await this.listArticles(parsedParams);
                    case 'get_article':
                        return await this.getArticle(parsedParams);
                    case 'list_ticket_fields':
                        return await this.listTicketFields();
                    case 'create_ticket_field':
                        return await this.createTicketField(parsedParams);
                    case 'delete_ticket_field':
                        return await this.deleteTicketField(parsedParams);
                    case 'list_macros':
                        return await this.listMacros(parsedParams);
                    case 'get_macro':
                        return await this.getMacro(parsedParams);
                    case 'apply_macro':
                        return await this.applyMacro(parsedParams);
                    case 'create_macro':
                        return await this.createMacro(parsedParams);
                    case 'update_macro':
                        return await this.updateMacro(parsedParams);
                    case 'delete_macro':
                        return await this.deleteMacro(parsedParams);
                    case 'search_macros':
                        return await this.searchMacros(parsedParams);
                    case 'upload_attachment':
                        return await this.uploadAttachment(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',
            };
        }
    }
    // ---- Ticket operations ----
    async listTickets(params) {
        const queryParams = new URLSearchParams();
        if (params.page)
            queryParams.set('page', String(params.page));
        if (params.per_page)
            queryParams.set('per_page', String(params.per_page));
        if (params.sort_by)
            queryParams.set('sort_by', params.sort_by);
        if (params.sort_order)
            queryParams.set('sort_order', params.sort_order);
        // If status filter, use the search API for filtering
        if (params.status) {
            const searchQuery = `type:ticket status:${params.status}`;
            queryParams.set('query', searchQuery);
            const queryString = queryParams.toString();
            const response = await this.makeZendeskApiRequest(`/api/v2/search.json?${queryString}`);
            return {
                operation: 'list_tickets',
                success: true,
                tickets: (response.results || []).map(this.mapTicket),
                count: response.count,
                next_page: response.next_page ?? null,
                error: '',
            };
        }
        const queryString = queryParams.toString();
        const endpoint = `/api/v2/tickets.json${queryString ? `?${queryString}` : ''}`;
        const response = await this.makeZendeskApiRequest(endpoint);
        return {
            operation: 'list_tickets',
            success: true,
            tickets: (response.tickets || []).map(this.mapTicket),
            count: response.count,
            next_page: response.next_page ?? null,
            error: '',
        };
    }
    async getTicket(params) {
        const response = await this.makeZendeskApiRequest(`/api/v2/tickets/${params.ticket_id}.json`);
        return {
            operation: 'get_ticket',
            success: true,
            ticket: this.mapTicket(response.ticket),
            error: '',
        };
    }
    async createTicket(params) {
        const ticketBody = {
            subject: params.subject,
            comment: {
                body: params.body,
                ...(params.uploads?.length ? { uploads: params.uploads } : {}),
            },
        };
        if (params.requester_email) {
            const requester = {
                email: params.requester_email,
            };
            if (params.requester_name) {
                requester.name = params.requester_name;
            }
            else {
                // Zendesk requires a name when creating a requester; derive from email
                requester.name = params.requester_email.split('@')[0];
            }
            ticketBody.requester = requester;
        }
        if (params.assignee_id)
            ticketBody.assignee_id = params.assignee_id;
        if (params.priority)
            ticketBody.priority = params.priority;
        if (params.type)
            ticketBody.type = params.type;
        if (params.tags)
            ticketBody.tags = params.tags;
        if (params.custom_fields)
            ticketBody.custom_fields = params.custom_fields;
        const response = await this.makeZendeskApiRequest('/api/v2/tickets.json', 'POST', { ticket: ticketBody });
        return {
            operation: 'create_ticket',
            success: true,
            ticket: this.mapTicket(response.ticket),
            error: '',
        };
    }
    async updateTicket(params) {
        const ticketBody = {};
        if (params.comment) {
            ticketBody.comment = {
                body: params.comment,
                public: params.public ?? true,
                ...(params.uploads?.length ? { uploads: params.uploads } : {}),
            };
        }
        else if (params.uploads?.length) {
            ticketBody.comment = {
                body: '',
                public: params.public ?? true,
                uploads: params.uploads,
            };
        }
        if (params.status)
            ticketBody.status = params.status;
        if (params.priority)
            ticketBody.priority = params.priority;
        if (params.assignee_id)
            ticketBody.assignee_id = params.assignee_id;
        if (params.tags)
            ticketBody.tags = params.tags;
        if (params.custom_fields)
            ticketBody.custom_fields = params.custom_fields;
        const response = await this.makeZendeskApiRequest(`/api/v2/tickets/${params.ticket_id}.json`, 'PUT', { ticket: ticketBody });
        return {
            operation: 'update_ticket',
            success: true,
            ticket: this.mapTicket(response.ticket),
            error: '',
        };
    }
    async listTicketFields() {
        const response = await this.makeZendeskApiRequest('/api/v2/ticket_fields.json');
        const fields = (response.ticket_fields || []).map((f) => ({
            id: f.id,
            type: f.type,
            title: f.title,
            description: f.description ?? null,
            active: f.active,
            required: f.required,
            custom_field_options: f.custom_field_options
                ? f.custom_field_options.map((o) => ({
                    name: o.name,
                    value: o.value,
                }))
                : null,
        }));
        return {
            operation: 'list_ticket_fields',
            success: true,
            ticket_fields: fields,
            error: '',
        };
    }
    async createTicketField(params) {
        const fieldBody = {
            type: params.type,
            title: params.title,
        };
        if (params.description)
            fieldBody.description = params.description;
        if (params.required !== undefined)
            fieldBody.required = params.required;
        if (params.active !== undefined)
            fieldBody.active = params.active;
        if (params.custom_field_options)
            fieldBody.custom_field_options = params.custom_field_options;
        if (params.tag)
            fieldBody.tag = params.tag;
        if (params.regexp_for_validation)
            fieldBody.regexp_for_validation = params.regexp_for_validation;
        const response = await this.makeZendeskApiRequest('/api/v2/ticket_fields.json', 'POST', { ticket_field: fieldBody });
        const f = response.ticket_field;
        return {
            operation: 'create_ticket_field',
            success: true,
            ticket_field: {
                id: f.id,
                type: f.type,
                title: f.title,
                active: f.active,
                required: f.required,
                custom_field_options: f.custom_field_options
                    ? f.custom_field_options.map((o) => ({
                        name: o.name,
                        value: o.value,
                    }))
                    : null,
            },
            error: '',
        };
    }
    async deleteTicketField(params) {
        await this.makeZendeskApiRequest(`/api/v2/ticket_fields/${params.ticket_field_id}.json`, 'DELETE');
        return {
            operation: 'delete_ticket_field',
            success: true,
            error: '',
        };
    }
    // ---- Comment operations ----
    async listTicketComments(params) {
        const queryParams = new URLSearchParams();
        if (params.page)
            queryParams.set('page', String(params.page));
        if (params.per_page)
            queryParams.set('per_page', String(params.per_page));
        if (params.sort_order)
            queryParams.set('sort_order', params.sort_order);
        const queryString = queryParams.toString();
        const endpoint = `/api/v2/tickets/${params.ticket_id}/comments.json${queryString ? `?${queryString}` : ''}`;
        const response = await this.makeZendeskApiRequest(endpoint);
        return {
            operation: 'list_ticket_comments',
            success: true,
            comments: (response.comments || []).map(this.mapComment),
            count: response.count,
            next_page: response.next_page ?? null,
            error: '',
        };
    }
    // ---- User operations ----
    async listUsers(params) {
        const queryParams = new URLSearchParams();
        if (params.page)
            queryParams.set('page', String(params.page));
        if (params.per_page)
            queryParams.set('per_page', String(params.per_page));
        if (params.query) {
            queryParams.set('query', params.query);
            if (params.role)
                queryParams.set('role', params.role);
            const queryString = queryParams.toString();
            const response = await this.makeZendeskApiRequest(`/api/v2/users/search.json?${queryString}`);
            return {
                operation: 'list_users',
                success: true,
                users: (response.users || []).map(this.mapUser),
                count: response.count,
                next_page: response.next_page ?? null,
                error: '',
            };
        }
        if (params.role)
            queryParams.set('role', params.role);
        const queryString = queryParams.toString();
        const endpoint = `/api/v2/users.json${queryString ? `?${queryString}` : ''}`;
        const response = await this.makeZendeskApiRequest(endpoint);
        return {
            operation: 'list_users',
            success: true,
            users: (response.users || []).map(this.mapUser),
            count: response.count,
            next_page: response.next_page ?? null,
            error: '',
        };
    }
    async getUser(params) {
        const response = await this.makeZendeskApiRequest(`/api/v2/users/${params.user_id}.json`);
        return {
            operation: 'get_user',
            success: true,
            user: this.mapUser(response.user),
            error: '',
        };
    }
    // ---- Organization operations ----
    async listOrganizations(params) {
        const queryParams = new URLSearchParams();
        if (params.page)
            queryParams.set('page', String(params.page));
        if (params.per_page)
            queryParams.set('per_page', String(params.per_page));
        if (params.query) {
            queryParams.set('name', params.query);
            const queryString = queryParams.toString();
            const response = await this.makeZendeskApiRequest(`/api/v2/organizations/search.json?${queryString}`);
            return {
                operation: 'list_organizations',
                success: true,
                organizations: (response.organizations || []).map(this.mapOrganization),
                count: response.count,
                next_page: response.next_page ?? null,
                error: '',
            };
        }
        const queryString = queryParams.toString();
        const endpoint = `/api/v2/organizations.json${queryString ? `?${queryString}` : ''}`;
        const response = await this.makeZendeskApiRequest(endpoint);
        return {
            operation: 'list_organizations',
            success: true,
            organizations: (response.organizations || []).map(this.mapOrganization),
            count: response.count,
            next_page: response.next_page ?? null,
            error: '',
        };
    }
    async getOrganization(params) {
        const response = await this.makeZendeskApiRequest(`/api/v2/organizations/${params.organization_id}.json`);
        return {
            operation: 'get_organization',
            success: true,
            organization: this.mapOrganization(response.organization),
            error: '',
        };
    }
    // ---- Search ----
    async search(params) {
        const queryParams = new URLSearchParams();
        queryParams.set('query', params.query);
        if (params.page)
            queryParams.set('page', String(params.page));
        if (params.per_page)
            queryParams.set('per_page', String(params.per_page));
        if (params.sort_by)
            queryParams.set('sort_by', params.sort_by);
        if (params.sort_order)
            queryParams.set('sort_order', params.sort_order);
        const queryString = queryParams.toString();
        const response = await this.makeZendeskApiRequest(`/api/v2/search.json?${queryString}`);
        return {
            operation: 'search',
            success: true,
            results: response.results || [],
            count: response.count,
            next_page: response.next_page ?? null,
            error: '',
        };
    }
    // ---- Help Center ----
    async listArticles(params) {
        const queryParams = new URLSearchParams();
        if (params.page)
            queryParams.set('page', String(params.page));
        if (params.per_page)
            queryParams.set('per_page', String(params.per_page));
        // Search endpoint vs list endpoint
        if (params.query) {
            queryParams.set('query', params.query);
            if (params.locale)
                queryParams.set('locale', params.locale);
            if (params.category_id)
                queryParams.set('category', params.category_id);
            if (params.section_id)
                queryParams.set('section', params.section_id);
            const queryString = queryParams.toString();
            const response = await this.makeZendeskApiRequest(`/api/v2/help_center/articles/search.json?${queryString}`);
            return {
                operation: 'list_articles',
                success: true,
                articles: (response.results || []).map(this.mapArticle),
                count: response.count,
                next_page: response.next_page ?? null,
                error: '',
            };
        }
        let basePath = '/api/v2/help_center';
        if (params.locale)
            basePath += `/${params.locale}`;
        if (params.section_id) {
            basePath += `/sections/${params.section_id}/articles.json`;
        }
        else if (params.category_id) {
            basePath += `/categories/${params.category_id}/articles.json`;
        }
        else {
            basePath += '/articles.json';
        }
        const queryString = queryParams.toString();
        const endpoint = `${basePath}${queryString ? `?${queryString}` : ''}`;
        const response = await this.makeZendeskApiRequest(endpoint);
        return {
            operation: 'list_articles',
            success: true,
            articles: (response.articles || []).map(this.mapArticle),
            count: response.count,
            next_page: response.next_page ?? null,
            error: '',
        };
    }
    async getArticle(params) {
        let endpoint = '/api/v2/help_center';
        if (params.locale)
            endpoint += `/${params.locale}`;
        endpoint += `/articles/${params.article_id}.json`;
        const response = await this.makeZendeskApiRequest(endpoint);
        return {
            operation: 'get_article',
            success: true,
            article: this.mapArticle(response.article),
            error: '',
        };
    }
    // ---- Macro operations ----
    async listMacros(params) {
        const queryParams = new URLSearchParams();
        if (params.page)
            queryParams.set('page', String(params.page));
        if (params.per_page)
            queryParams.set('per_page', String(params.per_page));
        if (params.category)
            queryParams.set('category', params.category);
        if (params.include)
            queryParams.set('include', params.include);
        // Use active or inactive endpoint
        let basePath = '/api/v2/macros';
        if (params.active === true) {
            basePath = '/api/v2/macros/active';
        }
        // For active === false, we list all and filter below
        const queryString = queryParams.toString();
        const endpoint = `${basePath}.json${queryString ? `?${queryString}` : ''}`;
        const response = await this.makeZendeskApiRequest(endpoint);
        let macros = (response.macros || []).map(this.mapMacro);
        // Filter inactive if requested
        if (params.active === false) {
            macros = macros.filter((m) => !m.active);
        }
        return {
            operation: 'list_macros',
            success: true,
            macros,
            count: response.count ?? macros.length,
            next_page: response.next_page ?? null,
            error: '',
        };
    }
    async getMacro(params) {
        const response = await this.makeZendeskApiRequest(`/api/v2/macros/${params.macro_id}.json`);
        return {
            operation: 'get_macro',
            success: true,
            macro: this.mapMacro(response.macro),
            error: '',
        };
    }
    async applyMacro(params) {
        const response = await this.makeZendeskApiRequest(`/api/v2/tickets/${params.ticket_id}/macros/${params.macro_id}/apply.json`);
        const result = response.result || {};
        return {
            operation: 'apply_macro',
            success: true,
            result: {
                ticket: result.ticket,
                comment: result.comment
                    ? {
                        body: result.comment.body,
                        html_body: result.comment.html_body,
                        public: result.comment.public,
                        scoped_body: result.comment.scoped_body,
                    }
                    : undefined,
            },
            error: '',
        };
    }
    async createMacro(params) {
        const macroBody = {
            title: params.title,
            actions: params.actions,
        };
        if (params.active !== undefined)
            macroBody.active = params.active;
        if (params.description)
            macroBody.description = params.description;
        if (params.restriction)
            macroBody.restriction = params.restriction;
        const response = await this.makeZendeskApiRequest('/api/v2/macros.json', 'POST', { macro: macroBody });
        return {
            operation: 'create_macro',
            success: true,
            macro: this.mapMacro(response.macro),
            error: '',
        };
    }
    async updateMacro(params) {
        const macroBody = {};
        if (params.title)
            macroBody.title = params.title;
        if (params.actions)
            macroBody.actions = params.actions;
        if (params.active !== undefined)
            macroBody.active = params.active;
        if (params.description !== undefined)
            macroBody.description = params.description;
        if (params.restriction)
            macroBody.restriction = params.restriction;
        const response = await this.makeZendeskApiRequest(`/api/v2/macros/${params.macro_id}.json`, 'PUT', { macro: macroBody });
        return {
            operation: 'update_macro',
            success: true,
            macro: this.mapMacro(response.macro),
            error: '',
        };
    }
    async deleteMacro(params) {
        await this.makeZendeskApiRequest(`/api/v2/macros/${params.macro_id}.json`, 'DELETE');
        return {
            operation: 'delete_macro',
            success: true,
            error: '',
        };
    }
    async searchMacros(params) {
        const queryParams = new URLSearchParams();
        queryParams.set('query', params.query);
        if (params.active !== undefined)
            queryParams.set('active', String(params.active));
        if (params.include)
            queryParams.set('include', params.include);
        if (params.page)
            queryParams.set('page', String(params.page));
        if (params.per_page)
            queryParams.set('per_page', String(params.per_page));
        const queryString = queryParams.toString();
        const response = await this.makeZendeskApiRequest(`/api/v2/macros/search.json?${queryString}`);
        return {
            operation: 'search_macros',
            success: true,
            macros: (response.macros || []).map(this.mapMacro),
            count: response.count ?? 0,
            next_page: response.next_page ?? null,
            error: '',
        };
    }
    static MIME_TYPES = {
        pdf: 'application/pdf',
        doc: 'application/msword',
        docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        xls: 'application/vnd.ms-excel',
        xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        ppt: 'application/vnd.ms-powerpoint',
        pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
        csv: 'text/csv',
        txt: 'text/plain',
        html: 'text/html',
        htm: 'text/html',
        json: 'application/json',
        xml: 'application/xml',
        zip: 'application/zip',
        png: 'image/png',
        jpg: 'image/jpeg',
        jpeg: 'image/jpeg',
        gif: 'image/gif',
        svg: 'image/svg+xml',
        webp: 'image/webp',
        mp4: 'video/mp4',
        mp3: 'audio/mpeg',
        wav: 'audio/wav',
    };
    inferContentType(filename, explicit) {
        if (explicit)
            return explicit;
        const ext = filename.split('.').pop()?.toLowerCase();
        if (ext && ext in ZendeskBubble.MIME_TYPES) {
            return ZendeskBubble.MIME_TYPES[ext];
        }
        return 'application/octet-stream';
    }
    async uploadAttachment(params) {
        const { filename, content, content_type } = params;
        let fileBuffer = Buffer.from(content, 'base64');
        const resolvedContentType = this.inferContentType(filename, content_type);
        // Strip UTF-8 BOM if present — Zendesk rejects text files with BOM
        // as "file type and file extension do not match"
        if (resolvedContentType.startsWith('text/') &&
            fileBuffer.length >= 3 &&
            fileBuffer[0] === 0xef &&
            fileBuffer[1] === 0xbb &&
            fileBuffer[2] === 0xbf) {
            fileBuffer = fileBuffer.subarray(3);
        }
        const response = await this.makeZendeskApiRequest(`/api/v2/uploads.json?filename=${encodeURIComponent(filename)}`, 'POST', undefined, {
            binary: fileBuffer,
            contentType: resolvedContentType,
        });
        const upload = response.upload;
        return {
            operation: 'upload_attachment',
            success: true,
            upload: {
                token: upload.token,
                attachment: {
                    id: upload.attachment.id,
                    file_name: upload.attachment.file_name,
                    content_url: upload.attachment.content_url,
                    size: upload.attachment.size,
                    content_type: upload.attachment.content_type,
                },
            },
            error: '',
        };
    }
    // ---- Mappers ----
    mapTicket = (t) => ({
        id: t.id,
        subject: t.subject,
        description: t.description,
        status: t.status,
        priority: t.priority,
        type: t.type,
        requester_id: t.requester_id,
        assignee_id: t.assignee_id,
        organization_id: t.organization_id,
        tags: t.tags,
        custom_fields: t.custom_fields,
        created_at: t.created_at,
        updated_at: t.updated_at,
    });
    mapComment = (c) => ({
        id: c.id,
        body: c.body,
        html_body: c.html_body,
        public: c.public,
        author_id: c.author_id,
        attachments: (c.attachments || []).map((a) => ({
            id: a.id,
            file_name: a.file_name,
            content_url: a.content_url,
            content_type: a.content_type,
            size: a.size,
        })),
        created_at: c.created_at,
    });
    mapUser = (u) => ({
        id: u.id,
        name: u.name,
        email: u.email,
        role: u.role,
        organization_id: u.organization_id,
        active: u.active,
        created_at: u.created_at,
    });
    mapOrganization = (o) => ({
        id: o.id,
        name: o.name,
        domain_names: o.domain_names,
        external_id: o.external_id,
        created_at: o.created_at,
    });
    mapMacro = (m) => ({
        id: m.id,
        title: m.title,
        description: m.description,
        active: m.active,
        actions: m.actions,
        restriction: m.restriction,
        created_at: m.created_at,
        updated_at: m.updated_at,
    });
    mapArticle = (a) => ({
        id: a.id,
        title: a.title,
        body: a.body,
        locale: a.locale,
        section_id: a.section_id,
        author_id: a.author_id,
        draft: a.draft,
        created_at: a.created_at,
        updated_at: a.updated_at,
    });
}
//# sourceMappingURL=zendesk.js.map