"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.infobipErrorToActivityEvent = exports.isInfobipError = void 0;
const axios_1 = require("axios");
const lodash_1 = require("lodash");
const zod_1 = require("zod");
const errorSchema = zod_1.z.object({
    requestError: zod_1.z.object({
        serviceException: zod_1.z.object({
            messageId: zod_1.z.coerce.string(),
            text: zod_1.z.string(),
        }),
    }),
});
const isInfobipError = (error) => {
    var _a;
    if ((0, axios_1.isAxiosError)(error)) {
        const parseResult = errorSchema.safeParse((_a = error.response) === null || _a === void 0 ? void 0 : _a.data);
        return parseResult.success;
    }
    return false;
};
exports.isInfobipError = isInfobipError;
const infobipErrorToActivityEvent = (error) => {
    var _a, _b;
    const messageId = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data.requestError.serviceException.messageId;
    const text = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data.requestError.serviceException.text;
    if ((0, lodash_1.isNil)(text)) {
        return [];
    }
    return [
        {
            date: new Date().toISOString(),
            text: {
                en: messageId !== null && messageId !== void 0 ? messageId : text,
            },
            error: {
                category: 'SERVER_ERROR',
                message: text,
            },
        },
    ];
};
exports.infobipErrorToActivityEvent = infobipErrorToActivityEvent;
//# sourceMappingURL=error.js.map