import { LEA } from '.' ; const subcounties = { 'Dublin City Council' : [ 13260429 , 13260422 , 13260420 , 13260421 , 13260427 , 13260428 , 13260423 , 13260426 , 13260424 , 13260425 , 13260430 ] , 'Dún Laoghaire–Rathdown County Council' : [ 13260410 , 13260406 , 13260409 , 13260407 , 13260408 , 13260405 ] , 'Fingal County Council' : [ 13260417 , 13260402 , 13260403 , 13260404 , 13260418 , 13260400 , 13260401 ] , 'South Dublin County Council' : [ 13260416 , 13260414 , 13260411 , 13260419 , 13260413 , 13260412 , 13260415 ] } as Record < string , number [ ] > ; function getLEAs ( countyName , areas ) { const sortedAreas = ( areas . filter ( ( area ) => ! ! area ) as LEA [ ] ) . sort ( ( a , b ) => a . area < b . area ? - 1 : 1 ) ; return { name : countyName , areas : sortedAreas } ; } export function getSubCounties ( countyName , areas ) { let dataFitsSchema = true ; const assignedLeaIds = new Set ( ) ; const getAllLeas = ( ) => [ getLEAs ( countyName , areas ) ] ; if ( ! countyName . match ( /\bDublin\b/ ) ) { return getAllLeas ( ) ; } const leasBySubcounty = Object . entries ( subcounties ) . map ( ( [ subcountyName , leaIds ] ) => getLEAs ( subcountyName , leaIds . map ( ( leaId ) => { const leaArea = areas . find ( ( area ) => area . id === `${ leaId }` ) ; if ( leaArea ) { assignedLeaIds . add ( leaId ) ; } else { dataFitsSchema = false ; } return leaArea ; } ) ) ) ; if ( [ ... assignedLeaIds ] . length !== areas . length ) { dataFitsSchema = false ; } return dataFitsSchema ? leasBySubcounty : getAllLeas ( ) ; }