"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateInheritanceReference = exports.generateTypeReference = exports.generateTypePath = void 0;
const tslib_1 = require("tslib");
const typescript_1 = tslib_1.__importStar(require("typescript"));
// eslint-disable-next-line import/no-cycle
const StructHelpers_1 = require("./StructHelpers");
const TypeMappings_1 = require("../TypeMappings");
const Utilities_1 = require("../../../Utilities");
function generateTypePath(type, factoryFn, context, relativeTo) {
    const nameParts = context.types.getTypeName(type, context, relativeTo).split('.');
    let cursor = typescript_1.factory.createIdentifier(nameParts.splice(0, 1)[0]);
    while (nameParts.length > 0) {
        cursor = factoryFn(cursor, typescript_1.factory.createIdentifier(Utilities_1.fixName(nameParts.splice(0, 1)[0])));
    }
    return cursor;
}
exports.generateTypePath = generateTypePath;
function generateTypeReference(type, context, relativeTo) {
    var _a;
    if (type.IsPrimitive) {
        return typescript_1.factory.createTypeReferenceNode(TypeMappings_1.getTypeMapping(type.TypeName));
    }
    const returnTypeType = StructHelpers_1.findKnownType(type, context);
    const returnTypeName = returnTypeType === null || returnTypeType === void 0 ? void 0 : returnTypeType.boxedValue;
    if (returnTypeName) {
        return typescript_1.factory.createTypeReferenceNode(returnTypeName);
    }
    const typeArguments = ((_a = type.TypeArguments) === null || _a === void 0 ? void 0 : _a.length)
        ? type.TypeArguments.map((argType) => generateTypeReference(argType, context, relativeTo))
        : undefined;
    return typescript_1.factory.createTypeReferenceNode(generateTypePath(type, typescript_1.factory.createQualifiedName, context, relativeTo), typeArguments);
}
exports.generateTypeReference = generateTypeReference;
function generateInheritanceReference(type, context) {
    var _a;
    const typeArguments = ((_a = type.TypeArguments) === null || _a === void 0 ? void 0 : _a.length)
        ? type.TypeArguments.map((argType) => generateTypeReference(argType, context, type))
        : undefined;
    return typescript_1.factory.createHeritageClause(typescript_1.default.SyntaxKind.ExtendsKeyword, [
        typescript_1.factory.createExpressionWithTypeArguments(generateTypePath(type, typescript_1.factory.createPropertyAccessExpression, context), typeArguments),
    ]);
}
exports.generateInheritanceReference = generateInheritanceReference;
//# sourceMappingURL=TypeNameHelpers.js.map