// -*- mode: js; 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>

// Action result templates: used after executing the action
// Either the action was successful, and we're done, or
// the action failed, and we give the user an option to try again

import assert from 'assert';
import { Ast, } from 'thingtalk';
import * as C from '../../ast_manip';
import * as D from '../../dialogue_acts';
import * as S from '../../state_manip';
import ThingpediaLoader from '../../load-thingpedia';
import { SlotBag } from '../../slot_bag';
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("complete_past_action_phrase");
   $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("thingpedia_complete_action_past", undefined)], ((action : Ast.Expression) : (Ast.Expression)|null => {
        // check against weird primitive templates, because
        // later we don't check the query
        if (!(action instanceof Ast.InvocationExpression))
            return null;
        return action;
    }), C.expressionKeyFn, {  });
   $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("thingpedia_complete_action_past", undefined), "for you"], ((action : Ast.Expression, $0 : undefined) : (Ast.Expression)|null => {
        // check against weird primitive templates, because
        // later we don't check the query
        if (!(action instanceof Ast.InvocationExpression))
            return null;
        return action;
    }), C.expressionKeyFn, {  });
   $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("thingpedia_complete_join_action_past", undefined)], ((action : Ast.Expression) : (Ast.Expression)|null => action), C.expressionKeyFn, {  });
   $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("thingpedia_complete_join_action_past", undefined), "for you"], ((action : Ast.Expression, $0 : undefined) : (Ast.Expression)|null => action), C.expressionKeyFn, {  });
   $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("complete_past_action_phrase", undefined), new $runtime.NonTerminal("preposition_input_param", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.inputParamKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((action : Ast.Expression, param : C.InputParamSlot) : (Ast.Expression)|null => C.addActionInputParam($loader, action, param)), C.expressionKeyFn, { priority: (0.1) });
   $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("complete_past_action_phrase", undefined), new $runtime.Choice(["with", "having"]), new $runtime.NonTerminal("npp_input_param", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.inputParamKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((action : Ast.Expression, $0 : undefined, param : C.InputParamSlot) : (Ast.Expression)|null => C.addActionInputParam($loader, action, param)), C.expressionKeyFn, { priority: (0.1) });
   $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("complete_past_action_phrase", undefined), new $runtime.NonTerminal("preposition_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((action : Ast.Expression, param : C.FilterSlot) : (Ast.Expression)|null => {
        if (!(param.ast instanceof Ast.AtomBooleanExpression))
            return null;
        const ipslot = { schema: param.schema, ptype: param.ptype,
            ast: new Ast.InputParam(null, param.ast.name, param.ast.value) };
        return C.addActionInputParam($loader, action, ipslot, { allowOutput: true });
    }), C.expressionKeyFn, { priority: (0.1) });
   $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("complete_past_action_phrase", undefined), new $runtime.Choice(["with", "having"]), new $runtime.NonTerminal("npp_filter", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.filterKeyFn> ? string : void), 0, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], ((action : Ast.Expression, $0 : undefined, param : C.FilterSlot) : (Ast.Expression)|null => {
        if (!(param.ast instanceof Ast.AtomBooleanExpression))
            return null;
        const ipslot = { schema: param.schema, ptype: param.ptype,
            ast: new Ast.InputParam(null, param.ast.name, param.ast.value) };
        return C.addActionInputParam($loader, action, ipslot, { allowOutput: true });
    }), C.expressionKeyFn, { priority: (0.1) });
   $grammar.declareSymbol("generic_action_success_phrase");
   $grammar.addRule("generic_action_success_phrase", ["your request was completed successfully"], (($0 : undefined) : (string)|null => "your request was completed successfully"), undefined, { identity: (true) });
   $grammar.addRule("generic_action_success_phrase", ["consider your request done !"], (($0 : undefined) : (string)|null => "consider your request done !"), undefined, { identity: (true) });
   $grammar.declareSymbol("action_success_phrase_with_result");
   $grammar.addRule("action_success_phrase_with_result", [new $runtime.NonTerminal("generic_action_success_phrase", undefined), ".", new $runtime.NonTerminal("direct_result_info_phrase", undefined)], (($0 : string, $1 : undefined, result : D.ResultPhrase) : (D.ActionSuccessPhraseWithResult)|null => [null, result.info]), D.actionSuccessPhraseWithResultKeyFn, { priority: (1) });
   $grammar.addRule("action_success_phrase_with_result", ["i", new $runtime.NonTerminal("complete_past_action_phrase", undefined), ".", new $runtime.NonTerminal("direct_result_info_phrase", ["functionName" as ("functionName" extends keyof ReturnType<typeof D.resultPhraseKeyFn> ? string : void), 1, "functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void)])], (($0 : undefined, action : Ast.Expression, $1 : undefined, result : D.ResultPhrase) : (D.ActionSuccessPhraseWithResult)|null => [action, result.info]), D.actionSuccessPhraseWithResultKeyFn, { priority: (2) });
   $grammar.declareSymbol("action_success_phrase");
   if ($options.flags.inference) {
       $grammar.addRule("action_success_phrase", [new $runtime.NonTerminal("ctx_completed_action_success", undefined), new $runtime.NonTerminal("generic_excitement_phrase", undefined), new $runtime.NonTerminal("ctx_thingpedia_result", undefined), "."], ((ctx : S.ContextInfo, $0 : string, result : SlotBag, $1 : undefined) : (S.AgentReplyRecord)|null => D.makeThingpediaActionSuccessPhrase(ctx, result)), undefined, { priority: (5) });
   }
   if (!$options.flags.inference) {
       $grammar.addRule("action_success_phrase", [new $runtime.NonTerminal("ctx_completed_action_success", undefined), new $runtime.NonTerminal("generic_excitement_phrase", undefined), new $runtime.NonTerminal("thingpedia_result", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.slotBagKeyFn> ? string : void), 0, "currentFunction" as ("currentFunction" extends keyof ReturnType<typeof S.contextKeyFn> ? string : void)]), "."], ((ctx : S.ContextInfo, $0 : string, result : SlotBag, $1 : undefined) : (S.AgentReplyRecord)|null => D.makeThingpediaActionSuccessPhrase(ctx, result)), undefined, { priority: (5) });
   }
   $grammar.addRule("action_success_phrase", [new $runtime.NonTerminal("ctx_completed_action_success", undefined), new $runtime.NonTerminal("generic_excitement_phrase", undefined), "i", new $runtime.NonTerminal("complete_past_action_phrase", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 0, "currentFunction" as ("currentFunction" extends keyof ReturnType<typeof S.contextKeyFn> ? string : void)]), "."], ((ctx : S.ContextInfo, $0 : string, $1 : undefined, action : Ast.Expression, $2 : undefined) : (S.AgentReplyRecord)|null => D.makeCompleteActionSuccessPhrase(ctx, action, null)), undefined, { priority: (2) });
   $grammar.addRule("action_success_phrase", [new $runtime.NonTerminal("ctx_completed_action_success", undefined), new $runtime.NonTerminal("generic_excitement_phrase", undefined), new $runtime.NonTerminal("action_success_phrase_with_result", undefined)], ((ctx : S.ContextInfo, $0 : string, phrase : D.ActionSuccessPhraseWithResult) : (S.AgentReplyRecord)|null => {
        const [action, info] = phrase;
        if (action)
            return D.makeCompleteActionSuccessPhrase(ctx, action, info);
        else
            return D.makeThingpediaActionSuccessPhrase(ctx, info);
    }), undefined, {  });
   $grammar.addRule("action_success_phrase", [new $runtime.NonTerminal("ctx_completed_action_success", undefined), new $runtime.NonTerminal("generic_excitement_phrase", undefined), new $runtime.NonTerminal("generic_action_success_phrase", undefined)], ((ctx : S.ContextInfo, $0 : string, $1 : string) : (S.AgentReplyRecord)|null => D.makeGenericActionSuccessPhrase(ctx)), undefined, { priority: (-10) });
   $grammar.declareSymbol("short_action_error_message");
   if ($options.flags.inference) {
       $grammar.addRule("short_action_error_message", [new $runtime.NonTerminal("ctx_completed_action_error", undefined), new $runtime.NonTerminal("ctx_thingpedia_error_message", undefined), "."], ((ctx : S.ContextInfo, msg : C.ErrorMessage, $0 : undefined) : (S.ContextInfo)|null => D.checkThingpediaErrorMessage(ctx, msg)), S.contextKeyFn, { priority: (5) });
   }
   if (!$options.flags.inference) {
       $grammar.addRule("short_action_error_message", [new $runtime.NonTerminal("ctx_completed_action_error", undefined), new $runtime.NonTerminal("thingpedia_error_message", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.errorMessageKeyFn> ? string : void), 0, "currentFunction" as ("currentFunction" extends keyof ReturnType<typeof S.contextKeyFn> ? string : void)]), "."], ((ctx : S.ContextInfo, msg : C.ErrorMessage, $0 : undefined) : (S.ContextInfo)|null => D.checkThingpediaErrorMessage(ctx, msg)), S.contextKeyFn, { priority: (5) });
   }
   $grammar.addRule("short_action_error_message", [new $runtime.NonTerminal("ctx_completed_action_error", undefined), new $runtime.NonTerminal("constant_String", undefined)], ((ctx : S.ContextInfo, msg : Ast.Value) : (S.ContextInfo)|null => {
        const error = ctx.error!;
        if (error.equals(msg))
            return ctx;
        else
            return null;
    }), S.contextKeyFn, {  });
   $grammar.declareSymbol("long_action_error_message");
   $grammar.addRule("long_action_error_message", [new $runtime.Choice(["i could not", "it was not possible to"]), new $runtime.NonTerminal("action_description_phrase", undefined), new $runtime.Choice([":", "because"]), new $runtime.NonTerminal("short_action_error_message", undefined)], (($0 : undefined, action : Ast.Invocation, $1 : undefined, ctx : S.ContextInfo) : (S.ContextInfo)|null => D.checkActionErrorMessage(ctx, action)), S.contextKeyFn, {  });
   $grammar.declareSymbol("one_param_try_different_param_question");
   $grammar.addRule("one_param_try_different_param_question", ["would you like to try a different", new $runtime.NonTerminal("input_param", undefined), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined) : (C.ParamSlot[])|null => {
        return [p];
    }), C.paramArrayKeyFn, {  });
   $grammar.addRule("one_param_try_different_param_question", ["would you like a different", new $runtime.NonTerminal("input_param", undefined), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined) : (C.ParamSlot[])|null => {
        return [p];
    }), C.paramArrayKeyFn, {  });
   $grammar.addRule("one_param_try_different_param_question", ["shall i try another", new $runtime.NonTerminal("input_param", undefined), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined) : (C.ParamSlot[])|null => {
        return [p];
    }), C.paramArrayKeyFn, {  });
   $grammar.addRule("one_param_try_different_param_question", ["how about for another", new $runtime.NonTerminal("input_param", undefined), "?"], (($0 : undefined, p : C.ParamSlot, $1 : undefined) : (C.ParamSlot[])|null => {
        return [p];
    }), C.paramArrayKeyFn, {  });
   $grammar.declareSymbol("two_param_try_different_param_question");
   $grammar.addRule("two_param_try_different_param_question", ["would you like to try a different", new $runtime.NonTerminal("input_param", undefined), "or", 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)]), "?"], (($0 : undefined, p1 : C.ParamSlot, $1 : undefined, p2 : C.ParamSlot, $2 : undefined) : (C.ParamSlot[])|null => {
        assert(C.isSameFunction(p1.schema, p2.schema));
        if (p1 === p2)
            return null;
        return [p1, p2];
    }), C.paramArrayKeyFn, {  });
   $grammar.addRule("two_param_try_different_param_question", ["would you like a different", new $runtime.NonTerminal("input_param", undefined), "or", 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)]), "?"], (($0 : undefined, p1 : C.ParamSlot, $1 : undefined, p2 : C.ParamSlot, $2 : undefined) : (C.ParamSlot[])|null => {
        assert(C.isSameFunction(p1.schema, p2.schema));
        if (p1 === p2)
            return null;
        return [p1, p2];
    }), C.paramArrayKeyFn, {  });
   $grammar.addRule("two_param_try_different_param_question", ["shall i try another", new $runtime.NonTerminal("input_param", undefined), "or", 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)]), "?"], (($0 : undefined, p1 : C.ParamSlot, $1 : undefined, p2 : C.ParamSlot, $2 : undefined) : (C.ParamSlot[])|null => {
        assert(C.isSameFunction(p1.schema, p2.schema));
        if (p1 === p2)
            return null;
        return [p1, p2];
    }), C.paramArrayKeyFn, {  });
   $grammar.addRule("two_param_try_different_param_question", ["how about for another", new $runtime.NonTerminal("input_param", undefined), "or", 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)]), "?"], (($0 : undefined, p1 : C.ParamSlot, $1 : undefined, p2 : C.ParamSlot, $2 : undefined) : (C.ParamSlot[])|null => {
        assert(C.isSameFunction(p1.schema, p2.schema));
        if (p1 === p2)
            return null;
        return [p1, p2];
    }), C.paramArrayKeyFn, {  });
   $grammar.declareSymbol("action_error_phrase");
   $grammar.addRule("action_error_phrase", [new $runtime.NonTerminal("sorry_preamble", undefined), new $runtime.NonTerminal("short_action_error_message", undefined), "."], (($0 : string, ctx : S.ContextInfo, $1 : undefined) : (S.AgentReplyRecord)|null => D.makeActionErrorPhrase(ctx, [])), undefined, {  });
   if (!$options.flags.inference) {
       $grammar.addRule("action_error_phrase", [new $runtime.NonTerminal("sorry_preamble", undefined), new $runtime.NonTerminal("short_action_error_message", undefined), ".", new $runtime.NonTerminal("one_param_try_different_param_question", undefined)], (($0 : string, ctx : S.ContextInfo, $1 : undefined, questions : C.ParamSlot[]) : (S.AgentReplyRecord)|null => D.makeActionErrorPhrase(ctx, questions)), undefined, { weight: (1), priority: (0.5) });
       $grammar.addRule("action_error_phrase", [new $runtime.NonTerminal("sorry_preamble", undefined), new $runtime.NonTerminal("short_action_error_message", undefined), ".", new $runtime.NonTerminal("two_param_try_different_param_question", undefined)], (($0 : string, ctx : S.ContextInfo, $1 : undefined, questions : C.ParamSlot[]) : (S.AgentReplyRecord)|null => D.makeActionErrorPhrase(ctx, questions)), undefined, { weight: (0.5) });
       $grammar.addRule("action_error_phrase", [new $runtime.NonTerminal("sorry_preamble", undefined), new $runtime.NonTerminal("long_action_error_message", undefined), ".", new $runtime.NonTerminal("one_param_try_different_param_question", undefined)], (($0 : string, ctx : S.ContextInfo, $1 : undefined, questions : C.ParamSlot[]) : (S.AgentReplyRecord)|null => D.makeActionErrorPhrase(ctx, questions)), undefined, { weight: (1), priority: (0.5) });
       $grammar.addRule("action_error_phrase", [new $runtime.NonTerminal("sorry_preamble", undefined), new $runtime.NonTerminal("long_action_error_message", undefined), ".", new $runtime.NonTerminal("two_param_try_different_param_question", undefined)], (($0 : string, ctx : S.ContextInfo, $1 : undefined, questions : C.ParamSlot[]) : (S.AgentReplyRecord)|null => D.makeActionErrorPhrase(ctx, questions)), undefined, { weight: (0.5) });
   }
};
