"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var message_handler_exports = {};
__export(message_handler_exports, {
  EndpointMessageHandler: () => EndpointMessageHandler
});
module.exports = __toCommonJS(message_handler_exports);
var import_error = require("../error.js");
var import_state = require("../state.js");
var import_command = require("./command.js");
var import_common = require("../common.js");
var import_message_handler = require("./access_control/message_handler.js");
class EndpointMessageHandler {
  static {
    __name(this, "EndpointMessageHandler");
  }
  driver;
  client;
  subHandlers = {
    access_control: new import_message_handler.EndpointAccessControlMessageHandler()
  };
  constructor(driver, client) {
    this.driver = driver;
    this.client = client;
  }
  async handle(message) {
    const { nodeId, command } = message;
    let endpoint;
    const node = this.driver.controller.nodes.get(nodeId);
    if (!node) {
      throw new import_error.NodeNotFoundError(nodeId);
    }
    if (message.endpoint) {
      endpoint = node.getEndpoint(message.endpoint);
      if (!endpoint) {
        throw new import_error.EndpointNotFoundError(nodeId, message.endpoint);
      }
    } else {
      endpoint = node;
    }
    const subHandler = this.subHandlers[message.command.split(".")[1]];
    if (subHandler) {
      return subHandler.handle(message, endpoint);
    }
    switch (message.command) {
      case import_command.EndpointCommand.invokeCCAPI: {
        const response = await endpoint.invokeCCAPI(message.commandClass, message.methodName, ...(0, import_common.deserializeBufferInArray)(message.args));
        return { response };
      }
      case import_command.EndpointCommand.supportsCCAPI: {
        const supported = endpoint.supportsCCAPI(message.commandClass);
        return { supported };
      }
      case import_command.EndpointCommand.supportsCC: {
        const supported = endpoint.supportsCC(message.commandClass);
        return { supported };
      }
      case import_command.EndpointCommand.controlsCC: {
        const controlled = endpoint.controlsCC(message.commandClass);
        return { controlled };
      }
      case import_command.EndpointCommand.isCCSecure: {
        const secure = endpoint.isCCSecure(message.commandClass);
        return { secure };
      }
      case import_command.EndpointCommand.getCCVersion: {
        const version = endpoint.getCCVersion(message.commandClass);
        return { version };
      }
      case import_command.EndpointCommand.getNodeUnsafe:
      case import_command.EndpointCommand.tryGetNode: {
        const node2 = endpoint.tryGetNode();
        return {
          node: node2 === void 0 ? node2 : (0, import_state.dumpNode)(node2, this.client.schemaVersion)
        };
      }
      case import_command.EndpointCommand.setRawConfigParameterValue: {
        return (0, import_common.setRawConfigParameterValue)(message, endpoint);
      }
      case import_command.EndpointCommand.getRawConfigParameterValue: {
        return (0, import_common.getRawConfigParameterValue)(message, endpoint);
      }
      case import_command.EndpointCommand.getCCs: {
        const commandClasses = Object.fromEntries(endpoint.getCCs());
        return { commandClasses };
      }
      case import_command.EndpointCommand.maySupportBasicCC: {
        const maySupport = endpoint.maySupportBasicCC();
        return { maySupport };
      }
      case import_command.EndpointCommand.wasCCRemovedViaConfig: {
        const removed = endpoint.wasCCRemovedViaConfig(message.commandClass);
        return { removed };
      }
      default: {
        throw new import_error.UnknownCommandError(command);
      }
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  EndpointMessageHandler
});
//# sourceMappingURL=message_handler.js.map
