"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toJSON = toJSON;
function toJSON(doc) {
    if (!doc)
        return doc;
    if (Array.isArray(doc)) {
        return doc.map(item => toJSON(item));
    }
    const obj = {};
    for (const key in doc) {
        if (Object.prototype.hasOwnProperty.call(doc, key)) {
            obj[key] = doc[key];
        }
    }
    if (obj._id) {
        const idStr = typeof obj._id === 'string' ? obj._id : obj._id.toString();
        obj._id = idStr;
        obj.id = idStr;
    }
    const refFields = ['classId', 'sectionId', 'teacherId', 'studentId', 'academicYearId', 'assignedTeacherId'];
    for (const field of refFields) {
        if (obj[field]) {
            if (typeof obj[field] === 'string') {
                continue;
            }
            else if (typeof obj[field] === 'object' && obj[field].toString) {
                obj[field] = obj[field].toString();
            }
        }
    }
    return obj;
}
//# sourceMappingURL=mongo-serializer.js.map