"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const utils_1 = require("../../../../../src/utils");
const fs_1 = require("fs");
const compiler_1 = require("svelte/compiler");
const DiagnosticsProvider_1 = require("../../../../../src/plugins/typescript-go/features/DiagnosticsProvider");
const mocha_1 = require("mocha");
const test_utils_1 = require("../../../typescript/test-utils");
const documents_1 = require("../../../../../src/lib/documents");
const typescript_1 = __importDefault(require("typescript"));
const importPackage_1 = require("../../../../../src/importPackage");
const assert_1 = __importDefault(require("assert"));
const isSvelte5Plus = Number(compiler_1.VERSION.split('.')[0]) >= 5;
const root = path_1.default.join(__dirname, '../../../typescript/features/diagnostics');
const { version: { major } } = (0, importPackage_1.getPackageInfo)('svelte', __dirname);
const expected = 'expectedv2.json';
const newSvelteMajorExpected = `expected_svelte_${major}.json`;
const expectedTsGo = 'expected_tsgo.json';
const newSvelteMajorExpectedTsGo = `expected_tsgo_svelte_${major}.json`;
const skips = [
    // Has to do with project assignment so not related to svelte-check --tsconfig support.
    '/fixtures/project-reference/nested',
    '/fixtures/project-reference/paths'
];
const providerPool = new Map();
(0, mocha_1.describe)('SvelteCheckTSGoDiagnosticsProvider', function () {
    const fixturesDir = path_1.default.join(root, 'fixtures');
    executeTests({
        dir: fixturesDir,
        workspaceDir: fixturesDir
    });
    after(() => {
        for (const provider of providerPool.values()) {
            provider.dispose();
        }
        providerPool.clear();
    });
});
function executeTests(testOptions) {
    const { dir } = testOptions;
    const inputFile = path_1.default.join(dir, 'input.svelte');
    const getService = setupService(testOptions.dir);
    if ((0, fs_1.existsSync)(inputFile)) {
        const name = dir.substring(root.length);
        const _it = (dir.endsWith('.v5') && !isSvelte5Plus) || skips.includes((0, utils_1.normalizePath)(name))
            ? it.skip
            : dir.endsWith('.only')
                ? it.only
                : it;
        _it(name, async () => {
            const service = getService(testOptions.dir);
            await executeTest(inputFile, testOptions, service);
        });
    }
    else {
        const _describe = dir.endsWith('.only') ? mocha_1.describe.only : mocha_1.describe;
        _describe(dir.substring(root.length), function () {
            const subDirs = (0, fs_1.readdirSync)(dir);
            for (const subDir of subDirs) {
                const stat = (0, fs_1.statSync)(path_1.default.join(dir, subDir));
                if (stat.isDirectory()) {
                    executeTests({
                        ...testOptions,
                        dir: path_1.default.join(dir, subDir)
                    });
                }
            }
        });
    }
}
async function executeTest(inputFile, testOptions, plugin) {
    const { dir } = testOptions;
    const document = new documents_1.Document((0, utils_1.pathToUrl)(inputFile), typescript_1.default.sys.readFile(inputFile) || '');
    const diagnostics = await plugin.getDiagnostics(document);
    const defaultExpectedFile = path_1.default.join(dir, expected);
    const expectedFileVariants = [
        path_1.default.join(dir, newSvelteMajorExpectedTsGo),
        path_1.default.join(dir, newSvelteMajorExpected),
        path_1.default.join(dir, expectedTsGo)
    ];
    const expectedFile = expectedFileVariants.find(fs_1.existsSync) ?? defaultExpectedFile;
    const snapshotFormatter = await (0, test_utils_1.createJsonSnapshotFormatter)(dir);
    await (0, test_utils_1.updateSnapshotIfFailedOrEmpty)({
        assertion() {
            assert_1.default.deepStrictEqual(diagnostics, JSON.parse((0, fs_1.readFileSync)(expectedFile, 'utf-8')));
        },
        expectedFile,
        getFileContent() {
            return snapshotFormatter(diagnostics);
        },
        rootDir: __dirname
    });
}
function setupService(dir) {
    before(async () => {
        const tsconfig = typescript_1.default.findConfigFile(dir, typescript_1.default.sys.fileExists, 'tsconfig.json');
        if (providerPool.has(tsconfig)) {
            return;
        }
        const syncApi = await import('@typescript/native-preview/unstable/sync');
        const tsAst = await import('@typescript/native-preview/unstable/ast');
        const service = new DiagnosticsProvider_1.SvelteCheckTSGoDiagnosticsProvider(syncApi, tsAst, tsconfig, 'svelte2tsx', (filePath, content) => new documents_1.Document((0, utils_1.pathToUrl)(filePath), content));
        providerPool.set(tsconfig, service);
    });
    return getService;
    function getService(dir) {
        const tsconfig = typescript_1.default.findConfigFile(dir, typescript_1.default.sys.fileExists, 'tsconfig.json');
        const service = providerPool.get(tsconfig);
        if (!service) {
            throw new Error(`Service for ${tsconfig} not found`);
        }
        return service;
    }
}
//# sourceMappingURL=index.test.js.map