import {
  getWhatsappContent,
  getWhatsappStatus,
  getWhatsappCategory,
  getWhatsappCta,
  bytesConversion,
  getWhatsappDocPreview,
  getWhatsappQuickReply,
  getWhatsappAutoFill,
  getWhatsappCarouselButtonView,
} from '../utils';
import mockdata from '../../mockdata';
import { HOST_ICS, PHONE_NUMBER, QUICK_REPLY } from '../constants';
import { WHATSAPP_MEDIA_TYPES } from '../../Whatsapp/constants';

const {
  whatsappPreviewTemplateData,
  whatsappPreviewTemplateData2,
  whatsappPreviewTemplateDataVid,
  whatsappPreviewTemplateDataDoc,
  getWhatsappStatusOutput1,
  getWhatsappStatusOutput2,
  getWhatsappCategoryOutput1,
  getWhatsappCategoryOutput2,
  getWhatsappDocPreviewOutput,
  getWhatsappDocPreviewOutput1,
  whatsappPreviewTemplateData3,
  whatsappPreviewTemplateData3Result,
  whatsappPreviewTemplateData3QuickReplyResult,
  getWhatsappAutoFillData,
} = mockdata;

describe('Test utils', () => {
  it.concurrent('test getWhatsappContent', () => {
    expect(getWhatsappContent(whatsappPreviewTemplateData)).toMatchSnapshot();
    expect(getWhatsappContent(whatsappPreviewTemplateData2)).toMatchSnapshot();
    expect(getWhatsappContent(whatsappPreviewTemplateDataVid)).toMatchSnapshot();
    expect(getWhatsappContent(whatsappPreviewTemplateDataDoc)).toMatchSnapshot();
    expect(getWhatsappContent({})).toMatchSnapshot();
    expect(getWhatsappContent(whatsappPreviewTemplateData3)).toEqual(whatsappPreviewTemplateData3Result);
    expect(bytesConversion(1024)).toEqual('1.0 KB');
    expect(bytesConversion('abcd')).toEqual('0 bytes');
  });

  it.concurrent("test getWhatsappQuickReply", () => {
    expect(getWhatsappQuickReply(whatsappPreviewTemplateData3)).toEqual(
      whatsappPreviewTemplateData3QuickReplyResult
    );
  });

  it.concurrent('test getWhatsappContent for preview', () => {
    expect(
      getWhatsappContent(whatsappPreviewTemplateData, true),
    ).toMatchSnapshot();
  });

  it.concurrent('test getWhatsappCta', () => {
    expect(getWhatsappCta(whatsappPreviewTemplateData)).toMatchSnapshot();
    expect(getWhatsappCta(whatsappPreviewTemplateData2)).toMatchSnapshot();
    expect(getWhatsappCta({})).toMatchSnapshot();
  });

  it.concurrent('test getWhatsappStatus', () => {
    expect(getWhatsappStatus('approved')).toEqual(getWhatsappStatusOutput1);
    expect(getWhatsappStatus('awaitingApproval')).toEqual(getWhatsappStatusOutput2);
    expect(getWhatsappStatus('pending')).toEqual(getWhatsappStatusOutput2);
  });

  it.concurrent('test getWhatsappCategory', () => {
    expect(getWhatsappCategory('ALERT_UPDATE', 'twiliowhatsapptrans')).toEqual(
      getWhatsappCategoryOutput1,
    );
    expect(getWhatsappCategory('UTILITY', 'karixwhatsappbulk')).toEqual(
      getWhatsappCategoryOutput2,
    );
    expect(getWhatsappCategory('UTILITY', HOST_ICS)).toEqual(
      getWhatsappCategoryOutput2,
    );
  });
  it.concurrent('test getWhatsappAutoFill', () => {
    expect(getWhatsappAutoFill({versions:{base:{content:{whatsapp:{category: 'AUTHENTICATION'}}}}})).toEqual(
      getWhatsappAutoFillData,
    );
  });

  it.concurrent('test getWhatsappDocPreview', () => {
    const whatsappDocParams = {
      whatsappDocName: "java_tutorial.pdf",
      whatsappDocSize: 1089150,
      whatsappDocPages: 2,
      whatsappDocImg: "https://play-lh.googleusercontent.com/BkRfMfIRPR9hUnmIYGDgHHKjow-g18-ouP6B2ko__VnyUHSi1spcc78UtZ4sVUtBH4g",
    };
    expect(JSON.stringify(getWhatsappDocPreview(whatsappDocParams))).toEqual(
      JSON.stringify(getWhatsappDocPreviewOutput),
    );
    //getWhatsappDocPreviewOutput is the output when page number is greater than 1
    //getWhatsappDocPreviewOutput1 is the output when page number is equal than 1
    expect(JSON.stringify(getWhatsappDocPreview({
      ...whatsappDocParams,
      whatsappDocPages: 1,
    }))).toStrictEqual(
      JSON.stringify(getWhatsappDocPreviewOutput1),
    );
  });

  describe('test getWhatsappCarouselButtonView', () => {
    it.concurrent('should return empty array for null/undefined/empty input', () => {
      expect(getWhatsappCarouselButtonView(null)).toEqual([]);
      expect(getWhatsappCarouselButtonView(undefined)).toEqual([]);
      expect(getWhatsappCarouselButtonView([])).toEqual([]);
    });

    it.concurrent('should render single button correctly', () => {
      const singleButton = [{
        text: 'Click Me',
        buttonType: 'URL'
      }];
      
      const result = getWhatsappCarouselButtonView(singleButton);
      expect(result).toHaveLength(2); // 1 divider + 1 button
      expect(result[0].props.className).toBe('whatsapp-list-view-divider'); // Divider
      expect(result[1].props.children[1]).toBe('Click Me'); // Button text
    });

    it.concurrent('should render multiple buttons with dividers', () => {
      const multipleButtons = [
        { text: 'Call Us', type: PHONE_NUMBER },
        { text: 'Reply', type: QUICK_REPLY },
        { text: 'Visit Website', buttonType: 'URL' }
      ];
      
      const result = getWhatsappCarouselButtonView(multipleButtons);
      expect(result).toHaveLength(6); // Initial divider + 3 buttons + 2 intermediate dividers
    });

    it.concurrent('should handle template preview mode correctly', () => {
      const buttons = [{
        text: 'Click Me',
        buttonType: 'URL'
      }];
      
      const result = getWhatsappCarouselButtonView(buttons, true);
      expect(result[0].props.className).toBe('whatsapp-divider'); // Check preview mode divider class
    });

    it.concurrent('should handle buttons without text properly', () => {
      const buttons = [
        { buttonType: 'URL' },
        { text: 'Valid Button', buttonType: 'URL' }
      ];
      
      const result = getWhatsappCarouselButtonView(buttons);
      expect(result).toHaveLength(3); // Initial divider + 1 valid button + 1 divider
    });

    it.concurrent('should use correct icons based on button type', () => {
      const buttons = [
        { text: 'Call Now', type: PHONE_NUMBER },
        { text: 'Quick Reply', type: QUICK_REPLY },
        { text: 'Visit Site', buttonType: 'URL' }
      ];
      
      const result = getWhatsappCarouselButtonView(buttons);
      
      // Check icon types
      expect(result[1].props.children[0].props.type).toBe('call'); // Phone number icon
      expect(result[3].props.children[0].props.type).toBe('small-link'); // Quick reply icon
      expect(result[5].props.children[0].props.type).toBe('launch'); // URL icon
    });

    it.concurrent('should handle both type and buttonType properties', () => {
      const buttons = [
        { text: 'Button 1', type: PHONE_NUMBER },
        { text: 'Button 2', buttonType: PHONE_NUMBER }
      ];
      
      const result = getWhatsappCarouselButtonView(buttons);
      // Both buttons should have call icons
      expect(result[1].props.children[0].props.type).toBe('call');
      expect(result[3].props.children[0].props.type).toBe('call');
    });
  });

  describe('carousel functionality', () => {
    const mockTemplate = {
      versions: {
        base: {
          content: {
            whatsapp: {
              languages: [{ content: 'Test message' }],
              carouselData: [
                {
                  mediaType: 'image',
                  imageUrl: 'https://example.com/image.jpg',
                },
                {
                  mediaType: 'video',
                  videoUrl: 'https://example.com/video.mp4',
                  videoPreviewImg: 'https://example.com/preview.jpg',
                },
              ],
            },
          },
        },
      },
    };

    it.concurrent('should return original carousel data when isPreview is false', () => {
      const result = getWhatsappContent(mockTemplate, false);
      expect(result.carouselData).toEqual([
        {
          mediaType: 'image',
          imageUrl: 'https://example.com/image.jpg',
        },
        {
          mediaType: 'video',
          videoUrl: 'https://example.com/video.mp4',
          videoPreviewImg: 'https://example.com/preview.jpg',
        },
      ]);
      expect(result.carouselMediaType).toBeUndefined();
    });

    it.concurrent('should transform carousel data correctly when isPreview is true', () => {
      const result = getWhatsappContent(mockTemplate, true);

      expect(result.carouselData).toEqual([
        {
          mediaType: 'image',
          imageUrl: 'https://example.com/image.jpg',
          imageSrc: 'https://example.com/image.jpg',
        },
        {
          mediaType: 'video',
          videoUrl: 'https://example.com/video.mp4',
          videoPreviewImg: 'https://example.com/preview.jpg',
          videoSrc: 'https://example.com/video.mp4',
        },
      ]);
      expect(result.carouselMediaType).toBe('image');
    });

    it.concurrent('should handle empty carousel data', () => {
      const emptyTemplate = {
        versions: {
          base: {
            content: {
              whatsapp: {
                languages: [{ content: 'Test message' }],
                carouselData: [],
              },
            },
          },
        },
      };

      const result = getWhatsappContent(emptyTemplate, true);
      expect(result.carouselData).toEqual([]);
      expect(result.carouselMediaType).toBeUndefined();
    });

    it.concurrent('should handle undefined carousel data', () => {
      const undefinedTemplate = {
        versions: {
          base: {
            content: {
              whatsapp: {
                languages: [{ content: 'Test message' }],
              },
            },
          },
        },
      };

      const result = getWhatsappContent(undefinedTemplate, true);
      expect(result.carouselData).toEqual([]);
      expect(result.carouselMediaType).toBeUndefined();
    });
  });
});
