/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { InternalError, UnexpectedDataError } from "#general";
import { ValidationOutOfBoundsError, validatorOf } from "../common/ValidationError.js";
import { TlvUInt16 } from "../tlv/TlvNumber.js";
import { TlvWrapper } from "../tlv/TlvWrapper.js";
function GroupId(groupId, validate = true) {
  if (!validate || groupId >= 0 && groupId <= 65535) {
    return Number(groupId);
  }
  throw new ValidationOutOfBoundsError(`Invalid group ID: ${groupId}`);
}
((GroupId2) => {
  GroupId2.NO_GROUP_ID = GroupId2(0);
  GroupId2.ALL_NODES = 65535;
  GroupId2.ALL_NON_ICD_NODES = 65534;
  GroupId2.isValid = validatorOf(GroupId2);
  function isApplicationGroupId(groupId) {
    return groupId >= 1 && groupId <= 65279;
  }
  GroupId2.isApplicationGroupId = isApplicationGroupId;
  function assertGroupId(groupId) {
    if (groupId === GroupId2.NO_GROUP_ID) {
      throw new UnexpectedDataError(`A Group ID need to be specified and can not be 0`);
    }
  }
  GroupId2.assertGroupId = assertGroupId;
  function fromNodeId(nodeId) {
    if (!(0, GroupId2.isGroupNodeId)(nodeId)) {
      throw new InternalError(`Node ID ${nodeId} is not a group node ID`);
    }
    const groupId = Number(nodeId & BigInt(65535));
    return GroupId2(groupId);
  }
  GroupId2.fromNodeId = fromNodeId;
  GroupId2.isGroupNodeId = (nodeId) => {
    const nodeIdHex = nodeId.toString(16);
    return nodeIdHex.startsWith("ffffffffffff") && nodeIdHex.length === 16;
  };
})(GroupId || (GroupId = {}));
const TlvGroupId = new TlvWrapper(
  TlvUInt16,
  (groupId) => groupId,
  (value) => GroupId(value, false)
  // No automatic validation on decode
);
export {
  GroupId,
  TlvGroupId
};
//# sourceMappingURL=GroupId.js.map
