import { useLinkResolver } from '@island.is/web/hooks/useLinkResolver' ; import { useDateUtils as _useDateUtils } from '@island.is/web/i18n/useDateUtils' ; import { ISODate , RegName , nameToSlug } from '@island.is/regulations' ; export type RegulationSearchKey = 'q' | 'rn' | 'year' | 'yearTo' | 'ch' | 'iA' | 'iR' | 'page' ; export type RegulationSearchFilters = Record < RegulationSearchKey , string > ; export const useRegulationLinkResolver = ( ) => { const utils = useLinkResolver ( ) ; return { ... utils , linkToRegulation : ( regulationName , props ? ) => { const href = utils . linkResolver ( 'regulation' , [ nameToSlug ( regulationName ) , ] ) . href ; if ( ! props ) { return href ; } if ( ( 'd' in props && props . d ) || ( 'on' in props && props . on ) ) { const date = 'd' in props ? '/d/' + props . d : '/on/' + props . on ; const diff = props . diff ? '/diff' : '' ; const earlierDate = props . diff && 'earlierDate' in props ? '/' + props . earlierDate : '' ; return href + date + diff + earlierDate ; } if ( 'diff' in props && props . diff ) { return href + '/diff' ; } if ( 'original' in props && props . original ) { return href + '/original' ; } return href ; } , linkToRegulationSearch : < Keys extends RegulationSearchKey > ( filters ) => utils . linkResolver ( 'regulationshome' ) . href + '?' + new URLSearchParams ( filters ) . toString ( ) , } ; } ; export const useDateUtils = ( ) => { const dateUtl = _useDateUtils ( ) ; const formatDate = ( isoDate ) => { return dateUtl . format ( new Date ( isoDate ) , 'd. MMM yyyy' ) ; } ; return { formatDate } ; } ;