"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.TaonPortsContext_1736454437350_addFreePorts = void 0;
// @ts-nocheck
const lib_1 = require("taon/lib");
const lib_2 = require("tnp-core/lib");
const tcp_udp_ports_1 = require("../base/tcp-udp-ports");
let TaonPortsContext_1736454437350_addFreePorts = class TaonPortsContext_1736454437350_addFreePorts extends lib_1.TaonBaseMigration {
    portsController = this.injectController(tcp_udp_ports_1.TaonPortsController);
    async up(queryRunner) {
        const portsTableRepo = await queryRunner.manager.getRepository(tcp_udp_ports_1.Port);
        const addFreePortsInRange = lib_2._.range(this.portsController.START_PORT, this.portsController.END_PORT + 1);
        const allTakeByOsPort = (await portsTableRepo.find({
            where: {
                status: 'assigned-taken-by-os',
            },
        })).map(f => f.port);
        const freePortsInRange = addFreePortsInRange.map(port => tcp_udp_ports_1.Port.from({
            port: port,
            serviceId: tcp_udp_ports_1.Port.getTitleForFreePort(port),
            status: 'unassigned',
        })).filter(f => !allTakeByOsPort.includes(f.port));
        await portsTableRepo.save(freePortsInRange);
        for (const port of freePortsInRange) {
            this.portsController.portsCache.set(port.serviceId, port);
        }
    }
    async down(queryRunner) {
        // revert this "something" in db
        const db = await queryRunner.manager.getRepository(tcp_udp_ports_1.Port);
        await db.remove(await db.find({
            where: {
                status: 'unassigned',
            },
        }));
        const allPorts = Array.from(this.portsController.portsCache.values()).filter(f => f.status === 'unassigned');
        for (const port of allPorts) {
            this.portsController.portsCache.delete(port.serviceId);
        }
    }
};
exports.TaonPortsContext_1736454437350_addFreePorts = TaonPortsContext_1736454437350_addFreePorts;
exports.TaonPortsContext_1736454437350_addFreePorts = TaonPortsContext_1736454437350_addFreePorts = __decorate([
    (0, lib_1.TaonMigration)({
        className: 'TaonPortsContext_1736454437350_addFreePorts',
    })
], TaonPortsContext_1736454437350_addFreePorts);
//# sourceMappingURL=1736454437350_addFreePorts.js.map