"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoSerializeInterceptor = void 0;
const common_1 = require("@nestjs/common");
const operators_1 = require("rxjs/operators");
const mongoose_1 = require("mongoose");
let MongoSerializeInterceptor = class MongoSerializeInterceptor {
    intercept(context, next) {
        return next.handle().pipe((0, operators_1.map)(data => this.transformResponse(data)));
    }
    transformResponse(data, seen = new WeakSet()) {
        if (!data)
            return data;
        if (typeof data === 'object' && data !== null) {
            if (seen.has(data)) {
                return '[Circular]';
            }
            seen.add(data);
        }
        if (Array.isArray(data)) {
            return data.map(item => this.transformResponse(item, seen));
        }
        if (typeof data === 'object') {
            const transformed = {};
            for (const key in data) {
                if (Object.prototype.hasOwnProperty.call(data, key)) {
                    const value = data[key];
                    if (key === '$__' || key === '$isNew' || key === '_doc' || key === 'isNew') {
                        continue;
                    }
                    if (value instanceof mongoose_1.Types.ObjectId) {
                        transformed[key] = value.toString();
                        if (key === '_id') {
                            transformed.id = value.toString();
                        }
                    }
                    else if (value && typeof value === 'object' && value.constructor?.name === 'ObjectID') {
                        transformed[key] = value.toString();
                        if (key === '_id') {
                            transformed.id = value.toString();
                        }
                    }
                    else if (Array.isArray(value)) {
                        transformed[key] = this.transformResponse(value, seen);
                    }
                    else if (value && typeof value === 'object' && !(value instanceof Date)) {
                        transformed[key] = this.transformResponse(value, seen);
                    }
                    else {
                        transformed[key] = value;
                    }
                }
            }
            if (transformed._id && !transformed.id) {
                transformed.id = transformed._id;
            }
            return transformed;
        }
        return data;
    }
};
exports.MongoSerializeInterceptor = MongoSerializeInterceptor;
exports.MongoSerializeInterceptor = MongoSerializeInterceptor = __decorate([
    (0, common_1.Injectable)()
], MongoSerializeInterceptor);
//# sourceMappingURL=mongo-serialize.interceptor.js.map