"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.salesApiErrorToActivityEvent = exports.isSalesApiError = void 0;
const axios_1 = require("axios");
const lodash_1 = require("lodash");
const zod_1 = require("zod");
const salesApiErrorSchema = zod_1.z.object({
    errors: zod_1.z.array(zod_1.z.object({
        error: zod_1.z.object({
            resource: zod_1.z.string().optional(),
            field: zod_1.z.string().optional(),
            code: zod_1.z.string(),
            message: zod_1.z.string(),
            details: zod_1.z.string().optional(),
        }),
        meta: zod_1.z.object({}),
    })),
    meta: zod_1.z.object({}),
});
const isSalesApiError = (error) => {
    var _a;
    if ((0, axios_1.isAxiosError)(error)) {
        const parseResult = salesApiErrorSchema.safeParse((_a = error.response) === null || _a === void 0 ? void 0 : _a.data);
        return parseResult.success;
    }
    return false;
};
exports.isSalesApiError = isSalesApiError;
const salesApiErrorToActivityEvent = (error) => {
    var _a, _b;
    const errorData = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data;
    if ((0, lodash_1.isNil)(errorData) || ((_b = errorData.errors) === null || _b === void 0 ? void 0 : _b.length) === 0) {
        return [];
    }
    return errorData.errors.map((error) => {
        var _a;
        return {
            date: new Date().toISOString(),
            text: {
                en: (0, lodash_1.isNil)(error.error.details) ? error.error.code : error.error.message,
            },
            error: {
                category: 'SERVER_ERROR',
                message: (_a = error.error.details) !== null && _a !== void 0 ? _a : error.error.message,
            },
        };
    });
};
exports.salesApiErrorToActivityEvent = salesApiErrorToActivityEvent;
//# sourceMappingURL=error.js.map