"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert = __importStar(require("assert"));
const sinon_1 = __importDefault(require("sinon"));
const typescript_1 = __importDefault(require("typescript"));
const svS = __importStar(require("../../../src/plugins/typescript/svelte-sys"));
const module_loader_1 = require("../../../src/plugins/typescript/module-loader");
describe('createSvelteModuleLoader', () => {
    afterEach(() => {
        sinon_1.default.restore();
    });
    function setup(resolvedModule) {
        const getSvelteSnapshotStub = sinon_1.default
            .stub()
            .returns({ scriptKind: typescript_1.default.ScriptKind.JSX });
        const resolveStub = sinon_1.default.stub().returns({
            resolvedModule
        });
        const moduleCacheMock = {
            getPackageJsonInfoCache: () => ({})
        };
        const moduleResolutionHost = { ...typescript_1.default.sys };
        const svelteSys = {
            ...svS.createSvelteSys(typescript_1.default.sys)
        };
        sinon_1.default.stub(svS, 'createSvelteSys').returns(svelteSys);
        const compilerOptions = { strict: true, paths: { '/@/*': [] } };
        const moduleResolver = (0, module_loader_1.createSvelteModuleLoader)(getSvelteSnapshotStub, compilerOptions, typescript_1.default.sys, {
            ...typescript_1.default,
            createModuleResolutionCache: () => moduleCacheMock,
            resolveModuleName: resolveStub
        }, () => moduleResolutionHost);
        return {
            getSvelteSnapshotStub,
            moduleCacheMock: moduleCacheMock,
            resolveStub,
            compilerOptions,
            moduleResolver,
            svelteSys,
            moduleResolutionHost
        };
    }
    function lastCall(stub) {
        return stub.getCall(stub.getCalls().length - 1);
    }
    it('uses svelte script kind if resolved module is svelte file', async () => {
        const resolvedModule = {
            extension: typescript_1.default.Extension.Ts,
            resolvedFileName: 'filename.d.svelte.ts'
        };
        const { getSvelteSnapshotStub, moduleResolver, svelteSys } = setup(resolvedModule);
        svelteSys.getRealSveltePathIfExists = (filename) => filename === 'filename.d.svelte.ts' ? 'filename.svelte' : filename;
        const result = moduleResolver.resolveModuleNames(['./normal.ts'], 'C:/somerepo/somefile.svelte', undefined, undefined, undefined);
        assert.deepStrictEqual(result, [
            {
                extension: typescript_1.default.Extension.Jsx,
                resolvedFileName: 'filename.svelte',
                isExternalLibraryImport: undefined
            }
        ]);
        assert.deepStrictEqual(lastCall(getSvelteSnapshotStub).args, ['filename.svelte']);
    });
    it('uses cache if module was already resolved before', async () => {
        const resolvedModule = {
            extension: typescript_1.default.Extension.Ts,
            resolvedFileName: 'filename.ts'
        };
        const { resolveStub, moduleResolver } = setup(resolvedModule);
        // first call
        moduleResolver.resolveModuleNames(['./normal.ts'], 'C:/somerepo/somefile.svelte', undefined, undefined, undefined);
        // second call, which should be from cache
        const result = moduleResolver.resolveModuleNames(['./normal.ts'], 'C:/somerepo/somefile.svelte', undefined, undefined, undefined);
        assert.deepStrictEqual(result, [resolvedModule]);
        assert.deepStrictEqual(resolveStub.callCount, 1);
    });
});
//# sourceMappingURL=module-loader.test.js.map