"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = __importDefault(require("debug"));
const to_field_1 = require("../bean/to-field");
const to_method_1 = require("../to-method");
const log = debug_1.default('j2t:core:toInterface');
async function toInterface(typeDef, intepretHandle) {
    log('调用转换方法 toInterface::');
    let methods = [];
    let properties = [];
    for (let fieldName in typeDef.fields) {
        properties.push(await to_field_1.toField(fieldName, typeDef.fields[fieldName], intepretHandle));
    }
    let filtersMethodNames = genePropsGetSet(properties.map(({ name }) => name));
    log('添加过滤方法:: %j', filtersMethodNames);
    //add extra javaType declare;
    let extraImport = ['argumentMap'];
    for (let methodName in typeDef.methods) {
        if (filtersMethodNames.includes(methodName)) {
            continue;
        }
        let _methodName = methodName;
        if (typeDef.methods[methodName].isOverride) {
            console.log('文件是override类型::', typeDef.name, methodName);
            _methodName = methodName.substring(0, methodName.lastIndexOf('@override'));
        }
        let methodItem = await to_method_1.toMethod(_methodName, typeDef.methods[methodName], intepretHandle);
        //如果是基本类型, 生成typescript的类型与js-to-java类型相对应 javaXXX;
        let methodDef = typeDef.methods[methodName];
        for (var i = 0, iLen = methodDef.params.length; i < iLen; i++) {
            var paramItem = methodDef.params[i];
            if (paramItem.isArray) {
                if (TypeMap[paramItem.elementType.name]) {
                    methodItem.parameters[i].type =
                        TypeMap[paramItem.elementType.name] + '[]';
                    if (!extraImport.includes(TypeMap[paramItem.elementType.name])) {
                        extraImport.push(TypeMap[paramItem.elementType.name]);
                    }
                }
            }
            else {
                if (TypeMap[paramItem.name]) {
                    methodItem.parameters[i].type = TypeMap[paramItem.name];
                    if (!extraImport.includes(TypeMap[paramItem.name])) {
                        extraImport.push(TypeMap[paramItem.name]);
                    }
                }
            }
        }
        methods.push(methodItem);
    }
    intepretHandle.sourceFile.addImport({
        moduleSpecifier: 'interpret-util',
        defaultImport: `{${extraImport.join(',')}}`,
    });
    log('转换 名称::%s 属性 :%j  方法:%j', typeDef.name, properties, methods);
    return {
        name: 'I' + intepretHandle.getTypeInfo(typeDef.name).className,
        isExported: true,
        methods,
        properties,
    };
}
exports.toInterface = toInterface;
function genePropsGetSet(propsNames) {
    let filtersMethodNames = [];
    propsNames.forEach(name => {
        if (name.startsWith('is')) {
            filtersMethodNames.push(name);
            filtersMethodNames.push(name.replace('is', 'set'));
        }
        else {
            let _methodName = name.charAt(0).toUpperCase() + name.slice(1);
            filtersMethodNames.push('set' + _methodName);
            filtersMethodNames.push('get' + _methodName);
        }
    });
    return filtersMethodNames;
}
exports.genePropsGetSet = genePropsGetSet;
const TypeMap = {
    'java.lang.Boolean': 'JavaBoolean',
    boolean: 'JavaBoolean',
    'java.lang.Integer': 'JavaInteger',
    int: 'Javaint',
    short: 'Javashort',
    'java.lang.Short': 'JavaShort',
    byte: 'Javabyte',
    'java.lang.Byte': 'JavaByte',
    long: 'Javalong',
    'java.lang.Long': 'JavaLong',
    double: 'Javadouble',
    'java.lang.Double': 'JavaDouble',
    float: 'Javafloat',
    'java.lang.Float': 'JavaFloat',
    'java.lang.String': 'JavaString',
    char: 'Javachar',
    'java.lang.Character': 'bbbbb',
    'java.util.List': 'JavaList',
    'java.util.Set': 'JavaSet',
    'java.util.HashMap': 'JavaHashMap',
    'java.util.Map': 'JavaMap',
};
