"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCustomer = void 0;
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
const extensions_core_1 = require("@awell-health/extensions-core");
const settings_1 = require("../../../settings");
const config_1 = require("./config");
const client_1 = require("../../client");
const lodash_1 = require("lodash");
exports.getCustomer = {
    key: 'getCustomer',
    title: 'Get customer',
    description: 'Retrieve information about a customer using the Desk API.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: true,
    onActivityCreated: async (payload, onComplete, onError) => {
        var _a;
        try {
            const { settings: { applicationId, chatApiToken, deskApiToken }, fields: { customerId }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    settings: settings_1.SettingsValidationSchema,
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const client = new client_1.SendbirdClient({
                applicationId,
                chatApiToken,
                deskApiToken,
            });
            const res = await client.deskApi.getCustomer(customerId);
            const customFields = (_a = res.data.customFields) === null || _a === void 0 ? void 0 : _a.reduce((acc, curr) => {
                return { ...acc, [curr.key]: curr.value };
            }, {});
            await onComplete({
                data_points: {
                    sendbirdId: res.data.sendbirdId,
                    project: String(res.data.project),
                    channelType: res.data.channelType,
                    displayName: res.data.displayName,
                    createdAt: new Date(res.data.createdAt).toISOString(),
                    customFields: !(0, lodash_1.isEmpty)(customFields)
                        ? JSON.stringify(customFields)
                        : '',
                },
            });
        }
        catch (err) {
            if (err instanceof zod_1.ZodError) {
                const error = (0, zod_validation_error_1.fromZodError)(err);
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: error.message },
                            error: {
                                category: 'WRONG_INPUT',
                                message: error.message,
                            },
                        },
                    ],
                });
            }
            else if ((0, client_1.isSendbirdDeskError)(err)) {
                const events = (0, client_1.sendbirdDeskErrorToActivityEvent)(err);
                await onError({ events });
            }
            else {
                const message = err.message;
                await onError({
                    events: [
                        {
                            date: new Date().toISOString(),
                            text: { en: message },
                            error: {
                                category: 'SERVER_ERROR',
                                message,
                            },
                        },
                    ],
                });
            }
        }
    },
};
//# sourceMappingURL=getCustomer.js.map