import { AbstractControl, ValidatorFn } from '@angular/forms';
import { SlutredovisningFormStep0EducationFormData } from './slutredovisning-form-step0-education.component';
import { EducationLevel } from '@msfa-models/slutredovisning.model';

export interface SlutredovisningFormErrors {
  needDescriptionOfOther?: string;
}

export class SlutredovisningFormStep0Education {
  static slutredovisningFormStep1EducationIsValid(): ValidatorFn {
    return (c: AbstractControl): SlutredovisningFormErrors => {
      let errors: SlutredovisningFormErrors = {};

      const { educationLevel, otherExplanation } = c.value as SlutredovisningFormStep0EducationFormData;

      if (educationLevel === EducationLevel.Annat && (!otherExplanation || otherExplanation?.length <= 0)) {
        errors = {
          ...errors,
          needDescriptionOfOther: 'Ifall Annat är valt som utbildningsnivå krävs en beskrivning.',
        };
      }

      return errors;
    };
  }
}
