import { DRIVERS_LICENSES } from '@msfa-constants/drivers-licenses';
import { DriversLicenseResponse } from './api/drivers-license.response.model';

export interface DriversLicense {
  licenses: string[];
  accessToCar: boolean;
}

function mapLicensesAsStringToArray(licenses = ''): string[] {
  const allLicenses = DRIVERS_LICENSES.sort((a, b) => b.length - a.length);
  let search = licenses;
  const found: string[] = [];

  allLicenses.forEach(license => {
    if (search.indexOf(license) >= 0) {
      found.push(license);
      search = search.replace(license, '');
    }
  });

  return found;
}

export function mapResponseToDriversLicense(data: DriversLicenseResponse): DriversLicense {
  return {
    licenses: mapLicensesAsStringToArray(data.korkort.behorighet),
    accessToCar: data.tillgang_till_bil,
  };
}
