/**
 * Test suite for common utility functions
 */
import {
  getUserNameById,
  getMergedUserList,
  removeLinksFromHtmlContent,
  iframePreviewAdjustWidth,
} from '../common';

// Mock the Auth module
jest.mock('@capillarytech/cap-ui-utils', () => ({
  Auth: {
    hasFeatureAccess: jest.fn(),
  },
}));

describe('common Utility Functions', () => {
  describe('getUserNameById', () => {
    it('should return full name when user found', () => {
      const userList = [
        { userId: '1', firstName: 'John', lastName: 'Doe' },
        { userId: '2', firstName: 'Jane', lastName: 'Smith' },
      ];

      const result = getUserNameById('1', userList);

      expect(result).toBe('John Doe');
    });

    it('should return empty string when user not found', () => {
      const userList = [
        { userId: '1', firstName: 'John', lastName: 'Doe' },
      ];

      const result = getUserNameById('999', userList);

      expect(result).toBe('');
    });

    it('should handle empty user list', () => {
      const result = getUserNameById('1', []);

      expect(result).toBe('');
    });

    it('should handle multiple users with same ID (returns last match)', () => {
      const userList = [
        { userId: '1', firstName: 'John', lastName: 'Doe' },
        { userId: '1', firstName: 'Jane', lastName: 'Smith' },
      ];

      const result = getUserNameById('1', userList);

      // Should return the last match due to forEach iteration
      expect(result).toBe('Jane Smith');
    });

    it('should handle users with empty names', () => {
      const userList = [
        { userId: '1', firstName: '', lastName: '' },
      ];

      const result = getUserNameById('1', userList);

      expect(result).toBe(' ');
    });

    it('should handle users with only first name', () => {
      const userList = [
        { userId: '1', firstName: 'John', lastName: '' },
      ];

      const result = getUserNameById('1', userList);

      expect(result).toContain('John');
    });
  });

  describe('getMergedUserList', () => {
    it('should merge all user lists when isCapUser is true', () => {
      const userList = {
        capUsers: [
          { userId: '1', name: 'CAP User' },
        ],
        otherUsers: [
          { userId: '2', name: 'Other User' },
        ],
      };

      const result = getMergedUserList(userList, true);

      expect(result).toHaveLength(2);
      expect(result[0].name).toBe('CAP User');
      expect(result[1].name).toBe('Other User');
    });

    it('should exclude capUsers when isCapUser is false', () => {
      const userList = {
        capUsers: [
          { userId: '1', name: 'CAP User' },
        ],
        otherUsers: [
          { userId: '2', name: 'Other User' },
        ],
      };

      const result = getMergedUserList(userList, false);

      expect(result).toHaveLength(1);
      expect(result[0].name).toBe('Other User');
    });

    it('should handle empty user lists', () => {
      const userList = {};

      const result = getMergedUserList(userList, true);

      expect(result).toHaveLength(0);
    });

    it('should handle empty nested arrays', () => {
      const userList = {
        capUsers: [],
        otherUsers: [],
      };

      const result = getMergedUserList(userList, true);

      expect(result).toHaveLength(0);
    });

    it('should handle single user list', () => {
      const userList = {
        users: [
          { userId: '1', name: 'User 1' },
          { userId: '2', name: 'User 2' },
        ],
      };

      const result = getMergedUserList(userList, true);

      expect(result).toHaveLength(2);
    });

    it('should default isCapUser to true', () => {
      const userList = {
        capUsers: [{ userId: '1', name: 'CAP User' }],
        otherUsers: [{ userId: '2', name: 'Other User' }],
      };

      const result = getMergedUserList(userList);

      expect(result).toHaveLength(2);
    });
  });

  describe('removeLinksFromHtmlContent', () => {
    it('should remove href attributes from anchor tags', () => {
      const content = '<a href="https://example.com">Click here</a>';

      const result = removeLinksFromHtmlContent(content);

      expect(result).toContain('<a>');
      expect(result).not.toContain('href="https://example.com"');
    });

    it('should preserve anchor tag content', () => {
      const content = '<a href="https://example.com">Click here</a>';

      const result = removeLinksFromHtmlContent(content);

      expect(result).toContain('Click here');
    });

    it('should handle multiple links', () => {
      const content = '<a href="url1">Link 1</a><a href="url2">Link 2</a>';

      const result = removeLinksFromHtmlContent(content);

      expect(result).toContain('<a>');
      expect(result).toContain('Link 1');
      expect(result).toContain('Link 2');
    });

    it('should handle complex HTML with nested content', () => {
      const content = '<div><p><a href="url">Link</a></p></div>';

      const result = removeLinksFromHtmlContent(content);

      expect(result).toContain('<a>');
      expect(result).toContain('<p>');
      expect(result).toContain('Link');
    });

    it('should handle content without links', () => {
      const content = '<p>No links here</p>';

      const result = removeLinksFromHtmlContent(content);

      expect(result).toContain('No links here');
      expect(result).toContain('<p>');
    });

    it('should handle anchor tags with multiple attributes', () => {
      const content = '<a href="url" title="Title" class="link">Link</a>';

      const result = removeLinksFromHtmlContent(content);

      expect(result).toContain('<a>');
      expect(result).not.toContain('href="url"');
    });

    it('should handle empty anchor tags', () => {
      const content = '<a href="url"></a>';

      const result = removeLinksFromHtmlContent(content);

      expect(result).toContain('<a>');
    });
  });

  describe('iframePreviewAdjustWidth', () => {
    it('should add width attribute to img tags', () => {
      const content = '<img src="image.jpg" />';

      const result = iframePreviewAdjustWidth(content);

      expect(result).toContain('width="310"');
      expect(result).toContain('src="image.jpg"');
    });

    it('should set width to 310 pixels', () => {
      const content = '<img src="image.jpg" />';

      const result = iframePreviewAdjustWidth(content);

      expect(result).toContain('width="310"');
    });

    it('should handle multiple img tags', () => {
      const content = '<img src="image1.jpg" /><img src="image2.jpg" />';

      const result = iframePreviewAdjustWidth(content);

      const widthCount = (result.match(/width="310"/g) || []).length;
      expect(widthCount).toBe(2);
    });

    it('should preserve existing img attributes', () => {
      const content = '<img src="image.jpg" alt="Alt text" />';

      const result = iframePreviewAdjustWidth(content);

      expect(result).toContain('src="image.jpg"');
      expect(result).toContain('alt="Alt text"');
      expect(result).toContain('width="310"');
    });

    it('should handle img tags with other attributes', () => {
      const content = '<img src="image.jpg" height="200" class="responsive" />';

      const result = iframePreviewAdjustWidth(content);

      expect(result).toContain('width="310"');
      expect(result).toContain('height="200"');
      expect(result).toContain('class="responsive"');
    });

    it('should handle content without img tags', () => {
      const content = '<p>No images here</p>';

      const result = iframePreviewAdjustWidth(content);

      expect(result).toContain('<p>No images here</p>');
      expect(result).not.toContain('width="310"');
    });

    it('should handle complex HTML structure', () => {
      const content = '<div><img src="image.jpg" /><p>Text</p><img src="image2.jpg" /></div>';

      const result = iframePreviewAdjustWidth(content);

      const widthCount = (result.match(/width="310"/g) || []).length;
      expect(widthCount).toBe(2);
      expect(result).toContain('<p>Text</p>');
    });

    it('should handle self-closing and non-self-closing img tags', () => {
      const content1 = '<img src="image.jpg" />';
      const content2 = '<img src="image.jpg">';

      const result1 = iframePreviewAdjustWidth(content1);
      const result2 = iframePreviewAdjustWidth(content2);

      expect(result1).toContain('width="310"');
      expect(result2).toContain('width="310"');
    });

    it('should handle empty content', () => {
      const result = iframePreviewAdjustWidth('');
      // When empty string is parsed as HTML, the DOM parser adds html/head/body tags
      expect(result).toContain('<html>');
      expect(result).toContain('<body>');
    });
  });
});
