import Type from "typescript-dotnet-core/Types" ; import OrderedStringKeyDictionary from "typescript-dotnet-core/Collections/Dictionaries/OrderedStringKeyDictionary" ; import { isFiniteEnumerableOrArrayLike } from "typescript-dotnet-core/Collections/Enumeration/Enumerator" ; import UriComponent from "./UriComponent" ; import QueryParam from "./QueryParam" ; import { encode , parse } from "./QueryParams" ; /**
 * Provides a means for parsing and building a set of parameters.
 *
 * In other languages, dictionaries are not reliable for retaining the order of stored values. So for certainty and flexibility we use an ordered dictionary as a base class.
 */ export default class QueryBuilder extends OrderedStringKeyDictionary < UriComponent . Value | UriComponent . Value [ ] > { constructor ( query , decodeValues = true ) { super ( ) ; this . importQuery ( query , decodeValues ) ; } static init ( query , decodeValues = true ) { return new QueryBuilder ( query , decodeValues ) ; } importQuery ( query , decodeValues = true ) { if ( Type . isString ( query ) ) { this . importFromString ( < string > query , decodeValues ) ; } else if ( isFiniteEnumerableOrArrayLike ( query ) ) { this . importEntries ( query ) ; } else { this . importMap ( < UriComponent . Map > query ) ; } return this ; } param values param deserialize param decodeValues returns { QueryBuilder } importFromString ( values , deserialize = true , decodeValues = true ) { const _ = this ; parse ( values , ( key , value ) => { if ( _ . containsKey ( key ) ) { const prev = _ . getValue ( key ) ; if ( ( prev ) instanceof ( Array ) ) prev . push ( value ) ; else _ . setValue ( key , [ < UriComponent . Value > prev , value ] ) ; } else _ . setValue ( key , value ) ; } , deserialize , decodeValues ) ; return this ; } /**
     * Returns the encoded URI string
     */ encode ( prefixIfNotEmpty ? ) { return encode ( this , prefixIfNotEmpty ) ; } toString ( ) { return this . encode ( ) ; } }