/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Status } from "#globals/Status.js";
import { MatterError, capitalize, decamelize } from "@matter/general";
const specializationIndex = {};
class StatusResponseError extends MatterError {
  constructor(message, code, clusterCode) {
    super(message);
    this.code = code;
    this.clusterCode = clusterCode;
  }
  get bareMessage() {
    return this.message.replace(/ \(code .+\)$/, "");
  }
  static is(error, ...codes) {
    const sre = StatusResponseError.of(error);
    return !!sre && (!codes.length || codes.includes(sre.code));
  }
  get id() {
    const id = super.id;
    if (id === "status-response") {
      const code = Status[this.code];
      if (code) {
        return decamelize(code);
      }
    }
    return id;
  }
  static create(code, message, clusterCode) {
    const type = specializationIndex[code];
    if (type) {
      return new type(message, clusterCode);
    }
    throw new UnknownStatusResponseError(message ?? "Unknown status response", code, clusterCode);
  }
}
class UnknownStatusResponseError extends StatusResponseError {
}
const StatusResponse = Object.fromEntries(
  Object.entries(Status).map(([name, code]) => {
    if (typeof code !== "number") {
      return void 0;
    }
    const ct = {
      [name]: class extends StatusResponseError {
        constructor(message, clusterCode) {
          if (message === void 0) {
            message = capitalize(decamelize(name, " "));
          }
          super(message, code, clusterCode);
          let codeStr = `code ${code}`;
          if (clusterCode !== void 0) {
            codeStr = `${codeStr}; cluster code ${clusterCode}`;
          }
          this.message = `${message} (${codeStr})`;
        }
      }
    };
    const constructor = ct[name];
    Object.defineProperty(constructor, "name", { value: name });
    specializationIndex[code] = constructor;
    return [`${name}Error`, constructor];
  }).filter((e) => e)
);
class ReceivedStatusResponseError extends StatusResponseError {
}
export {
  ReceivedStatusResponseError,
  StatusResponse,
  StatusResponseError,
  UnknownStatusResponseError
};
//# sourceMappingURL=StatusResponseError.js.map
