// @ts-nocheck
import { TaonMigration, TaonBaseMigration } from 'taon/lib-prod';
import { ___NS__range } from 'tnp-core/lib-prod';
import { Port, TaonPortsController } from '../base/tcp-udp-ports';
let TaonPortsContext_1736454437350_addFreePorts = class TaonPortsContext_1736454437350_addFreePorts extends TaonBaseMigration {
    portsController = this.injectController(TaonPortsController);
    async up(queryRunner) {
        const portsTableRepo = await queryRunner.manager.getRepository(Port);
        const addFreePortsInRange = ___NS__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 => Port.from({
            port: port,
            serviceId: 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(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);
        }
    }
};
TaonPortsContext_1736454437350_addFreePorts = __decorate([
    TaonMigration({
        className: 'TaonPortsContext_1736454437350_addFreePorts',
    })
], TaonPortsContext_1736454437350_addFreePorts);
export { TaonPortsContext_1736454437350_addFreePorts };
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/migrations/1736454437350_addFreePorts.js.map