import z from "zod";
import { REGEX_HYPERLINK, REGEX_NUMBER, REGEX_PHONE } from "../utils/regex";
export const getButtonSchema = (t) => z.object({
    text: z.string().trim().min(1, t('quick_message.button_text_required') || 'Please enter button text').max(20, t('quick_message.button_text_max_length') || 'Maximum 20 characters'),
    action_type: z.enum(['text', 'url', 'tel'], { message: t('quick_message.invalid_action_type') || 'Invalid action type selected' }),
    action: z.string().nullish(),
}).superRefine((data, ctx) => {
    if (data.action_type === 'url' || data.action_type === 'tel') {
        if (!data.action || data.action?.trim() === '') {
            ctx.addIssue({
                code: 'custom',
                message: t('quick_message.button_action_required') || `Please enter a value for ${data.action_type === 'url' ? 'link' : 'phone number'}`,
                path: ['action'],
            });
        }
    }
});
export const getButtonContentSchema = (t) => z.object({
    type: z.literal('button'),
    text: z.string().trim().min(1, t('quick_message.text_required') || 'Please enter text').max(2000, t('quick_message.text_max_length') || 'Maximum 2000 characters'),
    buttons: z.array(getButtonSchema(t)).min(1, t('quick_message.at_least_one_button') || 'Please add at least 1 button').max(4, t('quick_message.max_four_buttons') || 'You can add up to 4 buttons only'),
});
const getMenuListItemSchema = (t) => z.object({
    file_url: z.string().nullish(),
    file_id: z.string().nullish(),
    title: z.string().trim().min(1, t('quick_message.title_required') || 'Please enter a title').max(80, t('quick_message.title_max_length') || 'Maximum 80 characters'),
    description: z.string().trim().min(1, t('quick_message.description_required') || 'Please enter a description').max(80, t('quick_message.description_max_length') || 'Maximum 80 characters'),
    buttons: z.array(getButtonSchema(t)).min(1, t('quick_message.at_least_one_button') || 'Please add at least 1 button').max(3, t('quick_message.max_three_buttons') || 'You can add up to 3 buttons per item only'),
});
export const getMenuListContentSchema = (t) => z.object({
    type: z.literal('menu_list'),
    menu_list: z.array(getMenuListItemSchema(t)).min(1, t('quick_message.at_least_one_menu_item') || 'Please add at least 1 menu item').max(10, t('quick_message.max_ten_menu_items') || 'You can add up to 10 menu items only'),
});
export const getBillEditSchema = (t) => z.object({
    value: z.string().check(z.regex(/^\d*\.?\d*$/, { message: `${t('please_input')} ${t('amount')}` }), z.minLength(1, { message: `${t('please_input')} ${t('amount')}` })),
});
export const getDialogEditSchema = (t) => z.object({
    event_name: z.enum(['ViewContent', 'AddToCart', 'Purchase', 'LeadSubmitted'], { message: t('quick_message.invalid_event') || 'Invalid event selected' }),
});
export const getDialogEditorSchema = (t, dialogType = 'input', field, allowEmpty = false) => {
    if (['full_address', 'order_address'].includes(dialogType)) {
        const checkAddress = z.string().check(z.trim(), z.minLength(1, { message: `${t('please_input')} ${t('address')}` }));
        const checkProvince = z.string().check(z.trim(), z.minLength(1, { message: `${t('please_input')} ${t('province')}` }));
        const checkDistrict = z.string().check(z.trim(), z.minLength(1, { message: `${t('please_input')} ${t('district')}` }));
        const checkSubDistrict = z.string().check(z.trim(), z.minLength(1, { message: `${t('please_input')} ${t('sub_district')}` }));
        const checkZipCode = z.string().check(z.trim(), z.minLength(1, { message: `${t('please_input')} ${t('zip_code')}` }));
        return z.object({
            [field]: allowEmpty
                ? z.nullable(z.object({}))
                : (dialogType === 'order_address'
                    ? z.object({
                        name: z.string().check(z.trim(), z.minLength(1, { message: `${t('please_input')} ${t('name')}` })),
                        phone_number: z.string().check(z.trim(), z.minLength(1, { message: `${t('please_input')} ${t('tel')}` })),
                        address: checkAddress,
                        province: checkProvince,
                        district: checkDistrict,
                        sub_district: checkSubDistrict,
                        zip_code: checkZipCode,
                    })
                    : z.object({
                        address: checkAddress,
                        province: checkProvince,
                        district: checkDistrict,
                        sub_district: checkSubDistrict,
                        zip_code: checkZipCode,
                    })),
        });
    }
    if (['file', 'checkbox', 'multiselect'].includes(dialogType)) {
        const isFile = dialogType === 'file';
        const itemSchema = isFile
            ? z.object({ name: z.string(), type: z.string(), url: z.string() })
            : z.object({ label: z.string(), value: z.string() });
        const check = z.array(itemSchema);
        if (allowEmpty) {
            return z.object({
                [field]: z.union([z.literal(''), check])
            });
        }
        return z.object({
            [field]: check.min(1, isFile ? 'Please select a file' : 'Please select at least one option')
        });
    }
    if (field.toLowerCase() === 'email') {
        const check = z.string().check(z.email());
        return z.object({
            [field]: allowEmpty ? z.union([z.literal(''), check]) : check
        });
    }
    if (field.toLowerCase() === 'phone') {
        const check = z.string().check(z.regex(REGEX_PHONE));
        return z.object({
            [field]: allowEmpty ? z.union([z.literal(''), check]) : check
        });
    }
    if (field.toLowerCase() === 'number') {
        const check = z.string().check(z.regex(REGEX_NUMBER));
        return z.object({
            [field]: allowEmpty ? z.union([z.literal(''), check]) : check
        });
    }
    if (field.toLowerCase() === 'url') {
        const check = z.string().check(z.regex(REGEX_HYPERLINK));
        return z.object({
            [field]: allowEmpty ? z.union([z.literal(''), check]) : check
        });
    }
    if (field.toLowerCase() === 'file') {
        const check = z.array(z.string()).min(1, t('quick_message.file_required') || 'Please select a file');
        return z.object({
            [field]: allowEmpty ? z.union([z.literal(''), check]) : check
        });
    }
    const check = z.string().check(z.trim());
    return z.object({
        [field]: allowEmpty ? z.union([z.literal(''), check]) : check
    });
};
