/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { MutableCluster } from "../cluster/mutation/MutableCluster.js";
import { Attribute, Command, TlvNoResponse, OptionalAttribute, OptionalEvent } from "../cluster/Cluster.js";
import { TlvUInt64, TlvUInt32 } from "../tlv/TlvNumber.js";
import { TlvNoArguments } from "../tlv/TlvNoArguments.js";
import { AccessLevel } from "@matter/model";
import { BitFlag } from "../schema/BitmapSchema.js";
import { TlvArray } from "../tlv/TlvArray.js";
import { TlvField, TlvOptionalField, TlvObject } from "../tlv/TlvObject.js";
import { TlvString, TlvByteString } from "../tlv/TlvString.js";
import { Priority } from "../globals/Priority.js";
import { ClusterRegistry } from "../cluster/ClusterRegistry.js";
var SoftwareDiagnostics;
((SoftwareDiagnostics2) => {
  let Feature;
  ((Feature2) => {
    Feature2["Watermarks"] = "Watermarks";
  })(Feature = SoftwareDiagnostics2.Feature || (SoftwareDiagnostics2.Feature = {}));
  SoftwareDiagnostics2.TlvThreadMetrics = TlvObject({
    /**
     * The Id field shall be a server-assigned per-thread unique ID that is constant for the duration of the thread.
     * Efforts SHOULD be made to avoid reusing ID values when possible.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.13.5.1.1
     */
    id: TlvField(0, TlvUInt64),
    /**
     * The Name field shall be set to a vendor defined name or prefix of the software thread that is static for the
     * duration of the thread.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.13.5.1.2
     */
    name: TlvOptionalField(1, TlvString.bound({ maxLength: 8 })),
    /**
     * The StackFreeCurrent field shall indicate the current amount of stack memory, in bytes, that are not being
     * utilized on the respective thread.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.13.5.1.3
     */
    stackFreeCurrent: TlvOptionalField(2, TlvUInt32),
    /**
     * The StackFreeMinimum field shall indicate the minimum amount of stack memory, in bytes, that has been
     * available at any point between the current time and this attribute being reset or initialized on the
     * respective thread. This value shall only be reset upon a Node reboot or upon receiving of the ResetWatermarks
     * command.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.13.5.1.4
     */
    stackFreeMinimum: TlvOptionalField(3, TlvUInt32),
    /**
     * The StackSize field shall indicate the amount of stack memory, in bytes, that has been allocated for use by
     * the respective thread.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.13.5.1.5
     */
    stackSize: TlvOptionalField(4, TlvUInt32)
  });
  SoftwareDiagnostics2.TlvSoftwareFaultEvent = TlvObject({
    /**
     * This field shall be set to the ID of the software thread in which the last software fault occurred.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.13.8.1.1
     */
    id: TlvField(0, TlvUInt64),
    /**
     * This field shall be set to a manufacturer-specified name or prefix of the software thread in which the last
     * software fault occurred.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.13.8.1.2
     */
    name: TlvOptionalField(1, TlvString.bound({ maxLength: 8 })),
    /**
     * This field shall be a manufacturer-specified payload intended to convey information to assist in further
     * diagnosing or debugging a software fault. The FaultRecording field may be used to convey information such as,
     * but not limited to, thread backtraces or register contents.
     *
     * @see {@link MatterSpecification.v142.Core} § 11.13.8.1.3
     */
    faultRecording: TlvOptionalField(2, TlvByteString.bound({ maxLength: 1024 }))
  });
  SoftwareDiagnostics2.WatermarksComponent = MutableCluster.Component({
    attributes: {
      /**
       * Indicates the maximum amount of heap memory, in bytes, that has been used by the Node. This value shall
       * only be reset upon a Node reboot or upon receiving of the ResetWatermarks command.
       *
       * @see {@link MatterSpecification.v142.Core} § 11.13.6.4
       */
      currentHeapHighWatermark: Attribute(3, TlvUInt64)
    },
    commands: {
      /**
       * This command is used to reset the high watermarks for heap and stack memory.
       *
       * Receipt of this command shall reset the following values which track high and lower watermarks:
       *
       *   - The StackFreeMinimum field of the ThreadMetrics attribute
       *
       *   - The CurrentHeapHighWatermark attribute
       *
       * ### Effect on Receipt
       *
       * On receipt of this command, the Node shall make the following modifications to attributes it supports:
       *
       * If implemented, the server shall set the value of the CurrentHeapHighWatermark attribute to the value of
       * the CurrentHeapUsed attribute.
       *
       * If implemented, the server shall set the value of the StackFreeMinimum field for every thread to the
       * value of the corresponding thread’s StackFreeCurrent field.
       *
       * @see {@link MatterSpecification.v142.Core} § 11.13.7.1
       */
      resetWatermarks: Command(0, TlvNoArguments, 0, TlvNoResponse, { invokeAcl: AccessLevel.Manage })
    }
  });
  SoftwareDiagnostics2.Base = MutableCluster.Component({
    id: 52,
    name: "SoftwareDiagnostics",
    revision: 1,
    features: {
      /**
       * Node makes available the metrics for high watermark related to memory consumption.
       */
      watermarks: BitFlag(0)
    },
    attributes: {
      /**
       * This attribute shall be a list of ThreadMetricsStruct structs. Each active thread on the Node shall be
       * represented by a single entry within the ThreadMetrics attribute.
       *
       * @see {@link MatterSpecification.v142.Core} § 11.13.6.1
       */
      threadMetrics: OptionalAttribute(0, TlvArray(SoftwareDiagnostics2.TlvThreadMetrics, { maxLength: 64 }), { default: [] }),
      /**
       * Indicates the current amount of heap memory, in bytes, that are free for allocation. The effective amount
       * may be smaller due to heap fragmentation or other reasons.
       *
       * @see {@link MatterSpecification.v142.Core} § 11.13.6.2
       */
      currentHeapFree: OptionalAttribute(1, TlvUInt64),
      /**
       * Indicates the current amount of heap memory, in bytes, that is being used.
       *
       * @see {@link MatterSpecification.v142.Core} § 11.13.6.3
       */
      currentHeapUsed: OptionalAttribute(2, TlvUInt64)
    },
    events: {
      /**
       * This Event shall be generated when a software fault occurs on the Node.
       *
       * @see {@link MatterSpecification.v142.Core} § 11.13.8.1
       */
      softwareFault: OptionalEvent(0, Priority.Info, SoftwareDiagnostics2.TlvSoftwareFaultEvent)
    },
    /**
     * This metadata controls which SoftwareDiagnosticsCluster elements matter.js activates for specific feature
     * combinations.
     */
    extensions: MutableCluster.Extensions({ flags: { watermarks: true }, component: SoftwareDiagnostics2.WatermarksComponent })
  });
  SoftwareDiagnostics2.ClusterInstance = MutableCluster(SoftwareDiagnostics2.Base);
  SoftwareDiagnostics2.Cluster = SoftwareDiagnostics2.ClusterInstance;
  const WTRMRK = { watermarks: true };
  SoftwareDiagnostics2.CompleteInstance = MutableCluster({
    id: SoftwareDiagnostics2.Cluster.id,
    name: SoftwareDiagnostics2.Cluster.name,
    revision: SoftwareDiagnostics2.Cluster.revision,
    features: SoftwareDiagnostics2.Cluster.features,
    attributes: {
      ...SoftwareDiagnostics2.Cluster.attributes,
      currentHeapHighWatermark: MutableCluster.AsConditional(
        SoftwareDiagnostics2.WatermarksComponent.attributes.currentHeapHighWatermark,
        { mandatoryIf: [WTRMRK] }
      )
    },
    commands: {
      resetWatermarks: MutableCluster.AsConditional(
        SoftwareDiagnostics2.WatermarksComponent.commands.resetWatermarks,
        { mandatoryIf: [WTRMRK] }
      )
    },
    events: SoftwareDiagnostics2.Cluster.events
  });
  SoftwareDiagnostics2.Complete = SoftwareDiagnostics2.CompleteInstance;
})(SoftwareDiagnostics || (SoftwareDiagnostics = {}));
const SoftwareDiagnosticsCluster = SoftwareDiagnostics.Cluster;
ClusterRegistry.register(SoftwareDiagnostics.Complete);
export {
  SoftwareDiagnostics,
  SoftwareDiagnosticsCluster
};
//# sourceMappingURL=software-diagnostics.js.map
