import { AbstractControl, ValidatorFn } from '@angular/forms';
import { SlutredovisningFormStep0StillUnemployedFormData } from './slutredovisning-form-step0-still-unemployed.component';
import { StillUnemployedReason } from '@msfa-models/slutredovisning.model';

export interface SlutredovisningStillUnemployedFormErrors {
  needDescriptionOfOther?: string;
  atLeastOneSelected?: string;
}

export class SlutredovisningFormStep0StillUnemployed {
  static slutredovisningFormStep0StillUnemployedIsValid(): ValidatorFn {
    return (c: AbstractControl): SlutredovisningStillUnemployedFormErrors => {
      let errors: SlutredovisningStillUnemployedFormErrors = {};

      const {
        reasonsGoalNotReached,
        stillUnemployedExplanation,
      } = c.value as SlutredovisningFormStep0StillUnemployedFormData;

      if (Object.values(reasonsGoalNotReached).every(reasonIsChecked => !reasonIsChecked)) {
        errors = {
          ...errors,
          atLeastOneSelected: 'Minst en anledning måste väljas.',
        };
      }

      // TODO: Is it required that at least one reason is chosen? Check with Bea and Kia
      if (
        reasonsGoalNotReached[StillUnemployedReason.Annat] === true &&
        (!stillUnemployedExplanation || stillUnemployedExplanation?.length <= 0)
      ) {
        errors = {
          ...errors,
          needDescriptionOfOther: 'Ifall Annat är valt som utbildningsnivå krävs en beskrivning.',
        };
      }

      return errors;
    };
  }
}
