"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
const debug_1 = __importDefault(require("debug"));
const fs_extra_1 = require("fs-extra");
const handle_1 = require("./handle");
const log = debug_1.default('j2t:core:application');
/**
 *
 *Translation request for provider
 *
 *
 */
class Request {
    constructor(config) {
        this.interpretedFiles = [];
        this.typeInfo = new Map();
        log('Request init');
        this.config = config;
    }
    isRecorded(fileAbsPath) {
        return this.interpretedFiles.includes(fileAbsPath);
    }
    record(fileAbsPath) {
        this.interpretedFiles.push(fileAbsPath);
    }
    async work() {
        log('read jar config', this.config.jarInfo);
        this.jarInfo = await fs_extra_1.readJson(this.config.jarInfo);
        await this.interpret();
    }
    async interpret() {
        for (let providerPath of this.jarInfo.providers) {
            log('start transaction for provider::', providerPath);
            await new handle_1.IntepretHandle(providerPath, this).work();
        }
    }
    getAst(classPath) {
        if (this.jarInfo.classes[classPath]) {
            return this.jarInfo.classes[classPath];
        }
        else {
            throw new Error("Can't find class ast" + classPath);
        }
    }
    hasAst(classPath) {
        return !!this.jarInfo.classes[classPath];
    }
    get outputDir() {
        return this.config.output;
    }
    get providerSuffix() {
        return this.config.providerSuffix || "Provider";
    }
    registerTypeInfo(typeInfoItem) {
        let key = '';
        if (typeInfoItem.classPath) {
            key = typeInfoItem.classPath;
        }
        if (this.typeInfo.has(key)) {
            log('update class typeInfo:%o', typeInfoItem);
            this.typeInfo.set(key, Object.assign({}, this.typeInfo.get(key), typeInfoItem));
        }
        else {
            log('register one class typeInfo:%o', typeInfoItem);
            this.typeInfo.set(key, typeInfoItem);
        }
    }
    getTypeInfo(classPath) {
        return this.typeInfo.get(classPath);
    }
}
exports.Request = Request;
