"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 __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startLanguageServer = exports.createConnection = void 0;
const fs = require("fs");
const request_light_1 = require("request-light");
const html = require("vscode-html-languageservice");
const vscode = require("vscode-languageserver/node");
const server_1 = require("../common/server");
const file_1 = require("../common/schemaRequestHandlers/file");
const http_1 = require("../common/schemaRequestHandlers/http");
const fileSystem_1 = require("./fileSystem");
const vscode_uri_1 = require("vscode-uri");
__exportStar(require("../index"), exports);
function createConnection() {
    return vscode.createConnection(vscode.ProposedFeatures.all);
}
exports.createConnection = createConnection;
function startLanguageServer(connection, ...plugins) {
    const uriToFileName = (uri) => vscode_uri_1.URI.parse(uri).fsPath.replace(/\\/g, '/');
    const fileNameToUri = (fileName) => vscode_uri_1.URI.file(fileName).toString();
    (0, server_1.startCommonLanguageServer)(connection, () => {
        return {
            plugins,
            connection,
            runtimeEnv: {
                uriToFileName,
                fileNameToUri,
                timer: {
                    setImmediate(callback, ...args) {
                        const handle = setImmediate(callback, ...args);
                        return { dispose: () => clearImmediate(handle) };
                    },
                },
                loadTypescript(tsdk) {
                    for (const name of ['./typescript.js', './tsserverlibrary.js']) {
                        try {
                            return require(require.resolve(name, { paths: [tsdk] }));
                        }
                        catch { }
                    }
                    // for bun
                    for (const name of ['typescript.js', 'tsserverlibrary.js']) {
                        try {
                            return require(tsdk + '/' + name);
                        }
                        catch { }
                    }
                    throw new Error(`Can't find typescript.js or tsserverlibrary.js in ${tsdk}`);
                },
                async loadTypescriptLocalized(tsdk, locale) {
                    try {
                        const path = require.resolve(`./${locale}/diagnosticMessages.generated.json`, { paths: [tsdk] });
                        return require(path);
                    }
                    catch { }
                },
                schemaRequestHandlers: {
                    file: file_1.default,
                    http: http_1.default,
                    https: http_1.default,
                },
                onDidChangeConfiguration(settings) {
                    (0, request_light_1.configure)(settings.http && settings.http.proxy, settings.http && settings.http.proxyStrictSSL);
                },
                createFileSystemHost: fileSystem_1.createNodeFileSystemHost,
                fileSystemProvide: {
                    stat: (uri) => {
                        return new Promise((resolve, reject) => {
                            fs.stat(uriToFileName(uri), (err, stats) => {
                                if (stats) {
                                    resolve({
                                        type: stats.isFile() ? html.FileType.File
                                            : stats.isDirectory() ? html.FileType.Directory
                                                : stats.isSymbolicLink() ? html.FileType.SymbolicLink
                                                    : html.FileType.Unknown,
                                        ctime: stats.ctimeMs,
                                        mtime: stats.mtimeMs,
                                        size: stats.size,
                                    });
                                }
                                else {
                                    reject(err);
                                }
                            });
                        });
                    },
                    readDirectory: (uri) => {
                        return new Promise((resolve, reject) => {
                            fs.readdir(uriToFileName(uri), (err, files) => {
                                if (files) {
                                    resolve(files.map(file => [file, html.FileType.File]));
                                }
                                else {
                                    reject(err);
                                }
                            });
                        });
                    },
                },
            },
        };
    });
}
exports.startLanguageServer = startLanguageServer;
//# sourceMappingURL=index.js.map