"use strict";
// -*- mode: typescript; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2019-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 util = __importStar(require("util"));
const fs = __importStar(require("fs"));
const byline_1 = __importDefault(require("byline"));
const csv_parse_1 = __importDefault(require("csv-parse"));
const path = __importStar(require("path"));
// Load strings and entities from files
//
// Strings are loaded from the TSV files generated from
// https://almond.stanford.edu/thingpedia/strings/download/:x
//
// Entities are loaded from the JSON files returned from
// https://almond.stanford.edu/thingpedia/api/v3/entities/list/:x
class FileParameterProvider {
    constructor(filename, paramLocale) {
        this._filename = filename;
        this._paramLocale = paramLocale || 'en-US';
        this._dirname = path.dirname(filename);
        this._paths = new Map;
        this._loaded = false;
    }
    async open() {
        const file = fs.createReadStream(this._filename);
        file.setEncoding('utf8');
        const input = byline_1.default(file);
        input.on('data', (line) => {
            if (/^\s*(#|$)/.test(line))
                return;
            const [stringOrEntity, locale, type, filepath] = line.trim().split('\t');
            if (stringOrEntity !== 'string' && stringOrEntity !== 'entity')
                throw new Error(`Invalid syntax: ${line}`);
            if (locale === this._paramLocale)
                this._paths.set(stringOrEntity + '+' + type, path.resolve(this._dirname, filepath));
        });
        await new Promise((resolve, reject) => {
            input.on('end', resolve);
            input.on('error', reject);
        });
        this._loaded = true;
    }
    async close() {
    }
    async _getStrings(stringType) {
        const filepath = this._paths.get('string+' + stringType);
        if (!filepath)
            return [];
        const strings = [];
        const input = fs.createReadStream(filepath)
            .pipe(csv_parse_1.default({ delimiter: '\t', relax: true }));
        input.on('data', (line) => {
            const value = line[0];
            let preprocessed, weight;
            if (line.length === 1) {
                preprocessed = line[0];
                weight = 1.0;
            }
            else if (line.length === 2) {
                if (isFinite(+line[1])) {
                    preprocessed = line[0];
                    weight = line[1];
                }
                else {
                    preprocessed = line[1];
                    weight = 1.0;
                }
            }
            else {
                preprocessed = line[1];
                weight = parseFloat(line[2]) || 1.0;
            }
            if (!(weight > 0.0))
                weight = 1.0;
            strings.push({ value, preprocessed, weight });
        });
        return new Promise((resolve, reject) => {
            input.on('end', () => {
                if (strings.length === 0)
                    console.log('actually no values for', stringType, filepath);
                resolve(strings);
            });
            input.on('error', reject);
        });
    }
    async _getEntities(stringType) {
        return (await this.getEntity(stringType)).map((e) => {
            return { preprocessed: e.canonical, weight: 1.0, value: e.value, name: e.name };
        });
    }
    async getEntity(stringType) {
        if (!this._loaded)
            await this.open();
        const filepath = this._paths.get('entity+' + stringType);
        if (!filepath)
            return [];
        return JSON.parse(await util.promisify(fs.readFile)(filepath, { encoding: 'utf8' })).data;
    }
    async get(valueListType, valueListName) {
        if (!this._loaded)
            await this.open();
        switch (valueListType) {
            case 'string':
                return this._getStrings(valueListName);
            case 'entity':
                return this._getEntities(valueListName);
            default:
                throw new TypeError(`Unexpected value list type ${valueListType}`);
        }
    }
}
exports.default = FileParameterProvider;
//# sourceMappingURL=file_parameter_provider.js.map