// -*- mode: typescript; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2020 The Board of Trustees of the Leland Stanford Junior University
//
// 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.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>

// Slot fill templates: the agent asks a question, and the user replies
//
// There are two types slot-fill templates:
// - for searches: the agent is trying to add a filter
// - for actions/queries w/ parameters: the agent is trying to add a parameter

import { Ast, } from 'thingtalk';
import * as C from '../../ast_manip';
import ThingpediaLoader from '../../load-thingpedia';
import type * as $Genie from "genie-toolkit";
export default async function($runtime : typeof $Genie.SentenceGeneratorRuntime, $ttUtils : typeof $Genie.ThingTalkUtils, $options : $Genie.SentenceGeneratorTypes.GrammarOptions, $locale : $Genie.I18n.LanguagePack, $grammar : $Genie.SentenceGenerator<any, any>, $loader : ThingpediaLoader) : Promise<void> {
   $grammar.declareSymbol("one_param_slot_fill_question");
   $grammar.addRule("one_param_slot_fill_question", [new $runtime.NonTerminal("thingpedia_slot_fill_question", undefined), "?"], ((p : C.ParamSlot, $0 : undefined) : (C.ParamSlot[])|null => [p]), C.paramArrayKeyFn, { priority: (0.1) });
   $grammar.addRule("one_param_slot_fill_question", ["what", new $runtime.NonTerminal("input_param", undefined), new $runtime.NonTerminal("prefer_question_phrase", undefined), "?"], (($0 : undefined, p : C.ParamSlot, $1 : string, $2 : undefined) : (C.ParamSlot[])|null => [p]), C.paramArrayKeyFn, {  });
   $grammar.declareSymbol("two_param_slot_fill_question");
   $grammar.addRule("two_param_slot_fill_question", ["what", new $runtime.NonTerminal("input_param", undefined), "and", new $runtime.NonTerminal("input_param", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), new $runtime.NonTerminal("prefer_question_phrase", undefined), "?"], (($0 : undefined, p1 : C.ParamSlot, $1 : undefined, p2 : C.ParamSlot, $2 : string, $3 : undefined) : (C.ParamSlot[])|null => {
        if (p1.name === p2.name || !C.isSameFunction(p1.schema, p2.schema))
            return null;
        return [p1, p2];
    }), C.paramArrayKeyFn, {  });
   $grammar.addRule("two_param_slot_fill_question", [new $runtime.NonTerminal("thingpedia_slot_fill_question", undefined), "and", "what", new $runtime.NonTerminal("input_param", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), new $runtime.NonTerminal("prefer_question_phrase", undefined), "?"], ((p1 : C.ParamSlot, $0 : undefined, $1 : undefined, p2 : C.ParamSlot, $2 : string, $3 : undefined) : (C.ParamSlot[])|null => {
        if (p1.name === p2.name || !C.isSameFunction(p1.schema, p2.schema))
            return null;
        return [p1, p2];
    }), C.paramArrayKeyFn, {  });
   $grammar.addRule("two_param_slot_fill_question", ["what", new $runtime.NonTerminal("input_param", undefined), new $runtime.NonTerminal("prefer_question_phrase", undefined), "and", new $runtime.NonTerminal("thingpedia_slot_fill_question", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.paramKeyFn> ? string : void)]), "?"], (($0 : undefined, p1 : C.ParamSlot, $1 : string, $2 : undefined, p2 : C.ParamSlot, $3 : undefined) : (C.ParamSlot[])|null => {
        if (p1.name === p2.name || !C.isSameFunction(p1.schema, p2.schema))
            return null;
        return [p1, p2];
    }), C.paramArrayKeyFn, {  });
   $grammar.declareSymbol("slot_fill_question");
   $grammar.addRule("slot_fill_question", [new $runtime.NonTerminal("one_param_slot_fill_question", undefined)], (($0 : C.ParamSlot[]) : (C.ParamSlot[])|null => { return $0; }), C.paramArrayKeyFn, { priority: (0.1), identity: (true) });
   if (!$options.flags.inference) {
       $grammar.addRule("slot_fill_question", [new $runtime.NonTerminal("two_param_slot_fill_question", undefined)], (($0 : C.ParamSlot[]) : (C.ParamSlot[])|null => { return $0; }), C.paramArrayKeyFn, { identity: (true) });
   }
   $grammar.declareSymbol("imprecise_slot_fill_answer_phrase");
   $grammar.addRule("imprecise_slot_fill_answer_phrase", [new $runtime.NonTerminal("constant_Any", undefined)], ((slot : Ast.Value) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_answer_phrase", [new $runtime.NonTerminal("npp_input_param", undefined)], ((slot : C.InputParamSlot) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_answer_phrase", [new $runtime.NonTerminal("apv_input_param", undefined)], ((slot : C.InputParamSlot) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_answer_phrase", [new $runtime.NonTerminal("pvp_input_param", undefined)], ((slot : C.InputParamSlot) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_answer_phrase", [new $runtime.NonTerminal("preposition_input_param", undefined)], ((slot : C.InputParamSlot) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.declareSymbol("imprecise_slot_fill_coref_answer");
   $grammar.addRule("imprecise_slot_fill_coref_answer", [new $runtime.NonTerminal("coref_constant", undefined)], ((slot : Ast.Value) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_coref_answer", [new $runtime.NonTerminal("coref_npp_input_param", undefined)], ((slot : C.InputParamSlot) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_coref_answer", [new $runtime.NonTerminal("coref_apv_input_param", undefined)], ((slot : C.InputParamSlot) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_coref_answer", [new $runtime.NonTerminal("coref_pvp_input_param", undefined)], ((slot : C.InputParamSlot) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_coref_answer", [new $runtime.NonTerminal("coref_preposition_input_param", undefined)], ((slot : C.InputParamSlot) : (Ast.Value|C.InputParamSlot)|null => slot), undefined, {  });
   $grammar.declareSymbol("imprecise_slot_fill_answer");
   $grammar.addRule("imprecise_slot_fill_answer", [new $runtime.NonTerminal("imprecise_slot_fill_answer_phrase", undefined), new $runtime.Choice(["please", ""]), "."], ((ans : Ast.Value|C.InputParamSlot, $0 : undefined, $1 : undefined) : (Ast.Value|C.InputParamSlot)|null => ans), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_answer", [new $runtime.NonTerminal("generic_preamble_for_answer", undefined), new $runtime.NonTerminal("imprecise_slot_fill_answer_phrase", undefined), "."], (($0 : string, ans : Ast.Value|C.InputParamSlot, $1 : undefined) : (Ast.Value|C.InputParamSlot)|null => ans), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_answer", [new $runtime.NonTerminal("imprecise_slot_fill_coref_answer", undefined), new $runtime.Choice(["please", ""]), "."], ((ans : Ast.Value|C.InputParamSlot, $0 : undefined, $1 : undefined) : (Ast.Value|C.InputParamSlot)|null => ans), undefined, {  });
   $grammar.addRule("imprecise_slot_fill_answer", [new $runtime.NonTerminal("generic_preamble_for_answer", undefined), new $runtime.NonTerminal("imprecise_slot_fill_coref_answer", undefined), "."], (($0 : string, ans : Ast.Value|C.InputParamSlot, $1 : undefined) : (Ast.Value|C.InputParamSlot)|null => ans), undefined, {  });
   $grammar.declareSymbol("precise_slot_fill_answer");
   $grammar.addRule("precise_slot_fill_answer", [new $runtime.NonTerminal("generic_preamble_for_action", undefined), new $runtime.NonTerminal("action_continuation_command", undefined)], (($0 : string, action : Ast.Invocation) : (Ast.Invocation)|null => action), C.invocationKeyFn, {  });
   $grammar.addRule("precise_slot_fill_answer", [new $runtime.NonTerminal("generic_preamble_for_answer", undefined), new $runtime.NonTerminal("generic_article", undefined), new $runtime.NonTerminal("continuation_query", undefined)], (($0 : string, $1 : string, query : Ast.Invocation) : (Ast.Invocation)|null => query), C.invocationKeyFn, {  });
   $grammar.addRule("precise_slot_fill_answer", [new $runtime.NonTerminal("generic_verb_for_search", undefined), new $runtime.NonTerminal("generic_article", undefined), new $runtime.NonTerminal("continuation_query", undefined)], (($0 : string, $1 : string, query : Ast.Invocation) : (Ast.Invocation)|null => query), C.invocationKeyFn, {  });
};
