import { generateTrackerLink, generatePlaceholderString, generateArray } from './generators.mjs';
import { months } from '~constants/common';

describe('Generate Tracker Link Helper', () => {
  test('generateTrackerLink main', () => {
    const trackerLink = generateTrackerLink({ type: 'casino', short_name: 'bet365' }, 'main');
    expect(trackerLink).toBeTruthy();
    expect(typeof trackerLink).toEqual('string');
    expect(trackerLink).toEqual('/no/visit/bet365');
  });
  test('generateTrackerLink non main', () => {
    const trackerLink = generateTrackerLink({ type: 'casino', short_name: 'bet365' }, 'non_main');
    expect(trackerLink).toBeTruthy();
    expect(typeof trackerLink).toEqual('string');
    expect(trackerLink).toEqual('/no/visit/bet365/non_main');
  });
  test('generateTrackerLink no tracker format', () => {
    const trackerLink = generateTrackerLink({ type: 'casino', short_name: 'bet365' }, 'main');
    expect(trackerLink).toEqual('/no/visit/bet365');
  });
});

describe('Generate Placeholders String', () => {
  test('generatePlaceholderString uppercase', () => {
    const string = generatePlaceholderString('Some title [MONTH] [YEAR]');
    const date = new Date();
    expect(string).not.toContain('[MONTH]');
    expect(string).not.toContain('[YEAR]');
    expect(string).toContain(date.getFullYear().toString());
    expect(string).toContain(months[date.getMonth()]);
  });
  test('generatePlaceholderString lowercase', () => {
    const string = generatePlaceholderString(
      'Some title [currentdate] [currentmonth] [currentyear] [sitename] [title]',
      null,
      {
        siteName: 'Site Name',
        pageTitle: 'Site Title',
      }
    );
    const date = new Date();
    expect(string).not.toContain('[currentdate]');
    expect(string).not.toContain('[currentmonth]');
    expect(string).not.toContain('[currentyear]');
    expect(string).not.toContain('[sitename]');
    expect(string).not.toContain('[title]');
    expect(string).toContain(date.getDate().toString());
    expect(string).toContain(months[date.getMonth()]);
    expect(string).toContain(date.getFullYear().toString());
    expect(string).toContain('Site Name');
    expect(string).toContain('Site Title');
  });
  test('generatePlaceholderString translated', () => {
    const translateMonths = months.reduce(
      (translated, month) => ({ ...translated, [month]: `translate${month.substring(0, 1)}` }),
      {}
    );
    const string = generatePlaceholderString('Some title [MONTH]', translateMonths);
    const date = new Date();

    expect(string).not.toContain('[MONTH]');
    expect(string).toContain(translateMonths[months[date.getMonth()]]);
  });
  test('generatePlaceholderString empty', () => {
    const string = generatePlaceholderString();
    expect(string).not.toBeTruthy();
  });
});

describe('Generate Array From Object', () => {
  test('generateArray empty', () => {
    const output = generateArray();
    expect(Object.prototype.toString.call(output)).toEqual('[object Array]');
    expect(output.length).toEqual(0);
  });
  test('generateArray array', () => {
    const output = generateArray(['a', 'b']);
    expect(Object.prototype.toString.call(output)).toEqual('[object Array]');
    expect(output.length).toEqual(2);
    expect(output[0]).toEqual('a');
    expect(output[1]).toEqual('b');
  });
  test('generateArray object', () => {
    const output = generateArray({ a: 'a', b: 'b' });
    expect(Object.prototype.toString.call(output)).toEqual('[object Array]');
    expect(output.length).toEqual(2);
    expect(output[0]).toEqual('a');
    expect(output[1]).toEqual('b');
  });
});
