"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomFieldsValidationSchema = exports.MetadataValidationSchema = exports.JsonStringValidationSchema = void 0;
const lodash_1 = require("lodash");
const zod_1 = require("zod");
exports.JsonStringValidationSchema = zod_1.z
    .string()
    .transform((str, ctx) => {
    if ((0, lodash_1.isNil)(str) || (0, lodash_1.isEmpty)(str))
        return {};
    try {
        const parsedJson = JSON.parse(str);
        if ((0, lodash_1.isEmpty)(parsedJson)) {
            return {};
        }
        if (typeof parsedJson !== 'object' || Array.isArray(parsedJson)) {
            ctx.addIssue({
                code: 'custom',
                message: 'The value should represent an object',
            });
            return zod_1.z.NEVER;
        }
        const keys = Object.keys(parsedJson);
        if (keys.some((key) => /(.*,.*)+/.test(key))) {
            ctx.addIssue({
                code: 'custom',
                message: 'Each key of the JSON must not have a comma',
            });
            return zod_1.z.NEVER;
        }
        return parsedJson;
    }
    catch (e) {
        ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
        return zod_1.z.NEVER;
    }
});
exports.MetadataValidationSchema = exports.JsonStringValidationSchema.transform((jsonObject, ctx) => {
    if ((0, lodash_1.isNil)(jsonObject) || (0, lodash_1.isEmpty)(jsonObject))
        return {};
    try {
        const values = Object.values(jsonObject);
        if (values.length > 5) {
            ctx.addIssue({
                code: 'custom',
                message: 'JSON should have maximum of 5 key-value items',
            });
            return zod_1.z.NEVER;
        }
        if (values.some((val) => typeof val !== 'string')) {
            ctx.addIssue({
                code: 'custom',
                message: 'The value of each JSON key must be a string',
            });
            return zod_1.z.NEVER;
        }
        if (values.some((val) => val.length > 190)) {
            ctx.addIssue({
                code: 'custom',
                message: 'The value of each JSON key must not exceed 190 characters',
            });
            return zod_1.z.NEVER;
        }
        const keys = Object.keys(jsonObject);
        if (keys.some((key) => key.length > 128)) {
            ctx.addIssue({
                code: 'custom',
                message: 'Each key of the JSON must not exceed 128 characters',
            });
            return zod_1.z.NEVER;
        }
        return jsonObject;
    }
    catch (e) {
        ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
        return zod_1.z.NEVER;
    }
});
exports.CustomFieldsValidationSchema = exports.JsonStringValidationSchema.transform((jsonObject, ctx) => {
    if ((0, lodash_1.isNil)(jsonObject) || (0, lodash_1.isEmpty)(jsonObject))
        return '';
    try {
        const keys = Object.keys(jsonObject);
        const values = Object.values(jsonObject);
        const tooManyKeys = keys.length > 20;
        if (tooManyKeys) {
            ctx.addIssue({
                code: 'custom',
                message: 'The Customer object in Sendbird can only support a maximum of 20 custom fields',
            });
            return zod_1.z.NEVER;
        }
        const keysTooLong = keys.filter((key) => key.length > 20);
        if (keysTooLong.length > 0) {
            ctx.addIssue({
                code: 'custom',
                message: `The length of each JSON field's key must not exceed 20 characters. Please fix the following keys: ${keysTooLong.join(', ')}`,
            });
            return zod_1.z.NEVER;
        }
        const valuesTooLong = values.filter((val) => val.length > 190);
        if (valuesTooLong.length > 0) {
            ctx.addIssue({
                code: 'custom',
                message: `The value of each JSON field must not exceed 190 characters. Please fix the following values: ${valuesTooLong.join(', ')}`,
            });
            return zod_1.z.NEVER;
        }
        return JSON.stringify(jsonObject);
    }
    catch (e) {
        ctx.addIssue({ code: 'custom', message: 'Invalid JSON' });
        return zod_1.z.NEVER;
    }
});
//# sourceMappingURL=validation.js.map