/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { MutableCluster } from "../cluster/mutation/MutableCluster.js";
import { Command } from "../cluster/Cluster.js";
import { TlvField, TlvOptionalField, TlvObject } from "../tlv/TlvObject.js";
import { TlvEnum, TlvEpochUs, TlvSysTimeUs } from "../tlv/TlvNumber.js";
import { TlvString, TlvByteString } from "../tlv/TlvString.js";
import { StatusResponseError } from "../common/StatusResponseError.js";
import { Status as GlobalStatus } from "../globals/Status.js";
import { ClusterRegistry } from "../cluster/ClusterRegistry.js";
var DiagnosticLogs;
((DiagnosticLogs2) => {
  let Intent;
  ((Intent2) => {
    Intent2[Intent2["EndUserSupport"] = 0] = "EndUserSupport";
    Intent2[Intent2["NetworkDiag"] = 1] = "NetworkDiag";
    Intent2[Intent2["CrashLogs"] = 2] = "CrashLogs";
  })(Intent = DiagnosticLogs2.Intent || (DiagnosticLogs2.Intent = {}));
  let TransferProtocol;
  ((TransferProtocol2) => {
    TransferProtocol2[TransferProtocol2["ResponsePayload"] = 0] = "ResponsePayload";
    TransferProtocol2[TransferProtocol2["Bdx"] = 1] = "Bdx";
  })(TransferProtocol = DiagnosticLogs2.TransferProtocol || (DiagnosticLogs2.TransferProtocol = {}));
  DiagnosticLogs2.TlvRetrieveLogsRequest = TlvObject({
    /**
     * This field shall indicate why the diagnostic logs are being retrieved from the Node. A Node may utilize this
     * field to selectively determine the logs to transfer.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.11.5.1.1
     */
    intent: TlvField(0, TlvEnum()),
    /**
     * This field shall be used to indicate how the log transfer is to be realized. If the field is set to BDX, then
     * if the receiving Node supports BDX it shall attempt to use BDX to transfer any potential diagnostic logs; if
     * the receiving Node does not support BDX then the Node shall follow the requirements defined for a
     * TransferProtocolEnum of ResponsePayload. If this field is set to ResponsePayload the receiving Node shall
     * only utilize the LogContent field of the RetrieveLogsResponse command to transfer diagnostic log information.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.11.5.1.2
     */
    requestedProtocol: TlvField(1, TlvEnum()),
    /**
     * This field shall be present if the RequestedProtocol is BDX. The TransferFileDesignator shall be set as the
     * File Designator of the BDX transfer if initiated.
     *
     * ### Effect on Receipt
     *
     * On receipt of this command, the Node shall respond with a RetrieveLogsResponse command.
     *
     * If the RequestedProtocol is set to BDX the Node SHOULD immediately realize the RetrieveLogsResponse command
     * by initiating a BDX Transfer, sending a BDX SendInit message with the File Designator field of the message
     * set to the value of the TransferFileDesignator field of the RetrieveLogsRequest. On reception of a BDX
     * SendAccept message the Node shall send a RetrieveLogsResponse command with a Status field set to Success and
     * proceed with the log transfer over BDX. If a failure StatusReport is received in response to the SendInit
     * message, the Node shall send a RetrieveLogsResponse command with a Status of Denied. In the case where the
     * Node is able to fit the entirety of the requested logs within the LogContent field, the Status field of the
     * RetrieveLogsResponse shall be set to Exhausted and a BDX session shall NOT be initiated.
     *
     * If the RequestedProtocol is set to BDX and either the Node does not support BDX or it is not possible for the
     * Node to establish a BDX session, then the Node shall utilize the LogContent field of the RetrieveLogsResponse
     * command to transfer as much of the current logs as it can fit within the response, and the Status field of
     * the RetrieveLogsResponse shall be set to Exhausted.
     *
     * If the RequestedProtocol is set to ResponsePayload the Node shall utilize the LogContent field of the
     * RetrieveLogsResponse command to transfer as much of the current logs as it can fit within the response, and a
     * BDX session shall NOT be initiated.
     *
     * If the RequestedProtocol is set to BDX and there is no TransferFileDesignator the command shall fail with a
     * Status Code of INVALID_COMMAND.
     *
     * If the Intent and/or the RequestedProtocol arguments contain invalid (out of range) values the command shall
     * fail with a Status Code of INVALID_COMMAND.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.11.5.1.3
     */
    transferFileDesignator: TlvOptionalField(2, TlvString.bound({ maxLength: 32 }))
  });
  let Status;
  ((Status2) => {
    Status2[Status2["Success"] = 0] = "Success";
    Status2[Status2["Exhausted"] = 1] = "Exhausted";
    Status2[Status2["NoLogs"] = 2] = "NoLogs";
    Status2[Status2["Busy"] = 3] = "Busy";
    Status2[Status2["Denied"] = 4] = "Denied";
  })(Status = DiagnosticLogs2.Status || (DiagnosticLogs2.Status = {}));
  class ExhaustedError extends StatusResponseError {
    constructor(message = "All logs has been transferred", code = GlobalStatus.Failure, clusterCode = 1 /* Exhausted */) {
      super(message, code, clusterCode);
    }
  }
  DiagnosticLogs2.ExhaustedError = ExhaustedError;
  class NoLogsError extends StatusResponseError {
    constructor(message = "No logs of the requested type available", code = GlobalStatus.Failure, clusterCode = 2 /* NoLogs */) {
      super(message, code, clusterCode);
    }
  }
  DiagnosticLogs2.NoLogsError = NoLogsError;
  class BusyError extends StatusResponseError {
    constructor(message = "Unable to handle request, retry later", code = GlobalStatus.Failure, clusterCode = 3 /* Busy */) {
      super(message, code, clusterCode);
    }
  }
  DiagnosticLogs2.BusyError = BusyError;
  class DeniedError extends StatusResponseError {
    constructor(message = "The request is denied, no logs being transferred", code = GlobalStatus.Failure, clusterCode = 4 /* Denied */) {
      super(message, code, clusterCode);
    }
  }
  DiagnosticLogs2.DeniedError = DeniedError;
  DiagnosticLogs2.TlvRetrieveLogsResponse = TlvObject({
    /**
     * This field shall indicate the result of an attempt to retrieve diagnostic logs.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.11.5.2.1
     */
    status: TlvField(0, TlvEnum()),
    /**
     * This field shall be included in the command if the Status field has a value of Success or Exhausted. A Node
     * SHOULD utilize this field to transfer the newest diagnostic log entries. This field shall be empty if BDX is
     * requested and the Status field has a value of Success.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.11.5.2.2
     */
    logContent: TlvField(1, TlvByteString.bound({ maxLength: 1024 })),
    /**
     * This field SHOULD be included in the command if the Status field has a value of Success and the Node
     * maintains a wall clock. When included, the UTCTimeStamp field shall contain the value of the oldest log entry
     * in the diagnostic logs that are being transferred.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.11.5.2.3
     */
    utcTimeStamp: TlvOptionalField(2, TlvEpochUs),
    /**
     * This field SHOULD be included in the command if the Status field has a value of Success. When included, the
     * TimeSinceBoot field shall contain the time of the oldest log entry in the diagnostic logs that are being
     * transferred represented by the number of microseconds since the last time the Node went through a reboot.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.11.5.2.4
     */
    timeSinceBoot: TlvOptionalField(3, TlvSysTimeUs)
  });
  DiagnosticLogs2.ClusterInstance = MutableCluster({
    id: 50,
    name: "DiagnosticLogs",
    revision: 1,
    commands: {
      /**
       * Reception of this command starts the process of retrieving diagnostic logs from a Node.
       *
       * @see {@link MatterSpecification.v142.Core} § 11.11.5.1
       */
      retrieveLogsRequest: Command(0, DiagnosticLogs2.TlvRetrieveLogsRequest, 1, DiagnosticLogs2.TlvRetrieveLogsResponse)
    }
  });
  DiagnosticLogs2.Cluster = DiagnosticLogs2.ClusterInstance;
  DiagnosticLogs2.Complete = DiagnosticLogs2.Cluster;
})(DiagnosticLogs || (DiagnosticLogs = {}));
const DiagnosticLogsCluster = DiagnosticLogs.Cluster;
ClusterRegistry.register(DiagnosticLogs.Complete);
export {
  DiagnosticLogs,
  DiagnosticLogsCluster
};
//# sourceMappingURL=diagnostic-logs.js.map
