"use strict";
// -*- 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>
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// 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
const assert_1 = __importDefault(require("assert"));
const thingtalk_1 = require("thingtalk");
const C = __importStar(require("../../ast_manip"));
const D = __importStar(require("../../dialogue_acts"));
const S = __importStar(require("../../state_manip"));
async function default_1($runtime, $ttUtils, $options, $locale, $grammar, $loader) {
    $grammar.declareSymbol("complete_past_action_phrase");
    $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("thingpedia_complete_action_past", undefined)], ((action) => {
        // check against weird primitive templates, because
        // later we don't check the query
        if (!(action instanceof thingtalk_1.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, $0) => {
        // check against weird primitive templates, because
        // later we don't check the query
        if (!(action instanceof thingtalk_1.Ast.InvocationExpression))
            return null;
        return action;
    }), C.expressionKeyFn, {});
    $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("thingpedia_complete_join_action_past", undefined)], ((action) => action), C.expressionKeyFn, {});
    $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("thingpedia_complete_join_action_past", undefined), "for you"], ((action, $0) => action), C.expressionKeyFn, {});
    $grammar.addRule("complete_past_action_phrase", [new $runtime.NonTerminal("complete_past_action_phrase", undefined), new $runtime.NonTerminal("preposition_input_param", ["functionName", 0, "functionName"])], ((action, param) => 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", 0, "functionName"])], ((action, $0, param) => 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", 0, "functionName"])], ((action, param) => {
        if (!(param.ast instanceof thingtalk_1.Ast.AtomBooleanExpression))
            return null;
        const ipslot = { schema: param.schema, ptype: param.ptype,
            ast: new thingtalk_1.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", 0, "functionName"])], ((action, $0, param) => {
        if (!(param.ast instanceof thingtalk_1.Ast.AtomBooleanExpression))
            return null;
        const ipslot = { schema: param.schema, ptype: param.ptype,
            ast: new thingtalk_1.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) => "your request was completed successfully"), undefined, { identity: (true) });
    $grammar.addRule("generic_action_success_phrase", ["consider your request done !"], (($0) => "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, $1, result) => [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", 1, "functionName"])], (($0, action, $1, result) => [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, $0, result, $1) => 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", 0, "currentFunction"]), "."], ((ctx, $0, result, $1) => 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", 0, "currentFunction"]), "."], ((ctx, $0, $1, action, $2) => 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, $0, phrase) => {
        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, $0, $1) => 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, msg, $0) => 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", 0, "currentFunction"]), "."], ((ctx, msg, $0) => 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, msg) => {
        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, action, $1, ctx) => 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, p, $1) => {
        return [p];
    }), C.paramArrayKeyFn, {});
    $grammar.addRule("one_param_try_different_param_question", ["would you like a different", new $runtime.NonTerminal("input_param", undefined), "?"], (($0, p, $1) => {
        return [p];
    }), C.paramArrayKeyFn, {});
    $grammar.addRule("one_param_try_different_param_question", ["shall i try another", new $runtime.NonTerminal("input_param", undefined), "?"], (($0, p, $1) => {
        return [p];
    }), C.paramArrayKeyFn, {});
    $grammar.addRule("one_param_try_different_param_question", ["how about for another", new $runtime.NonTerminal("input_param", undefined), "?"], (($0, p, $1) => {
        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", 1, "functionName"]), "?"], (($0, p1, $1, p2, $2) => {
        assert_1.default(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", 1, "functionName"]), "?"], (($0, p1, $1, p2, $2) => {
        assert_1.default(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", 1, "functionName"]), "?"], (($0, p1, $1, p2, $2) => {
        assert_1.default(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", 1, "functionName"]), "?"], (($0, p1, $1, p2, $2) => {
        assert_1.default(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, ctx, $1) => 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, ctx, $1, questions) => 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, ctx, $1, questions) => 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, ctx, $1, questions) => 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, ctx, $1, questions) => D.makeActionErrorPhrase(ctx, questions)), undefined, { weight: (0.5) });
    }
}
exports.default = default_1;
;
//# sourceMappingURL=action-results.genie.js.map