"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: Silei Xu <silei@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 });
const thingtalk_1 = require("thingtalk");
const C = __importStar(require("../ast_manip"));
const load_thingpedia_1 = __importDefault(require("../load-thingpedia"));
async function default_1($runtime, $ttUtils, $options, $locale, $grammar, $loader) {
    $loader = new load_thingpedia_1.default($runtime, $ttUtils, $grammar, $locale, $options);
    await (await $runtime.import("../common.genie", __dirname))($runtime, $ttUtils, $options, $locale, $grammar, $loader);
    await $loader.init();
    await (await $runtime.import("./constants.genie", __dirname))($runtime, $ttUtils, $options, $locale, $grammar, $loader);
    $grammar.declareSymbol("table");
    $grammar.addRule("table", [new $runtime.NonTerminal("thingpedia_complete_query", undefined)], (($0) => { return $0; }), C.expressionKeyFn, { identity: (true) });
    $grammar.declareSymbol("filter");
    $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Any", undefined), "is", new $runtime.NonTerminal("constant_Any", undefined)], (($0, p, $1, v) => C.makeFilter($loader, p, '==', v)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Any", undefined), "is not", new $runtime.NonTerminal("constant_Any", undefined)], (($0, p, $1, v) => C.makeFilter($loader, p, '==', v, true)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Array__Any", undefined), "is", new $runtime.NonTerminal("constant_Any", undefined)], (($0, p, $1, v) => C.makeFilter($loader, p, 'contains', v)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Array__Any", undefined), "is not", new $runtime.NonTerminal("constant_Any", undefined)], (($0, p, $1, v) => C.makeFilter($loader, p, 'contains', v, true)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)]), new $runtime.Choice(["is samller than", "at most"]), new $runtime.NonTerminal("constant_Numeric", undefined)], (($0, p, $1, v) => C.makeFilter($loader, p, '<=', v)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["whose", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)]), new $runtime.Choice(["is larger than", "at least"]), new $runtime.NonTerminal("constant_Numeric", undefined)], (($0, p, $1, v) => C.makeFilter($loader, p, '>=', v)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["that", new $runtime.NonTerminal("out_param_avp", undefined), new $runtime.NonTerminal("constant_Any", undefined)], (($0, p, v) => C.makeFilter($loader, p, '==', v)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["that not", new $runtime.NonTerminal("out_param_avp", undefined), new $runtime.NonTerminal("constant_Any", undefined)], (($0, p, v) => C.makeFilter($loader, p, '==', v, true)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["that", new $runtime.NonTerminal("out_param_avp", undefined), new $runtime.NonTerminal("constant_Any", undefined)], (($0, p, v) => C.makeFilter($loader, p, 'contains', v)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["that not", new $runtime.NonTerminal("out_param_avp", undefined), new $runtime.NonTerminal("constant_Any", undefined)], (($0, p, v) => C.makeFilter($loader, p, 'contains', v, true)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["that has", new $runtime.NonTerminal("constant_Number", undefined), new $runtime.NonTerminal("out_param_Any", undefined)], (($0, v, p) => C.makeAggregateFilter(p, 'count', '*', '==', v)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["that has", new $runtime.Choice(["less than", "at most"]), new $runtime.NonTerminal("constant_Number", undefined), new $runtime.NonTerminal("out_param_Any", undefined)], (($0, $1, v, p) => C.makeAggregateFilter(p, 'count', '*', '<=', v)), C.filterKeyFn, {});
    $grammar.addRule("filter", ["that has", new $runtime.Choice(["more than", "at least"]), new $runtime.NonTerminal("constant_Number", undefined), new $runtime.NonTerminal("out_param_Any", undefined)], (($0, $1, v, p) => C.makeAggregateFilter(p, 'count', '*', '>=', v)), C.filterKeyFn, {});
    $grammar.declareSymbol("filtered_table");
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), new $runtime.NonTerminal("filter", undefined)], ((t, f) => C.addFilter(t, f)), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "and", new $runtime.NonTerminal("filter", undefined)], ((t, $0, f) => C.addFilter(t, f)), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is", new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((t1, $0, p, $1, t2) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is", new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((t1, $0, p, $1, t2) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is", new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((t1, $0, p, $1, t2) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is", new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((t1, $0, p, $1, t2) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is not", new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((t1, $0, p, $1, t2) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is not", new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((t1, $0, p, $1, t2) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is not", new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((t1, $0, p, $1, t2) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that is not", new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((t1, $0, p, $1, t2) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that", new $runtime.NonTerminal("table", undefined), new $runtime.NonTerminal("out_param_avp", undefined)], ((t1, $0, t2, p) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that", new $runtime.NonTerminal("filtered_table", undefined), "and", new $runtime.NonTerminal("out_param_avp", undefined)], ((t1, $0, t2, $1, p) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that", new $runtime.NonTerminal("table", undefined), "not", new $runtime.NonTerminal("out_param_avp", undefined)], ((t1, $0, t2, $1, p) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that", new $runtime.NonTerminal("filtered_table", undefined), "and not", new $runtime.NonTerminal("out_param_avp", undefined)], ((t1, $0, t2, $1, p) => {
        return C.addReverseGetPredicateJoin(t1, t2, p.name, true);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that has the smallest", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)])], ((table, $0, p) => C.makeArgMaxMinTable(table, p.name, 'asc')), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "that has the smallest", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)])], ((table, $0, p) => C.makeArgMaxMinTable(table, p.name, 'asc')), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that has the largest", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)])], ((table, $0, p) => C.makeArgMaxMinTable(table, p.name, 'desc')), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "that has the largest", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)])], ((table, $0, p) => C.makeArgMaxMinTable(table, p.name, 'desc')), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that has the least number of", new $runtime.NonTerminal("out_param_Array__Any", undefined)], ((table, $0, p) => {
        if (!table.schema.out[p.name] || !table.schema.out[p.name].isArray)
            return null;
        const conflictParam = C.hasConflictParam(table, p.name, 'count');
        if (conflictParam)
            return C.makeArgMaxMinTable(table, conflictParam, 'asc');
        return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, thingtalk_1.Type.Number, 'desc');
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "that has the least number of", new $runtime.NonTerminal("out_param_Array__Any", undefined)], ((table, $0, p) => {
        if (!table.schema.out[p.name] || !table.schema.out[p.name].isArray)
            return null;
        const conflictParam = C.hasConflictParam(table, p.name, 'count');
        if (conflictParam)
            return C.makeArgMaxMinTable(table, conflictParam, 'asc');
        return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, thingtalk_1.Type.Number, 'desc');
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("table", undefined), "that has the most number of", new $runtime.NonTerminal("out_param_Array__Any", undefined)], ((table, $0, p) => {
        if (!table.schema.out[p.name] || !table.schema.out[p.name].isArray)
            return null;
        const conflictParam = C.hasConflictParam(table, p.name, 'count');
        if (conflictParam)
            return C.makeArgMaxMinTable(table, conflictParam, 'desc');
        return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, thingtalk_1.Type.Number, 'desc');
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("filtered_table", undefined), "that has the most number of", new $runtime.NonTerminal("out_param_Array__Any", undefined)], ((table, $0, p) => {
        if (!table.schema.out[p.name] || !table.schema.out[p.name].isArray)
            return null;
        const conflictParam = C.hasConflictParam(table, p.name, 'count');
        if (conflictParam)
            return C.makeArgMaxMinTable(table, conflictParam, 'desc');
        return C.makeAggComputeArgMinMaxExpression(table, 'count', null, p, thingtalk_1.Type.Number, 'desc');
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((p, $0, table) => {
        if (p.name === 'id')
            return null;
        return C.makeSingleFieldProjection($loader, 'table', null, table, p);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("out_param_Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((p, $0, table) => {
        if (p.name === 'id')
            return null;
        return C.makeSingleFieldProjection($loader, 'table', null, table, p);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("table", undefined)], ((p, $0, table) => {
        if (p.name === 'id')
            return null;
        return C.makeSingleFieldProjection($loader, 'table', null, table, p);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", [new $runtime.NonTerminal("out_param_Array__Any", undefined), "of", new $runtime.NonTerminal("filtered_table", undefined)], ((p, $0, table) => {
        if (p.name === 'id')
            return null;
        return C.makeSingleFieldProjection($loader, 'table', null, table, p);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", ["number of", new $runtime.NonTerminal("table", undefined)], (($0, table) => {
        if (!table.schema.is_list)
            return null;
        const newSchema = table.schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT)
            .addArguments([new thingtalk_1.Ast.ArgumentDef(null, thingtalk_1.Ast.ArgDirection.OUT, 'count', thingtalk_1.Type.Number)]);
        newSchema.minimal_projection = [];
        if (newSchema instanceof thingtalk_1.Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new thingtalk_1.Ast.Value.Array([]);
        const agg = new thingtalk_1.Ast.AggregationExpression(null, table, '*', 'count', newSchema);
        return new thingtalk_1.Ast.ProjectionExpression(null, agg, ['count'], [], [], newSchema);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", ["number of", new $runtime.NonTerminal("filtered_table", undefined)], (($0, table) => {
        if (!table.schema.is_list)
            return null;
        const newSchema = table.schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT)
            .addArguments([new thingtalk_1.Ast.ArgumentDef(null, thingtalk_1.Ast.ArgDirection.OUT, 'count', thingtalk_1.Type.Number)]);
        newSchema.minimal_projection = [];
        if (newSchema instanceof thingtalk_1.Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new thingtalk_1.Ast.Value.Array([]);
        const agg = new thingtalk_1.Ast.AggregationExpression(null, table, '*', 'count', newSchema);
        return new thingtalk_1.Ast.ProjectionExpression(null, agg, ['count'], [], [], newSchema);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", ["total", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)]), "of", new $runtime.NonTerminal("table", undefined)], (($0, p, $1, t) => {
        const schema = t.schema;
        if (!schema.out[p.name] || !schema.out[p.name].isNumeric())
            return null;
        if (!schema.is_list)
            return null;
        const newSchema = schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT || arg.name === p.name);
        newSchema.minimal_projection = [];
        if (newSchema instanceof thingtalk_1.Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new thingtalk_1.Ast.Value.Array([]);
        const agg = new thingtalk_1.Ast.AggregationExpression(null, t, p.name, 'sum', newSchema);
        return new thingtalk_1.Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", ["total", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)]), "of", new $runtime.NonTerminal("filtered_table", undefined)], (($0, p, $1, t) => {
        const schema = t.schema;
        if (!schema.out[p.name] || !schema.out[p.name].isNumeric())
            return null;
        if (!schema.is_list)
            return null;
        const newSchema = schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT || arg.name === p.name);
        newSchema.minimal_projection = [];
        if (newSchema instanceof thingtalk_1.Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new thingtalk_1.Ast.Value.Array([]);
        const agg = new thingtalk_1.Ast.AggregationExpression(null, t, p.name, 'sum', newSchema);
        return new thingtalk_1.Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", ["average", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)]), "of", new $runtime.NonTerminal("table", undefined)], (($0, p, $1, t) => {
        const schema = t.schema;
        if (!schema.out[p.name] || !schema.out[p.name].isNumeric())
            return null;
        if (!schema.is_list)
            return null;
        const newSchema = schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT || arg.name === p.name);
        newSchema.minimal_projection = [];
        if (newSchema instanceof thingtalk_1.Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new thingtalk_1.Ast.Value.Array([]);
        const agg = new thingtalk_1.Ast.AggregationExpression(null, t, p.name, 'avg', newSchema);
        return new thingtalk_1.Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
    }), C.expressionKeyFn, {});
    $grammar.addRule("filtered_table", ["average", new $runtime.NonTerminal("out_param_Any", ["is_numeric", (true)]), "of", new $runtime.NonTerminal("filtered_table", undefined)], (($0, p, $1, t) => {
        const schema = t.schema;
        if (!schema.out[p.name] || !schema.out[p.name].isNumeric())
            return null;
        if (!schema.is_list)
            return null;
        const newSchema = schema.filterArguments((arg) => arg.direction !== thingtalk_1.Ast.ArgDirection.OUT || arg.name === p.name);
        newSchema.minimal_projection = [];
        if (newSchema instanceof thingtalk_1.Ast.FunctionDef)
            newSchema.annotations.minimal_projection = new thingtalk_1.Ast.Value.Array([]);
        const agg = new thingtalk_1.Ast.AggregationExpression(null, t, p.name, 'avg', newSchema);
        return new thingtalk_1.Ast.ProjectionExpression(null, agg, [p.name], [], [], newSchema);
    }), C.expressionKeyFn, {});
    $grammar.declareSymbol("$root");
    $grammar.addRule("$root", [new $runtime.NonTerminal("table", undefined)], ((t) => {
        const proj = C.sayProjection($loader, t);
        if (proj)
            return C.makeProgram($loader, proj);
        return null;
    }), undefined, {});
    $grammar.addRule("$root", [new $runtime.NonTerminal("filtered_table", undefined)], ((t) => {
        const filtered = C.sayProjection($loader, t);
        if (filtered)
            return C.makeProgram($loader, filtered);
        return null;
    }), undefined, {});
}
exports.default = default_1;
;
//# sourceMappingURL=sempre.genie.js.map