"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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IpWhitelistController = void 0;
const common_1 = require("@nestjs/common");
const current_user_decorator_1 = require("../../common/decorators/current-user.decorator");
const zod_validation_pipe_1 = require("../../common/pipes/zod-validation.pipe");
const jwt_guard_1 = require("../owner/guards/jwt.guard");
const ip_whitelist_dto_1 = require("./ip-whitelist.dto");
const ip_whitelist_service_1 = require("./ip-whitelist.service");
let IpWhitelistController = class IpWhitelistController {
    constructor(service) {
        this.service = service;
    }
    async detectClientIp(req, ip, res) {
        const forwarded = req.headers['x-forwarded-for'];
        let clientIp = req.ip || ip || '127.0.0.1';
        if (typeof forwarded === 'string') {
            clientIp = forwarded.split(',')[0].trim();
        }
        else if (Array.isArray(forwarded) && forwarded.length > 0) {
            clientIp = forwarded[0].trim();
        }
        const normalized = this.service.normalizeIp(clientIp);
        return res.status(common_1.HttpStatus.OK).json({
            status: common_1.HttpStatus.OK,
            message: 'Client IP detected successfully',
            data: {
                client_ip: normalized,
                raw_ip: clientIp,
                is_local: normalized === '127.0.0.1' ||
                    normalized === 'localhost' ||
                    normalized.startsWith('192.168.') ||
                    normalized.startsWith('10.') ||
                    normalized.startsWith('172.16.'),
            },
        });
    }
    async getAll(owner_id, res) {
        const result = await this.service.getAll(owner_id);
        return res.status(common_1.HttpStatus.OK).json(result);
    }
    async create(body, owner_id, res) {
        const result = await this.service.create(body, owner_id);
        return res.status(common_1.HttpStatus.CREATED).json(result);
    }
    async update(id, body, owner_id, res) {
        const result = await this.service.update(id, body, owner_id);
        return res.status(common_1.HttpStatus.OK).json(result);
    }
    async delete(id, owner_id, res) {
        const result = await this.service.delete(id, owner_id);
        return res.status(common_1.HttpStatus.OK).json(result);
    }
};
exports.IpWhitelistController = IpWhitelistController;
__decorate([
    (0, common_1.Get)('detect-ip'),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Ip)()),
    __param(2, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, Object]),
    __metadata("design:returntype", Promise)
], IpWhitelistController.prototype, "detectClientIp", null);
__decorate([
    (0, common_1.Get)(),
    __param(0, (0, current_user_decorator_1.CurrentUser)()),
    __param(1, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], IpWhitelistController.prototype, "getAll", null);
__decorate([
    (0, common_1.Post)(),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(ip_whitelist_dto_1.createIpWhitelistSchema)),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, Object]),
    __metadata("design:returntype", Promise)
], IpWhitelistController.prototype, "create", null);
__decorate([
    (0, common_1.Patch)(':id'),
    (0, common_1.UsePipes)(new zod_validation_pipe_1.ZodValidationPipe(ip_whitelist_dto_1.updateIpWhitelistSchema)),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, current_user_decorator_1.CurrentUser)()),
    __param(3, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, String, Object]),
    __metadata("design:returntype", Promise)
], IpWhitelistController.prototype, "update", null);
__decorate([
    (0, common_1.Delete)(':id'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __param(2, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object]),
    __metadata("design:returntype", Promise)
], IpWhitelistController.prototype, "delete", null);
exports.IpWhitelistController = IpWhitelistController = __decorate([
    (0, common_1.Controller)('ip-whitelist'),
    (0, common_1.UseGuards)(jwt_guard_1.JwtAuthGuard),
    __metadata("design:paramtypes", [ip_whitelist_service_1.IpWhitelistService])
], IpWhitelistController);
//# sourceMappingURL=ip-whitelist.controller.js.map