"use strict";
// -*- 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>
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 path = __importStar(require("path"));
const fs_1 = require("fs");
const thingtalk_1 = require("thingtalk");
const seedrandom = __importStar(require("seedrandom"));
const btrie_1 = require("../utils/btrie");
const batch_1 = require("../sentence-generator/batch");
const exact_1 = __importDefault(require("./exact"));
let TEMPLATE_FILE_PATH;
try {
    // try the path relative to our build location first (in dist/lib/dialogue-agent)
    TEMPLATE_FILE_PATH = require.resolve('../../../languages-dist/thingtalk/en/basic.genie');
}
catch (e) {
    if (e.code !== 'MODULE_NOT_FOUND')
        throw e;
    try {
        // if that fails, try the location relative to our source directory
        // (in case we're running with ts-node)
        TEMPLATE_FILE_PATH = require.resolve('../../languages-dist/thingtalk/en/basic.genie');
    }
    catch (e) {
        if (e.code !== 'MODULE_NOT_FOUND')
            throw e;
        // if that still fails, we're probably in the "compile-template" call
        // in a clean build, so set ourselves empty (it will not matter)
        TEMPLATE_FILE_PATH = '';
    }
}
const SYNTHETIC_DEPTH = 8;
const TARGET_PRUNING_SIZE = 1000;
async function safeGetMTime(filename) {
    try {
        const stat = await fs_1.promises.stat(filename);
        return stat.mtimeMs;
    }
    catch (e) {
        if (e.code !== 'ENOENT' && e.code !== 'ENOTDIR')
            throw e;
        return 0;
    }
}
/**
 * Build an exact matcher for a set of devices in the developer directory.
 *
 */
class ExactMatchBuilder {
    constructor(options) {
        this._locale = options.locale;
        this._timezone = options.timezone;
        this._cachefile = path.resolve(options.cachedir, 'exact-' + options.locale + '.btrie');
        this._developerdir = options.developerdir;
        this._tpClient = options.thingpediaClient;
        this._rng = seedrandom.alea('almond is awesome');
        this._matcher = new exact_1.default();
    }
    async _getDeveloperDirMTime() {
        let mtime = 0;
        const devices = [];
        for (const dir of this._developerdir) {
            for (const device of await fs_1.promises.readdir(dir)) {
                const manifest = path.resolve(dir, device, 'manifest.tt');
                const dataset = path.resolve(dir, device, 'dataset.tt');
                const manifestmtime = await safeGetMTime(manifest);
                mtime = Math.max(mtime, manifestmtime);
                mtime = Math.max(mtime, await safeGetMTime(dataset));
                if (manifestmtime > 0)
                    devices.push(device);
            }
        }
        return [mtime, devices];
    }
    _generateExact(forDevices) {
        const schemas = new thingtalk_1.SchemaRetriever(this._tpClient, null, true);
        const generatorOptions = {
            contextual: false,
            rootSymbol: '$root',
            flags: {
                bookkeeping: true,
                unbalanced: true,
            },
            rng: this._rng,
            locale: this._locale,
            timezone: this._timezone,
            templateFiles: [TEMPLATE_FILE_PATH],
            thingpediaClient: this._tpClient,
            schemaRetriever: schemas,
            onlyDevices: forDevices,
            maxDepth: SYNTHETIC_DEPTH,
            maxConstants: 5,
            targetPruningSize: TARGET_PRUNING_SIZE,
            debug: 1,
        };
        return new batch_1.BasicSentenceGenerator(generatorOptions);
    }
    async load() {
        this._matcher.clear();
        const [devdirmtime, devices] = await this._getDeveloperDirMTime();
        const existing = await safeGetMTime(this._cachefile);
        if (existing >= devdirmtime) {
            await this._matcher.load(this._cachefile);
            return this._matcher;
        }
        if (devdirmtime === 0) {
            // nothing in the developer directory
            return this._matcher;
        }
        console.log('Cached exact matcher is missing or stale, regenerating...');
        const builder = new btrie_1.BTrieBuilder((existing, newValue) => {
            if (existing === undefined)
                return newValue;
            else
                return existing + '\0' + newValue;
        });
        const sentenceStream = this._generateExact(devices);
        const tmp = new exact_1.default();
        sentenceStream.on('data', (ex) => {
            tmp.add(ex.preprocessed.split(' '), String(ex.target_code).split(' '));
        });
        await new Promise((resolve, reject) => {
            sentenceStream.on('end', resolve);
            sentenceStream.on('error', reject);
        });
        for (const [key, value] of tmp)
            builder.insert(key, value);
        await fs_1.promises.writeFile(this._cachefile, builder.build());
        await this._matcher.load(this._cachefile);
        return this._matcher;
    }
}
exports.default = ExactMatchBuilder;
//# sourceMappingURL=exactbuilder.js.map