import { parseOsmOpeningHours } from '../../API/opening-hours-osm.js';

describe('parseOsmOpeningHours', () => {
  test('returns null for empty / non-string input', () => {
    expect(parseOsmOpeningHours('')).toBeNull();
    expect(parseOsmOpeningHours(null)).toBeNull();
    expect(parseOsmOpeningHours(undefined)).toBeNull();
    expect(parseOsmOpeningHours(123)).toBeNull();
  });

  test('24/7 → every day 00:00–24:00', () => {
    expect(parseOsmOpeningHours('24/7')).toEqual({
      mon: ['00:00', '24:00'],
      tue: ['00:00', '24:00'],
      wed: ['00:00', '24:00'],
      thu: ['00:00', '24:00'],
      fri: ['00:00', '24:00'],
      sat: ['00:00', '24:00'],
      sun: ['00:00', '24:00'],
    });
  });

  test('Mo-Fr 09:00-21:00 → weekdays set, weekend null', () => {
    const result = parseOsmOpeningHours('Mo-Fr 09:00-21:00');
    expect(result.mon).toEqual(['09:00', '21:00']);
    expect(result.fri).toEqual(['09:00', '21:00']);
    expect(result.sat).toBeNull();
    expect(result.sun).toBeNull();
  });

  test('Multiple rules separated by semicolons', () => {
    const result = parseOsmOpeningHours('Mo-Fr 09:00-21:00; Sa 09:00-14:00; Su closed');
    expect(result.mon).toEqual(['09:00', '21:00']);
    expect(result.fri).toEqual(['09:00', '21:00']);
    expect(result.sat).toEqual(['09:00', '14:00']);
    expect(result.sun).toBeNull();
  });

  test('Comma-separated day list', () => {
    const result = parseOsmOpeningHours('Mo,We,Fr 10:00-14:00');
    expect(result.mon).toEqual(['10:00', '14:00']);
    expect(result.tue).toBeNull();
    expect(result.wed).toEqual(['10:00', '14:00']);
    expect(result.thu).toBeNull();
    expect(result.fri).toEqual(['10:00', '14:00']);
  });

  test('Split shifts collapsed to first-open / last-close', () => {
    const result = parseOsmOpeningHours('Mo-Fr 09:00-13:30,16:30-20:00');
    expect(result.mon).toEqual(['09:00', '20:00']);
    expect(result.fri).toEqual(['09:00', '20:00']);
  });

  test('Wrap-around day range Sa-Mo', () => {
    const result = parseOsmOpeningHours('Sa-Mo 10:00-18:00');
    expect(result.sat).toEqual(['10:00', '18:00']);
    expect(result.sun).toEqual(['10:00', '18:00']);
    expect(result.mon).toEqual(['10:00', '18:00']);
    expect(result.tue).toBeNull();
  });

  test('Public-holiday rules are ignored', () => {
    const result = parseOsmOpeningHours('Mo-Fr 09:00-21:00; PH off');
    expect(result.mon).toEqual(['09:00', '21:00']);
  });

  test('Parenthetical comments are stripped', () => {
    const result = parseOsmOpeningHours('Mo-Fr 09:00-14:00 (verano)');
    expect(result.mon).toEqual(['09:00', '14:00']);
  });

  test('"off" applies null to those days', () => {
    const result = parseOsmOpeningHours('Mo-Fr 09:00-21:00; Sa off');
    expect(result.sat).toBeNull();
    expect(result.mon).toEqual(['09:00', '21:00']);
  });

  test('Returns null when nothing parses', () => {
    expect(parseOsmOpeningHours('see website')).toBeNull();
    expect(parseOsmOpeningHours('?')).toBeNull();
  });

  test('Single-digit hours get zero-padded', () => {
    const result = parseOsmOpeningHours('Mo 9:00-18:00');
    expect(result.mon).toEqual(['09:00', '18:00']);
  });
});
