"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const ts = __importStar(require("typescript"));
function detectNPlusOne(source) {
    const sourceFile = ts.createSourceFile('test.ts', source, ts.ScriptTarget.Latest, true);
    let count = 0;
    function visit(node) {
        if (ts.isCallExpression(node)) {
            const callText = node.expression.getText();
            const isQuery = callText.includes('.find') ||
                callText.includes('.query') ||
                callText.includes('.execute') ||
                callText.includes('fetch') ||
                callText.includes('prisma.') ||
                callText.includes('db.');
            if (isQuery) {
                let parent = node.parent;
                while (parent) {
                    if (ts.isForStatement(parent) ||
                        ts.isForInStatement(parent) ||
                        ts.isForOfStatement(parent) ||
                        ts.isWhileStatement(parent) ||
                        ts.isDoStatement(parent) ||
                        (ts.isCallExpression(parent) &&
                            ts.isPropertyAccessExpression(parent.expression) &&
                            (parent.expression.name.text === 'map' || parent.expression.name.text === 'forEach'))) {
                        count++;
                        break;
                    }
                    parent = parent.parent;
                }
            }
        }
        ts.forEachChild(node, visit);
    }
    visit(sourceFile);
    return count;
}
describe('N+1 query detection (Unit 1.3.2)', () => {
    test('db.find() inside for-of loop is detected', () => {
        const code = `
      for (const id of ids) {
        await db.users.find({ id });
      }
    `;
        expect(detectNPlusOne(code)).toBe(1);
    });
    test('db.find() inside forEach callback is detected', () => {
        const code = `
      ids.forEach(async (id) => {
        await db.users.find({ id });
      });
    `;
        expect(detectNPlusOne(code)).toBe(1);
    });
    test('db.find() outside any loop is NOT detected', () => {
        const code = `await db.users.find({ id: 1 });`;
        expect(detectNPlusOne(code)).toBe(0);
    });
    test('prisma.query inside while loop is detected', () => {
        const code = `
      let i = 0;
      while (i < 10) {
        await prisma.user.findMany();
        i++;
      }
    `;
        expect(detectNPlusOne(code)).toBe(1);
    });
    test('multiple query calls in same loop counted once each', () => {
        const code = `
      for (const x of arr) {
        await db.a.find();
        await db.b.find();
      }
    `;
        expect(detectNPlusOne(code)).toBe(2);
    });
});
//# sourceMappingURL=code-scanner-n1-detection.test.js.map