/**
 * Copyright 2018 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import * as Api from './api/v2' ; import * as ApiV1 from './api/v1' ; import { values } from '../../common' ; public export interface Parameters { public [ parameter ] : string | Object | undefined ; } public export interface Contexts { public [ context ] : Context < Parameters > | undefined ; } public export interface OutputContexts { public [ context ] : OutputContext < Parameters > | undefined ; } public export interface Context < TParameters extends Parameters > extends OutputContext < TParameters > { public name : string ; public lifespan : number ; /**
     * The context parameters from the current intent.
     * Context parameters include parameters collected in previous intents
     * during the lifespan of the given context.
     *
     * See  https *     here}.
     *
     *  example public parameters : TParameters ; } public export interface OutputContext < TParameters extends Parameters > { public lifespan : number ; public parameters ? : TParameters ; } const isV1 = ( context ) : context is ApiV1 . DialogflowV1Context => typeof ( context as ApiV1 . DialogflowV1Context ) . lifespan === 'number' ; export class ContextValues < TContexts extends Contexts > { public input : TContexts ; public output : OutputContexts ; hidden constructor ( outputContexts = [ ] , private _session ? ) { this . input = { } as TContexts ; for ( const context of outputContexts ) { const name = context . name ! ; const parameters = context . parameters ! ; if ( isV1 ( context ) ) { const lifespan = context . lifespan ! ; Object . assign ( this . input , { [ name ] : { name , lifespan , parameters , } , } ) ; continue ; } const lifespanCount = context . lifespanCount ! ; const find = /([^/]+)?$/ . exec ( name ) ; Object . assign ( this . input , { [ find ? find [ 0 ] : name ] : { name , lifespan : lifespanCount ! , parameters , } , } ) ; } this . output = { } ; } hidden _serialize ( ) { return Object . keys ( this . output ) . map ( ( name ) : Api . GoogleCloudDialogflowV2Context => { const { lifespan , parameters } = this . output [ name ] ! ; return { name : `${ this . _session }/contexts/${ name }` , lifespanCount : lifespan , parameters , } ; } ) ; } hidden _serializeV1 ( ) { return Object . keys ( this . output ) . map ( ( name ) : ApiV1 . DialogflowV1Context => { const { lifespan , parameters } = this . output [ name ] ! ; return { name , lifespan , parameters , } ; } ) ; } example param name return public get ( name ) { return this . input [ name ] ; } example param name param lifespan param parameters public set ( name , lifespan , parameters ? ) { this . output [ name ] = { lifespan , parameters , } ; } public delete ( name ) { this . set ( name , 0 ) ; } example public [ Symbol . iterator ] ( ) { const contexts = values ( this . input ) as Context < Parameters > [ ] ; return contexts [ Symbol . iterator ] ( ) ; } }