"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeVisitor = exports.executeMethodVisitor = void 0;
/* eslint-disable no-param-reassign */
const core_1 = require("@il2js/core");
function executeMethodVisitor(visitor, methods) {
    if (!visitor.visitStaticMethod) {
        return methods;
    }
    const result = {};
    for (const [typeName, methodInfos] of Object.entries(methods)) {
        const visitorResult = core_1.excludeUndefined(methodInfos.map((methodInfo) => visitor.visitStaticMethod(methodInfo, typeName)));
        if (visitorResult && visitorResult.length) {
            result[typeName] = visitorResult;
        }
    }
    return result;
}
exports.executeMethodVisitor = executeMethodVisitor;
function executeVisitor(visitor, types) {
    if (visitor.visitTypeDef) {
        types = core_1.excludeUndefined(types.map(visitor.visitTypeDef));
    }
    for (const type of types) {
        if (visitor.visitTypeDef) {
            if (type.NestedTypes) {
                type.NestedTypes = executeVisitor(visitor, type.NestedTypes);
            }
        }
        if (visitor.visitField) {
            if (type.Fields) {
                type.Fields = core_1.excludeUndefined(type.Fields.map(visitor.visitField));
            }
            if (type.StaticFields) {
                type.StaticFields = core_1.excludeUndefined(type.StaticFields.map(visitor.visitField));
            }
        }
        if (visitor.visitTypeUsage) {
            if (type.Type.TypeArguments) {
                type.Type.TypeArguments = core_1.excludeUndefined(type.Type.TypeArguments.map(visitor.visitTypeUsage));
            }
            if (type.Type.BaseType) {
                type.Type.BaseType = visitor.visitTypeUsage(type.Type.BaseType);
            }
            if (type.Fields) {
                type.Fields = core_1.excludeUndefined(type.Fields.map((field) => {
                    const fieldType = visitor.visitTypeUsage(field.Type);
                    if (fieldType) {
                        field.Type = fieldType;
                        return field;
                    }
                    return undefined;
                }));
            }
            if (type.StaticFields) {
                type.StaticFields = core_1.excludeUndefined(type.StaticFields.map((field) => {
                    const fieldType = visitor.visitTypeUsage(field.Type);
                    if (fieldType) {
                        field.Type = fieldType;
                        return field;
                    }
                    return undefined;
                }));
            }
        }
    }
    return types;
}
exports.executeVisitor = executeVisitor;
//# sourceMappingURL=TypeVisitorExecutor.js.map