import React from 'react';
import { Provider } from 'react-redux';
import { configureStore }  from '@capillarytech/vulcan-react-sdk/utils';
import { initialReducer } from '../../../../../initialReducer';
import { mountWithIntl } from '../../../../../helpers/intl-enzym-test-helpers';
import { LineWrapper } from '../index';
import { mockData } from './mockData';
import {isValidText, isValidWhatsappCtaText} from '../utils';
import history from '../../../../../utils/history';

let store;
beforeAll(() => {
  store = configureStore({}, initialReducer, history);
});

jest.mock('../../ImageCarousel/Content', () => ({
  __esModule: true,
  default: (props) => (
    <div className="LineImageCarouselContent-mock" {...props}>
      LineImageCarouselContent
    </div>
  ),
}));

jest.mock('../../../../TagList/index.js', () => ({
  __esModule: true,
  default: (props) => (
    <div className="tag-mock" {...props}>
      TagList
    </div>
  ),
}));

describe('line wrapper test/>', () => {
  let renderedComponent;
  const deleteWrapper = jest.fn();
  const shiftUp = jest.fn();
  const shiftDown = jest.fn();
  const formatMessage = jest.fn();
  const fetchSchemaForEntity = jest.fn();
  const renderHelper = (selectedType) => {
    renderedComponent = mountWithIntl(
      <Provider store={store}>
        <LineWrapper
          index={0}
          intl={{ formatMessage }}
          deleteWrapper={deleteWrapper}
          shiftUp={shiftUp}
          shiftDown={shiftDown}
          selectedType={selectedType}
          lineWrappers={1}
          Templates={mockData.Templates}
          content={mockData.content}
          location={mockData.location}
          isFullMode={true}
          globalActions={{ fetchSchemaForEntity }}
          Line={mockData.Line}
        />
      </Provider>,
    );
  };

  it('should render create mode', () => {
    renderHelper('text');
    expect(renderedComponent).toMatchSnapshot();
    renderHelper('template');
    expect(renderedComponent).toMatchSnapshot();
  });
  
  describe('isValidText', () => {
    it('should return true for valid text', () => {
      const validText = 'This is a valid text 123';
      expect(isValidText(validText)).toBe(true);
    });
  
    it('should return false for text with special characters', () => {
      const textWithSpecialChars = 'Invalid text @#$';
      expect(isValidText(textWithSpecialChars)).toBe(false);
    });
  
    it('should return false for text with emojis', () => {
      const textWithEmojis = 'Invalid text 😊';
      expect(isValidText(textWithEmojis)).toBe(false);
    });
    it('should return false for text with emojis,newlines,special char', () => {
      const textWithEmojis = 'Invalid text 😊@#$ \n';
      expect(isValidText(textWithEmojis)).toBe(false);
    });
  
    it('should return true for an empty string', () => {
      const emptyString = '';
      expect(isValidText(emptyString)).toBe(true);
    });
  });

  describe('isValidWhatsappCtaText', () => {
    it('should return false for non-string input', () => {
      expect(isValidWhatsappCtaText(123)).toBe(false);
      expect(isValidWhatsappCtaText(null)).toBe(false);
      expect(isValidWhatsappCtaText(undefined)).toBe(false);
      expect(isValidWhatsappCtaText({})).toBe(false);
      expect(isValidWhatsappCtaText([])).toBe(false);
      expect(isValidWhatsappCtaText(true)).toBe(false);
    });

    it('should return true for multilingual and symbol text', () => {
      const valid = '確認一下 Привет مرحبا मान्य 123 @#&!?.:, -+/\\\'"=&₹€£$^(){}';
      expect(isValidWhatsappCtaText(valid)).toBe(true);
    });
  
    it('should return true for full allowed punctuation set', () => {
      const allowed = 'A1 z9 @#&!?.:, -+/\\\'"=&₹€£$^(){}';
      expect(isValidWhatsappCtaText(allowed)).toBe(true);
    });
  
    it('should return false for whitespace-only', () => {
      expect(isValidWhatsappCtaText('   ')).toBe(false);
      expect(isValidWhatsappCtaText('\t\t')).toBe(false);
    });
  
    it('should return true for leading and trailing spaces with valid text', () => {
      expect(isValidWhatsappCtaText('  Hi')).toBe(true);
      expect(isValidWhatsappCtaText('Hi  ')).toBe(true);
    });
  
    it('should return false for control or markdown chars', () => {
      expect(isValidWhatsappCtaText('Hello\nWorld')).toBe(false);
      expect(isValidWhatsappCtaText('Hello\tWorld')).toBe(false);
      expect(isValidWhatsappCtaText('*bold*')).toBe(false);
      expect(isValidWhatsappCtaText('_under_')).toBe(false);
      expect(isValidWhatsappCtaText('~strike~')).toBe(false);
      expect(isValidWhatsappCtaText('`code`')).toBe(false);
    });
  
    it('should return false for emojis, flags, ZWJ sequences and VS16', () => {
      expect(isValidWhatsappCtaText('Heart ❤️')).toBe(false);
      expect(isValidWhatsappCtaText('Flag 🇮🇳')).toBe(false);
      expect(isValidWhatsappCtaText('Family 👨‍👩‍👧‍👦')).toBe(false);
      expect(isValidWhatsappCtaText('Sun ☀️')).toBe(false);
    });
  
    it('should return false for mixed valid text with emoji inside', () => {
      expect(isValidWhatsappCtaText('Hello @# hi ❤️')).toBe(false);
    });
  
    it('should return true for backslash usage', () => {
      expect(isValidWhatsappCtaText('C:\\Path')).toBe(true);
    });
  });
  
});
