"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 to_enum_1 = require("./bean/to-enum");
const to_vo_1 = require("./bean/to-vo");
const to_interface_1 = require("./provider/to-interface");
const to_proxy_function_1 = require("./provider/to-proxy-function");
const to_wrapper_class_1 = require("./provider/to-wrapper-class");
const log = debug_1.default('j2t:core:toTypewcript');
/**
 * java 类型转换为typescript type-ast
 * @param astJava
 * @param {SourceFile} sourceFile
 * @returns {SourceFile}
 */
async function toTypescript(intepretHandle) {
    log('调用转换方法 toTypescript::', intepretHandle.classPath);
    let { sourceFile, astJava } = intepretHandle;
    let lastPointIndex = astJava.name.lastIndexOf('.') + 1;
    let typeInfo = {
        classPath: astJava.name,
        packagePath: astJava.name.substring(0, lastPointIndex),
        className: astJava.name.substring(lastPointIndex),
        isEnum: astJava.isEnum,
        isAbstract: astJava.isAbstract,
        isInterface: astJava.isInterface,
        isClass: !astJava.isEnum && !astJava.isInterface,
        isProvider: astJava.name.endsWith(String(intepretHandle.providerSuffix) || 'Provider'),
    };
    intepretHandle.request.registerTypeInfo(typeInfo);
    if (astJava.isAbstract && !typeInfo.isProvider) {
        console.warn('warning 抽象类型要注意了.classPath:', typeInfo.classPath);
    }
    try {
        if (astJava.isEnum) {
            sourceFile.addEnum(to_enum_1.toEnum(astJava.name, astJava, intepretHandle));
        }
        else {
            if (typeInfo.isProvider) {
                sourceFile.addInterface(await to_interface_1.toInterface(astJava, intepretHandle));
                sourceFile.addVariableStatement(to_wrapper_class_1.toWrapperClass(astJava, intepretHandle));
                sourceFile.addImport({
                    moduleSpecifier: 'dubbo2.js',
                    defaultImport: '{TDubboCallResult,Dubbo}',
                });
                sourceFile.addFunction(to_proxy_function_1.toProxyFunc({
                    typeName: intepretHandle.classPath.substring(intepretHandle.classPath.lastIndexOf('.') + 1),
                    typePath: intepretHandle.classPath,
                }));
            }
            else {
                sourceFile.addClass(await to_vo_1.toBeanClass(astJava, intepretHandle));
                sourceFile.addImport({
                    moduleSpecifier: 'js-to-java',
                    defaultImport: 'java',
                });
            }
        }
    }
    catch (err) {
        console.error(`为${intepretHandle.classPath},${JSON.stringify(typeInfo)} 添加内容出错,`, err);
    }
    return sourceFile;
}
exports.toTypescript = toTypescript;
