"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUser = void 0;
const zod_1 = require("zod");
const zod_validation_error_1 = require("zod-validation-error");
const date_fns_1 = require("date-fns");
const extensions_core_1 = require("@awell-health/extensions-core");
const settings_1 = require("../../../settings");
const config_1 = require("./config");
const client_1 = require("../../client");
exports.getUser = {
    key: 'getUser',
    title: 'Get user',
    description: 'Gets user using Chat API.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: false,
    onActivityCreated: async (payload, onComplete, onError) => {
        try {
            const { settings: { applicationId, chatApiToken, deskApiToken }, fields: { userId }, } = (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.chatApi.getUser(userId);
            await onComplete({
                data_points: {
                    nickname: res.data.nickname,
                    accessToken: res.data.access_token,
                    isActive: String(res.data.is_active),
                    createdAt: (0, date_fns_1.fromUnixTime)(res.data.created_at).toISOString(),
                    lastSeenAt: (0, date_fns_1.fromUnixTime)(res.data.last_seen_at).toISOString(),
                    hasEverLoggedIn: String(res.data.has_ever_logged_in),
                    metadata: JSON.stringify(res.data.metadata),
                },
            });
        }
        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.isSendbirdChatError)(err)) {
                const events = (0, client_1.sendbirdChatErrorToActivityEvent)(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=getUser.js.map