"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 });
exports.TestAnticipator = void 0;
const fs = __importStar(require("fs"));
class TestAnticipator {
    analyzeFunction(code) {
        const funcMatch = code.match(/(?:function|const)\s+(\w+)/);
        const name = funcMatch ? funcMatch[1] : 'unknown';
        const paramsMatch = code.match(/\(([^)]*)\)/);
        const params = paramsMatch
            ? paramsMatch[1].split(',').map((p) => p.trim()).filter(Boolean)
            : [];
        const returnMatch = code.match(/:\s*(\w+(?:\[\])?)\s*[{=]/);
        const returnType = returnMatch ? returnMatch[1] : 'unknown';
        const isAsync = code.includes('async') || code.includes('await');
        return { name, params, returnType, isAsync };
    }
    generateTests(code, framework = 'vitest') {
        const func = this.analyzeFunction(code);
        const tests = [];
        const importLine = framework === 'vitest'
            ? "import { describe, it, expect } from 'vitest';"
            : '';
        const prefix = importLine ? `${importLine}\n\n` : '';
        tests.push({
            name: `should return correct ${func.returnType}`,
            type: 'happy',
            code: this.formatTest(func.name, 'returns expected value', 'const result = TODO; expect(result).toBeDefined();', framework, prefix),
        });
        if (func.params.length > 0) {
            tests.push({
                name: `should handle missing ${func.params[0]}`,
                type: 'error',
                code: this.formatTest(func.name, 'handles missing required param', `expect(() => ${func.name}()).toThrow();`, framework, prefix),
            });
            tests.push({
                name: `should handle null ${func.params[0]}`,
                type: 'edge',
                code: this.formatTest(func.name, 'handles null input', `const result = ${func.name}(null); expect(result).toBeNull();`, framework, prefix),
            });
        }
        if (func.isAsync) {
            tests.push({
                name: 'should handle promise rejection',
                type: 'error',
                code: this.formatTest(func.name, 'handles async error', `await expect(${func.name}()).rejects.toThrow();`, framework, prefix),
            });
        }
        if (func.returnType === 'string') {
            tests.push({
                name: 'should return empty string for no input',
                type: 'edge',
                code: this.formatTest(func.name, 'returns empty string', `const result = ${func.name}(''); expect(result).toBe('');`, framework, prefix),
            });
        }
        if (func.returnType.includes('[]') || func.returnType === 'Array') {
            tests.push({
                name: 'should return empty array for no results',
                type: 'edge',
                code: this.formatTest(func.name, 'returns empty array', `const result = ${func.name}(); expect(result).toEqual([]);`, framework, prefix),
            });
        }
        return tests;
    }
    getTestFrameworkFromConfig(configPath) {
        if (configPath && fs.existsSync(configPath)) {
            const content = fs.readFileSync(configPath, 'utf-8');
            if (content.includes('vitest'))
                return 'vitest';
            if (content.includes('jest'))
                return 'jest';
        }
        return 'vitest';
    }
    formatTest(funcName, description, body, framework, prefix = '') {
        return `${prefix}describe('${funcName}', () => {\n  it('${description}', () => {\n    ${body}\n  });\n});`;
    }
}
exports.TestAnticipator = TestAnticipator;
//# sourceMappingURL=test-anticipator.js.map