import { getOwner } from '@ember/application' ; import type { default as QueryParamsService } from '../../services/query-params' ; export interface ITransformOptions < T > { defaultValue ? : string ; deserialize ? : ( queryParam : string ) => T ; serialize ? : ( queryParam : T ) => string ; } export type Args < T > = [ ] | [ string , ITransformOptions < T > ] | [ ITransformOptions < T > ] | [ string ] ; export function extractArgs < T > ( args , propertyKey ) { const [ maybePathMaybeOptions , maybeOptions ] = args ; let propertyPath ; let options ; if ( typeof maybePathMaybeOptions === 'string' ) { propertyPath = `current.${ maybePathMaybeOptions }` ; options = maybeOptions || { } ; } else { propertyPath = `current.${ propertyKey }` ; options = maybePathMaybeOptions || { } ; } return [ propertyPath , options ] ; } export function tryDeserialize < T > ( value , options ) { value = value ?? options . defaultValue ; if ( ! options . deserialize ) return value ; return options . deserialize ( value ) ; } export function trySerialize < T > ( value , options ) { if ( value === options . defaultValue ) { value = undefined ; } if ( ! options . serialize ) return value ; return options . serialize ( value ) ; } const serviceCache = new WeakMap ( ) ; export function ensureService ( context ) { let service = serviceCache . get ( context ) ; if ( ! service ) { service = getQPService ( context ) ; serviceCache . set ( context , service ) ; } return service ; } export function getQPService ( context ) { return getOwner ( context ) . lookup ( 'service:queryParams' ) ; }