/*
 * Copyright (C) 2023 Robert Bosch GmbH Copyright (C) 2023 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Duration, DurationType, MaxNumberOfDurations } from "./bci-duration-picker.model";
import {
  autoFillDuration, fitDurationToMaxValues,
  matchDurationWithDurationType,
  parseDurationObjectToString, parseDurationStringToObject
} from "./bci-duration-picker.helper";

describe('BciDurationPickerHelper', () => {
  const testDurationType = DurationType['HH:MM:SS'];
  const stringDuration = '123:12:12';
  const wrongStringDuration = '123:12';
  const objectDuration: Duration = {
    hours: 456,
    minutes: 45,
    seconds: 45
  };

  it('should match duration with durationType', (): void => {
    const wrongObjectDuration: Duration = {
      hours: 456,
      minutes: 45
    };

    expect(matchDurationWithDurationType(stringDuration, testDurationType)).toBeTruthy();
    expect(matchDurationWithDurationType(objectDuration, testDurationType)).toBeTruthy();
    expect(matchDurationWithDurationType(wrongStringDuration, testDurationType)).toBeFalsy();
    expect(matchDurationWithDurationType(wrongObjectDuration, testDurationType)).toBeFalsy();
  });

  it('should autofill the duration', (): void => {
    const firstValue = 123;

    expect(autoFillDuration(firstValue, testDurationType)).toMatchObject({
      hours: 123,
      minutes: 0,
      seconds: 0
    });
  });

  it('should parse string to object', (): void => {
    expect(parseDurationStringToObject(wrongStringDuration, testDurationType)).toBeNull();
    expect(parseDurationStringToObject(stringDuration, testDurationType)).toMatchObject({
      hours: 123,
      minutes: 12,
      seconds: 12
    });
  });

  it('should parse object to string', (): void => {
    expect(parseDurationObjectToString(objectDuration)).toBe('456:45:45');
  });

  it('should fit duration to max number accepted', (): void => {
    const outOfRangeStringDuration = '123:123:123';
    const maxNbOfDurations: MaxNumberOfDurations = {
      maxDays: undefined,
      maxHours: undefined,
      maxMinutes: 59,
      maxSeconds: 59
    };

    expect(fitDurationToMaxValues(outOfRangeStringDuration, maxNbOfDurations, testDurationType)).toBe('123:59:59');
  });
});
