import * as Getters from './getters';
import pagesMappedById from '../../tests/factories/pagesMappedById';

describe('Getters Helper', () => {
  const { location } = window;

  beforeAll(() => {
    delete window.location;
    window.location = { search: '' };
    jest.resetModules(); // this is important - it clears the cache
  });

  afterAll(() => {
    window.location = location;
  });

  test('getURLParam()', () => {
    window.location.search = 's=something';
    expect(Getters.getURLParam('s')).toEqual('something');
  });

  test('getPageTitle()', () => {
    window.location.search = 's=hello';
    expect(Getters.getPageTitle({ path: 's' })).toEqual('You searched for hello - irishluck.ie');
  });

  test('getPageTitle() non search page', () => {
    window.location.search = 's=hello';
    expect(Getters.getPageTitle({ meta_title: 'test meta title', path: '/' })).toEqual(
      'test meta title'
    );
  });

  test('getExtraField()', () => {
    expect(Getters.getExtraField({}, 'k')).toEqual(null);
  });

  test('getExtraField() non existent', () => {
    expect(Getters.getExtraField({}, 'k', 'def')).toEqual('def');
    expect(Getters.getExtraField({ name: 'foo' }, 'name', 'def')).toEqual('foo');
  });

  test('getModuleTitle()', () => {
    expect(
      Getters.getModuleTitle(
        { module_title: 'Test [operator_name] test' },
        { relation_type: 'operator', relation: { name: 'test name' } }
      )
    ).toEqual('Test test name test');
  });

  test('getSection()', () => {
    expect(Getters.getSection('short_code', { page: {}, marketSections: {} })).toBeNull();
    expect(
      Getters.getSection('short', {
        page: { sections: { short: 'bar' } },
        marketSections: { short: 'foo' },
      })
    ).toEqual('foo');
    expect(
      Getters.getSection('short', {
        page: { sections: { short: 'bar' } },
        marketSections: {},
      })
    ).toEqual('bar');
  });

  test('textWordsLimit()', () => {
    const text = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`;
    expect(Getters.textWordsLimit(text, 5)).toEqual('Lorem Ipsum is simply dummy...');
    expect(Getters.textWordsLimit(text, 2)).toEqual('Lorem Ipsum...');
    expect(Getters.textWordsLimit(1, 2)).toEqual(null);
  });

  test('filterImgSrc()', () => {
    expect(Getters.filterImgSrc('/wp-content/photo.jpg')).toEqual(
      'https://www.norskespilleautomater.com/wp-content/photo.jpg'
    );
    expect(Getters.filterImgSrc('/images/photo.jpg')).toEqual('/images/photo.jpg');
  });

  test('imagePrettyUrl()', () => {
    expect(
      Getters.imagePrettyUrl('photo.jpg', 350, 600).includes('https://cdn.images.com')
    ).toBeTruthy();
    expect(Getters.imagePrettyUrl('photo.jpg', 350, 600)).toEqual(
      'https://cdn.images.com/fit-in/350x600/photo.jpg'
    );
  });

  test('getImageExtension()', () => {
    expect(Getters.getImageExtension('photo.jpg')).toBeTruthy();
    expect(Getters.getImageExtension('photo.jpg')).toEqual('jpg');
    expect(Getters.getImageExtension(null)).toEqual(null);
  });

  test('getPageImage()', () => {
    expect(
      Getters.getPageImage({ relation_type: 'operator', relation: { default_logo_url: 'foo.jpg' } })
    ).toEqual(Getters.imagePrettyUrl('foo.jpg'));
    expect(
      Getters.getPageImage({ relation_type: 'operator', relation: { logo_url: 'bar.png' } })
    ).toEqual(Getters.imagePrettyUrl('bar.png'));
    expect(
      Getters.getPageImage({ relation_type: 'game', relation: { thumbnail_filename: 'foo.jpg' } })
    ).toEqual('foo.jpg');
    expect(Getters.getPageImage({ relation_type: 'game', featured_image: 'bar.gif' })).toEqual(
      Getters.imagePrettyUrl('bar.gif')
    );
    expect(Getters.getPageImage({ relation_type: 'article', banner: 'bar.svg' })).toEqual(
      Getters.imagePrettyUrl('bar.svg')
    );
    expect(
      Getters.getPageImage({ relation_type: 'software_provider', relation: { logo: 'logo.jpg' } })
    ).toEqual(Getters.imagePrettyUrl('logo.jpg'));
    expect(Getters.getPageImage({ banner: 'bar.svg' })).toEqual(Getters.imagePrettyUrl('bar.svg'));
  });

  test('prettyTracker()', () => {
    expect(Getters.prettyTracker({ short_name: 'short', relation_type: 'game' })).toEqual(
      '/no/visit/short'
    );
    expect(
      Getters.prettyTracker(
        {
          type: 'casino',
          relation_type: 'casino',
          short_name: 'short',
          links: { 'page-template-test': 'granular' },
        },
        'non_main',
        false,
        'page-template-test'
      )
    ).toEqual('/no/visit/short/page-template-test');
  });

  test('getBonus()', () => {
    expect(Getters.getBonus('name', '')).toBeNull();
    expect(Getters.getBonus('foo', { one_liners: { foo: { one_liner: 'bar' } } })).toEqual({
      one_liner: 'bar',
    });
    expect(
      Getters.getBonus('foo', { one_liners: {}, bonus: { one_liners: { foo: 'bar' } } })
    ).toEqual('bar');
    expect(Getters.getBonus('foo', { one_liners: {}, bonus: { one_liners: '' } })).toBeNull();
  });

  test('getGameExtraField()', () => {
    expect(Getters.getGameExtraField({}, 'key')).toBeNull();
    expect(
      Getters.getGameExtraField(
        { affiliate_games: { no_no: { extra_fields: { foo: 'xyz' } } } },
        'foo'
      )
    ).toEqual('xyz');
  });

  test('getSectionExtraField()', () => {
    expect(Getters.getSectionExtraField({}, '', '')).toBeNull();
    expect(Getters.getSectionExtraField('', { extra_fields: { bar: 'foo' } }, 'bar')).toEqual(
      'foo'
    );
    expect(
      Getters.getSectionExtraField(
        { sections: { foo: { extra_fields: { bar: 'name' } } } },
        'foo',
        'bar'
      )
    ).toEqual('name');
  });

  test('getGameRating()', () => {
    // expect(
    //   Getters.getGameRating({
    //     markets: {
    //       a: { first_rating: 1, second_rating: 2, third_rating: 3, fourth_rating: 4 },
    //       b: {},
    //       c: {},
    //     },
    //   })
    // ).toEqual(2.5);
    expect(
      Getters.getGameRating({
        first_rating: 6,
        second_rating: 2,
        third_rating: 3,
        fourth_rating: 5,
      })
    ).toEqual(4);
  });

  test('getGameIframe()', () => {
    expect(Getters.getGameIframe('')).toBeNull();
    expect(Getters.getGameIframe({ devices: { en: { desktop: { url: 'foo' } } } }, 'en')).toEqual(
      'foo'
    );
    expect(Getters.getGameIframe({ devices: { fr: { all: { url: 'bar' } } } }, 'fr')).toEqual(
      'bar'
    );
    expect(Getters.getGameIframe({ devices: { en: { desktop: { url: 'foo' } } } }, null)).toEqual(
      'foo'
    );
    expect(Getters.getGameIframe({ devices: { en: { all: { url: 'bar' } } } })).toEqual('bar');
    expect(Getters.getGameIframe({ iframe: 'iframe' })).toEqual('iframe');
    expect(Getters.getGameIframe({ key: 'value' })).toBeNull();
  });

  test('isTandcEnabled()', () => {
    expect(Getters.isTandcEnabled({})).toBeFalsy();
    expect(
      Getters.isTandcEnabled({
        terms_and_conditions_text_enabled: true,
        terms_and_conditions: true,
        terms_and_conditions_link_enabled: true,
        terms_and_condition_link_text: true,
      })
    ).toBeTruthy();
    expect(
      Getters.isTandcEnabled({
        terms_and_conditions: true,
        terms_and_conditions_link_enabled: true,
        terms_and_condition_link_text: true,
      })
    ).toBeFalsy();
    expect(
      Getters.isTandcEnabled({
        terms_and_conditions_text_enabled: true,
        terms_and_conditions: true,
      })
    ).toBeTruthy();
  });

  test('getUrl()', () => {
    expect(Getters.getUrl('/')).toEqual('http://urltest.com/');
    expect(Getters.getUrl('/games')).toEqual('http://urltest.com/games');
    expect(Getters.getUrl('games')).toEqual('http://urltest.com/games');
  });

  test('getLanguage()', () => {
    expect(Getters.getLanguage('str')).toEqual('str');
    expect(Getters.getLanguage()).toEqual('en');
    expect(Getters.getLanguage('no')).toEqual('nb-NO');
  });

  test('copyrightText()', () => {
    const copyRight = Getters.copyrightText();
    const today = new Date();
    const year = today.getFullYear();
    expect(typeof copyRight).toEqual('string');
    expect(copyRight).toContain(year.toString());
  });

  test('getFirstModuleByName()', () => {
    const object = {
      modules: [{ name: 'menu', children: 'hello' }],
    };
    const module = Getters.getFirstModuleByName(object, 'menu');
    expect(typeof module).toEqual('object');
    expect(module).toEqual({ name: 'menu', children: 'hello' });
  });

  test('translate()', () => {
    const object = { hello: 'world' };
    expect(Getters.translate(object, 'hello')).toEqual('world');
    expect(Getters.translate(object, 'foo', 'bar')).toEqual('bar');
  });

  test('getHomeBreadcrumbs() with categories', () => {
    const page = {
      categories: [{ short_name: 'category1' }],
      template: 'template1',
      type: 'type1',
    };
    const translations = {
      home_template1_category1: 'Template 1 Category 1 Home',
      Home: 'Home',
    };
    expect(Getters.getHomeBreadcrumbs(page, translations)).toEqual('Template 1 Category 1 Home');
  });

  test('getHomeBreadcrumbs() with categories but with no category translations', () => {
    const page = {
      categories: [{ short_name: 'category2' }, { short_name: 'category1' }],
      template: 'template1',
      type: 'type1',
    };
    const translations = {
      home_type1_category1: 'Template 1 Category 1 Home',
      home_type1: 'Template 1',
      Home: 'Home',
    };
    expect(Getters.getHomeBreadcrumbs(page, translations)).toEqual('Template 1');
  });

  test('getHomeBreadcrumbs() without categories', () => {
    const page = {
      categories: [],
      type: 'type1',
    };
    const translations = {
      home_type1: 'Type 1 Home',
      Home: 'Home',
    };
    expect(Getters.getHomeBreadcrumbs(page, translations)).toEqual('Type 1 Home');
  });

  test('getHomeBreadcrumbs() with missing translation', () => {
    const page = {
      categories: [],
      type: 'type2',
    };
    const translations = {
      Home: 'Home',
    };
    expect(Getters.getHomeBreadcrumbs(page, translations)).toEqual('Home');
  });

  const module = {
    items: [
      {
        id: 12300,
      },
      {
        id: 13212,
      },
    ],
  };

  test('add the operator item', () => {
    Getters.shiftFirstOperator(11607, module, pagesMappedById);
    expect(module.items[0].id).toBe(11607);
  });

  test('move opeartor first postion', () => {
    Getters.shiftFirstOperator(13212, module, pagesMappedById);
    expect(module.items[0].id).toBe(13212);
  });

  test('check when we have the operator in first postion', () => {
    Getters.shiftFirstOperator(13212, module, pagesMappedById);
    expect(module.items[0].id).toBe(13212);
  });

  test('If the reading time is bigger than half, it should return the next number ', () => {
    expect(Getters.getRoundMinutes('3:52')).toBe(4);
  });

  test('If reading time is less than half, it should return the last number ', () => {
    expect(Getters.getRoundMinutes('3:29')).toBe(3);
  });

  test('Test altText', () => {
    const alt = Getters.getAltText({ alt: 'imageAlt' }, 'default');
    expect(alt).toEqual('imageAlt');
  });

  test('Test without altText', () => {
    const altText = Getters.getAltText(null, 'default');
    expect(altText).toEqual('default');
  });

  const array = ['apple', 'banana', 'cherry'];
  const keysArray = ['fruit_apple', 'fruit_banana', 'fruit_cherry'];
  const translations = {
    fruit_apple: 'manzana',
    fruit_banana: 'plátano',
    fruit_cherry: 'cereza',
  };

  test('updateArrayTranslations with the data', () => {
    const result = Getters.updateArrayTranslations(array, keysArray, translations);

    expect(result).toEqual(['manzana', 'plátano', 'cereza']);
  });

  test('updateArrayTranslations with array missing', () => {
    const result = Getters.updateArrayTranslations(undefined, keysArray, translations);
    expect(result).toEqual(undefined);
  });

  test('updateArrayTranslations with arrayKeys missing', () => {
    const result = Getters.updateArrayTranslations(array, undefined, translations);
    expect(result).toEqual(undefined);
  });

  test('getObjectvalue recursive function to get value', () => {
    const getObjValue = Getters.getObjectValue({ name: 'test' }, 'name');
    expect(getObjValue).toEqual('test');
  });

  test('getObjectvalue recursive function to get 2 times nested value', () => {
    const getObjValue = Getters.getObjectValue({ test: { test2: 'test3' } }, 'test.test2');
    expect(getObjValue).toEqual('test3');
  });

  test('getObjectvalue recursive function no path it will return the object', () => {
    const getObjValue = Getters.getObjectValue({ name: 'test' });
    expect(getObjValue).toEqual({ name: 'test' });
  });

  // Bonus box
  const bonusData = {
    status: 'active',
    one_liners: {
      main: {
        one_liner: '100% Bonus',
      },
    },
  };
  test('returns null bonus', () => {
    const result = Getters.getBonusData({});
    expect(result).toBeNull();
  });

  test('data with mainLine', () => {
    const result = Getters.getBonusData(bonusData, 'main');
    expect(result).toEqual({
      isInoperative: false,
      mainLine: '100% Bonus',
      secondLine: null,
    });
  });

  test('data with mainLine and secondLine', () => {
    bonusData.one_liners.main.one_liner = '100% Bonus+50 free spins';
    const result = Getters.getBonusData(bonusData, 'main');
    expect(result).toEqual({
      isInoperative: false,
      mainLine: '100% Bonus',
      secondLine: '50 free spins',
    });
  });
});
