"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const platform = require("./platform.js");
const uri = require("./uri.js");
exports.Schemas = void 0;
(function(Schemas) {
  Schemas.inMemory = "inmemory";
  Schemas.vscode = "vscode";
  Schemas.internal = "private";
  Schemas.walkThrough = "walkThrough";
  Schemas.walkThroughSnippet = "walkThroughSnippet";
  Schemas.http = "http";
  Schemas.https = "https";
  Schemas.file = "file";
  Schemas.mailto = "mailto";
  Schemas.untitled = "untitled";
  Schemas.data = "data";
  Schemas.command = "command";
  Schemas.vscodeRemote = "vscode-remote";
  Schemas.vscodeRemoteResource = "vscode-remote-resource";
  Schemas.userData = "vscode-userdata";
  Schemas.vscodeCustomEditor = "vscode-custom-editor";
  Schemas.vscodeNotebook = "vscode-notebook";
  Schemas.vscodeNotebookCell = "vscode-notebook-cell";
  Schemas.vscodeNotebookCellMetadata = "vscode-notebook-cell-metadata";
  Schemas.vscodeNotebookCellOutput = "vscode-notebook-cell-output";
  Schemas.vscodeInteractive = "vscode-interactive";
  Schemas.vscodeInteractiveInput = "vscode-interactive-input";
  Schemas.vscodeSettings = "vscode-settings";
  Schemas.vscodeWorkspaceTrust = "vscode-workspace-trust";
  Schemas.vscodeTerminal = "vscode-terminal";
  Schemas.webviewPanel = "webview-panel";
  Schemas.vscodeWebview = "vscode-webview";
  Schemas.extension = "extension";
  Schemas.vscodeFileResource = "vscode-file";
  Schemas.tmp = "tmp";
  Schemas.vsls = "vsls";
})(exports.Schemas || (exports.Schemas = {}));
const connectionTokenQueryName = "tkn";
class RemoteAuthoritiesImpl {
  constructor() {
    this._hosts = /* @__PURE__ */ Object.create(null);
    this._ports = /* @__PURE__ */ Object.create(null);
    this._connectionTokens = /* @__PURE__ */ Object.create(null);
    this._preferredWebSchema = "http";
    this._delegate = null;
  }
  setPreferredWebSchema(schema) {
    this._preferredWebSchema = schema;
  }
  rewrite(uri$1) {
    if (this._delegate) {
      return this._delegate(uri$1);
    }
    const authority = uri$1.authority;
    let host = this._hosts[authority];
    if (host && host.indexOf(":") !== -1) {
      host = `[${host}]`;
    }
    const port = this._ports[authority];
    const connectionToken = this._connectionTokens[authority];
    let query = `path=${encodeURIComponent(uri$1.path)}`;
    if (typeof connectionToken === "string") {
      query += `&${connectionTokenQueryName}=${encodeURIComponent(connectionToken)}`;
    }
    return uri.URI.from({
      scheme: platform.isWeb ? this._preferredWebSchema : exports.Schemas.vscodeRemoteResource,
      authority: `${host}:${port}`,
      path: `/vscode-remote-resource`,
      query
    });
  }
}
const RemoteAuthorities = new RemoteAuthoritiesImpl();
class FileAccessImpl {
  asBrowserUri(uriOrModule, moduleIdToUrl) {
    const uri2 = this.toUri(uriOrModule, moduleIdToUrl);
    if (uri2.scheme === exports.Schemas.vscodeRemote) {
      return RemoteAuthorities.rewrite(uri2);
    }
    if (
      // ...only ever for `file` resources
      uri2.scheme === exports.Schemas.file && // ...and we run in native environments
      (platform.isNative || // ...or web worker extensions on desktop
      platform.isWebWorker && platform.globals.origin === `${exports.Schemas.vscodeFileResource}://${FileAccessImpl.FALLBACK_AUTHORITY}`)
    ) {
      return uri2.with({
        scheme: exports.Schemas.vscodeFileResource,
        // We need to provide an authority here so that it can serve
        // as origin for network and loading matters in chromium.
        // If the URI is not coming with an authority already, we
        // add our own
        authority: uri2.authority || FileAccessImpl.FALLBACK_AUTHORITY,
        query: null,
        fragment: null
      });
    }
    return uri2;
  }
  toUri(uriOrModule, moduleIdToUrl) {
    if (uri.URI.isUri(uriOrModule)) {
      return uriOrModule;
    }
    return uri.URI.parse(moduleIdToUrl.toUrl(uriOrModule));
  }
}
FileAccessImpl.FALLBACK_AUTHORITY = "vscode-app";
const FileAccess = new FileAccessImpl();
exports.FileAccess = FileAccess;
exports.RemoteAuthorities = RemoteAuthorities;
exports.connectionTokenQueryName = connectionTokenQueryName;
