import * as Bindings from "../bindings/bindings.js";
import { PrivateAPI } from "./ExtensionAPI.js";
export class LanguageExtensionEndpoint extends Bindings.DebuggerLanguagePlugins.DebuggerLanguagePlugin {
  supportedScriptTypes;
  port;
  nextRequestId;
  pendingRequests;
  constructor(name, supportedScriptTypes, port) {
    super(name);
    this.supportedScriptTypes = supportedScriptTypes;
    this.port = port;
    this.port.onmessage = this.onResponse.bind(this);
    this.nextRequestId = 0;
    this.pendingRequests = /* @__PURE__ */ new Map();
  }
  sendRequest(method, parameters) {
    return new Promise((resolve, reject) => {
      const requestId = this.nextRequestId++;
      this.pendingRequests.set(requestId, { resolve, reject });
      this.port.postMessage({ requestId, method, parameters });
    });
  }
  onResponse({ data }) {
    if ("event" in data) {
      const { event } = data;
      switch (event) {
        case PrivateAPI.LanguageExtensionPluginEvents.UnregisteredLanguageExtensionPlugin: {
          for (const { reject: reject2 } of this.pendingRequests.values()) {
            reject2(new Error("Language extension endpoint disconnected"));
          }
          this.pendingRequests.clear();
          this.port.close();
          const { pluginManager } = Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance();
          if (pluginManager) {
            pluginManager.removePlugin(this);
          }
          break;
        }
      }
      return;
    }
    const { requestId, result, error } = data;
    if (!this.pendingRequests.has(requestId)) {
      console.error(`No pending request ${requestId}`);
      return;
    }
    const { resolve, reject } = this.pendingRequests.get(requestId);
    this.pendingRequests.delete(requestId);
    if (error) {
      reject(new Error(error.message));
    } else {
      resolve(result);
    }
  }
  handleScript(script) {
    const language = script.scriptLanguage();
    return language !== null && script.debugSymbols !== null && language === this.supportedScriptTypes.language && this.supportedScriptTypes.symbol_types.includes(script.debugSymbols.type);
  }
  addRawModule(rawModuleId, symbolsURL, rawModule) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.AddRawModule, { rawModuleId, symbolsURL, rawModule });
  }
  removeRawModule(rawModuleId) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.RemoveRawModule, { rawModuleId });
  }
  sourceLocationToRawLocation(sourceLocation) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.SourceLocationToRawLocation, { sourceLocation });
  }
  rawLocationToSourceLocation(rawLocation) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.RawLocationToSourceLocation, { rawLocation });
  }
  getScopeInfo(type) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.GetScopeInfo, { type });
  }
  listVariablesInScope(rawLocation) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.ListVariablesInScope, { rawLocation });
  }
  getFunctionInfo(rawLocation) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.GetFunctionInfo, { rawLocation });
  }
  getInlinedFunctionRanges(rawLocation) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.GetInlinedFunctionRanges, { rawLocation });
  }
  getInlinedCalleesRanges(rawLocation) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.GetInlinedCalleesRanges, { rawLocation });
  }
  getTypeInfo(expression, context) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.GetTypeInfo, { expression, context });
  }
  getFormatter(expressionOrField, context) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.GetFormatter, { expressionOrField, context });
  }
  getInspectableAddress(field) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.GetInspectableAddress, { field });
  }
  async getMappedLines(rawModuleId, sourceFileURL) {
    return this.sendRequest(PrivateAPI.LanguageExtensionPluginCommands.GetMappedLines, { rawModuleId, sourceFileURL });
  }
  dispose() {
  }
}
//# sourceMappingURL=LanguageExtensionEndpoint.js.map
