// -*- 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>

// Coref actions: actions that operate on the current result, referred by coreference ("it", "that", "the restaurant")

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 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("coref_action_phrase");
   $grammar.addRule("coref_action_phrase", [new $runtime.NonTerminal("action_coref_context", undefined)], ((action : C.ExpressionWithCoreference) : (Ast.Invocation)|null => {
        if (!(action.expression instanceof Ast.InvocationExpression))
            return null;
        if (action.slot !== null || !$loader.isIDType(action.type)) // TODO: non-id parameter passing
            return null;
        return C.replacePlaceholderWithUndefined(action.expression.invocation, action.pname!);
    }), C.invocationKeyFn, {  });
   $grammar.addRule("coref_action_phrase", [new $runtime.NonTerminal("get_command_coref_context", undefined)], ((action : C.ExpressionWithCoreference) : (Ast.Invocation)|null => {
        if (!(action.expression instanceof Ast.InvocationExpression))
            return null;
        if (action.slot !== null || !$loader.isIDType(action.type)) // TODO: non-id parameter passing
            return null;
        return C.replacePlaceholderWithUndefined(action.expression.invocation, action.pname!);
    }), C.invocationKeyFn, {  });
   $grammar.declareSymbol("list_coref_action_phrase");
   $grammar.addRule("list_coref_action_phrase", [new $runtime.NonTerminal("action_coref_list_context", undefined)], ((action : C.ExpressionWithCoreference) : (Ast.Invocation)|null => {
        if (!(action.expression instanceof Ast.InvocationExpression))
            return null;
        if (action.slot !== null || !$loader.isIDType(action.type)) // TODO: non-id parameter passing
            return null;
        return C.replacePlaceholderWithUndefined(action.expression.invocation, action.pname!);
    }), C.invocationKeyFn, {  });
   $grammar.addRule("list_coref_action_phrase", [new $runtime.NonTerminal("get_command_coref_list_context", undefined)], ((action : C.ExpressionWithCoreference) : (Ast.Invocation)|null => {
        if (!(action.expression instanceof Ast.InvocationExpression))
            return null;
        if (action.slot !== null || !$loader.isIDType(action.type)) // TODO: non-id parameter passing
            return null;
        return C.replacePlaceholderWithUndefined(action.expression.invocation, action.pname!);
    }), C.invocationKeyFn, {  });
   $grammar.declareSymbol("contextual_action_phrase");
   $grammar.addRule("contextual_action_phrase", [new $runtime.NonTerminal("complete_action", undefined)], ((action : Ast.Expression) : (Ast.Invocation)|null => {
        if (!(action instanceof Ast.InvocationExpression))
            return null;
        return action.invocation;
    }), C.invocationKeyFn, {  });
   $grammar.addRule("contextual_action_phrase", [new $runtime.NonTerminal("complete_get_command", undefined)], ((action : Ast.Expression) : (Ast.Invocation)|null => {
        if (!(action instanceof Ast.InvocationExpression))
            return null;
        return action.invocation;
    }), C.invocationKeyFn, {  });
   $grammar.declareSymbol("action_coref_recommendation");
   $grammar.addRule("action_coref_recommendation", ["would you like to", new $runtime.NonTerminal("coref_action_phrase", undefined), "?"], (($0 : undefined, action : Ast.Invocation, $1 : undefined) : (Ast.Invocation)|null => {
        if (action.in_params.some((ip) => !ip.value.isUndefined))
            return null;
        return action;
    }), C.invocationKeyFn, { priority: (1) });
   $grammar.addRule("action_coref_recommendation", ["would you like me to", new $runtime.NonTerminal("coref_action_phrase", undefined), " for you ?"], (($0 : undefined, action : Ast.Invocation, $1 : undefined) : (Ast.Invocation)|null => {
        if (action.in_params.some((ip) => !ip.value.isUndefined))
            return null;
        return action;
    }), C.invocationKeyFn, { priority: (1) });
   $grammar.declareSymbol("action_coref_list_proposal");
   $grammar.addRule("action_coref_list_proposal", ["would you like to", new $runtime.NonTerminal("list_coref_action_phrase", undefined), "?"], (($0 : undefined, action : Ast.Invocation, $1 : undefined) : (Ast.Invocation)|null => {
        if (action.in_params.some((ip) => !ip.value.isUndefined))
            return null;
        return action;
    }), C.invocationKeyFn, { priority: (1) });
   $grammar.declareSymbol("coref_action_command");
   $grammar.addRule("coref_action_command", [new $runtime.NonTerminal("ctx_with_action", undefined), new $runtime.NonTerminal("coref_action_phrase", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.invocationKeyFn> ? string : void), 0, "nextFunction" as ("nextFunction" extends keyof ReturnType<typeof S.contextKeyFn> ? string : void)])], ((ctx : S.ContextInfo, base : Ast.Invocation) : (Ast.Invocation)|null => D.contextualAction(ctx, base)), C.invocationKeyFn, {  });
   $grammar.addRule("coref_action_command", [new $runtime.NonTerminal("ctx_without_action", undefined), new $runtime.NonTerminal("coref_action_phrase", undefined)], ((ctx : S.ContextInfo, base : Ast.Invocation) : (Ast.Invocation)|null => base), C.invocationKeyFn, {  });
   $grammar.addRule("coref_action_command", [new $runtime.NonTerminal("coref_action_command", 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.invocationKeyFn> ? string : void)])], ((action : Ast.Invocation, param : C.InputParamSlot) : (Ast.Invocation)|null => C.addInvocationInputParam($loader, action, param)), C.invocationKeyFn, {  });
   $grammar.addRule("coref_action_command", [new $runtime.NonTerminal("coref_action_command", 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.invocationKeyFn> ? string : void)])], ((action : Ast.Invocation, $0 : undefined, param : C.InputParamSlot) : (Ast.Invocation)|null => C.addInvocationInputParam($loader, action, param)), C.invocationKeyFn, {  });
   $grammar.declareSymbol("action_by_name_command");
   $grammar.addRule("action_by_name_command", [new $runtime.NonTerminal("ctx_incomplete_action", undefined), new $runtime.NonTerminal("contextual_action_phrase", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.invocationKeyFn> ? string : void), 0, "nextFunction" as ("nextFunction" extends keyof ReturnType<typeof S.contextKeyFn> ? string : void)])], ((ctx : S.ContextInfo, base : Ast.Invocation) : (Ast.Invocation)|null => D.contextualAction(ctx, base)), C.invocationKeyFn, {  });
   $grammar.addRule("action_by_name_command", [new $runtime.NonTerminal("ctx_without_action", undefined), new $runtime.NonTerminal("contextual_action_phrase", undefined)], ((ctx : S.ContextInfo, base : Ast.Invocation) : (Ast.Invocation)|null => base), C.invocationKeyFn, {  });
   $grammar.addRule("action_by_name_command", [new $runtime.NonTerminal("action_by_name_command", 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.invocationKeyFn> ? string : void)])], ((action : Ast.Invocation, param : C.InputParamSlot) : (Ast.Invocation)|null => C.addInvocationInputParam($loader, action, param)), C.invocationKeyFn, {  });
   $grammar.addRule("action_by_name_command", [new $runtime.NonTerminal("action_by_name_command", 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.invocationKeyFn> ? string : void)])], ((action : Ast.Invocation, $0 : undefined, param : C.InputParamSlot) : (Ast.Invocation)|null => C.addInvocationInputParam($loader, action, param)), C.invocationKeyFn, {  });
   $grammar.declareSymbol("action_continuation_command");
   $grammar.addRule("action_continuation_command", [new $runtime.NonTerminal("ctx_incomplete_action", undefined), new $runtime.NonTerminal("coref_action_phrase", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.invocationKeyFn> ? string : void), 0, "nextFunction" as ("nextFunction" extends keyof ReturnType<typeof S.contextKeyFn> ? string : void)])], ((ctx : S.ContextInfo, base : Ast.Invocation) : (Ast.Invocation)|null => D.contextualAction(ctx, base)), C.invocationKeyFn, {  });
   $grammar.addRule("action_continuation_command", [new $runtime.NonTerminal("ctx_incomplete_action", undefined), new $runtime.NonTerminal("contextual_action_phrase", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.invocationKeyFn> ? string : void), 0, "nextFunction" as ("nextFunction" extends keyof ReturnType<typeof S.contextKeyFn> ? string : void)])], ((ctx : S.ContextInfo, base : Ast.Invocation) : (Ast.Invocation)|null => D.contextualAction(ctx, base)), C.invocationKeyFn, {  });
   $grammar.addRule("action_continuation_command", [new $runtime.NonTerminal("action_continuation_command", 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.invocationKeyFn> ? string : void)])], ((action : Ast.Invocation, param : C.InputParamSlot) : (Ast.Invocation)|null => C.addInvocationInputParam($loader, action, param)), C.invocationKeyFn, {  });
   $grammar.addRule("action_continuation_command", [new $runtime.NonTerminal("action_continuation_command", 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.invocationKeyFn> ? string : void)])], ((action : Ast.Invocation, $0 : undefined, param : C.InputParamSlot) : (Ast.Invocation)|null => C.addInvocationInputParam($loader, action, param)), C.invocationKeyFn, {  });
   $grammar.declareSymbol("continuation_query");
   $grammar.addRule("continuation_query", [new $runtime.NonTerminal("ctx_incomplete_action", undefined), new $runtime.NonTerminal("complete_table", ["functionName" as ("functionName" extends keyof ReturnType<typeof C.expressionKeyFn> ? string : void), 0, "nextFunction" as ("nextFunction" extends keyof ReturnType<typeof S.contextKeyFn> ? string : void)])], ((ctx : S.ContextInfo, base : Ast.Expression) : (Ast.Invocation)|null => {
        if (!(base instanceof Ast.InvocationExpression))
            return null;
        return D.contextualAction(ctx, base.invocation);
    }), C.invocationKeyFn, {  });
   $grammar.declareSymbol("action_description_phrase");
   $grammar.addRule("action_description_phrase", [new $runtime.NonTerminal("coref_action_phrase", undefined)], (($0 : Ast.Invocation) : (Ast.Invocation)|null => { return $0; }), C.invocationKeyFn, { identity: (true) });
   $grammar.addRule("action_description_phrase", [new $runtime.NonTerminal("thingpedia_complete_action", undefined)], ((action : Ast.Expression) : (Ast.Invocation)|null => {
        if (!(action instanceof Ast.InvocationExpression))
            return null;
        return action.invocation;
    }), C.invocationKeyFn, {  });
   $grammar.addRule("action_description_phrase", [new $runtime.NonTerminal("action_description_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.invocationKeyFn> ? string : void)])], ((action : Ast.Invocation, param : C.InputParamSlot) : (Ast.Invocation)|null => C.addInvocationInputParam($loader, action, param)), C.invocationKeyFn, {  });
   $grammar.addRule("action_description_phrase", [new $runtime.NonTerminal("action_description_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.invocationKeyFn> ? string : void)])], ((action : Ast.Invocation, $0 : undefined, param : C.InputParamSlot) : (Ast.Invocation)|null => C.addInvocationInputParam($loader, action, param)), C.invocationKeyFn, {  });
};
