"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 RemoteDescriptor_exports = {};
__export(RemoteDescriptor_exports, {
  RemoteDescriptor: () => RemoteDescriptor
});
module.exports = __toCommonJS(RemoteDescriptor_exports);
var import_general = require("@matter/general");
var import_protocol = require("@matter/protocol");
var import_types = require("@matter/types");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
var RemoteDescriptor;
((RemoteDescriptor2) => {
  function is(subject, object) {
    if (object.peerAddress !== void 0 && subject.peerAddress !== void 0) {
      return import_protocol.PeerAddress.is(subject.peerAddress, object.peerAddress);
    }
    if (object.deviceIdentifier !== void 0 && object.deviceIdentifier !== "") {
      return subject.deviceIdentifier === object.deviceIdentifier;
    }
    return false;
  }
  RemoteDescriptor2.is = is;
  function fromLongForm(long) {
    const result = {};
    const {
      addresses,
      discoveredAt,
      ttl,
      deviceIdentifier,
      discriminator,
      commissioningMode,
      vendorId,
      productId,
      deviceType,
      deviceName,
      rotatingIdentifier,
      pairingHint,
      pairingInstructions,
      sessionParameters,
      tcpSupport,
      longIdleTimeOperatingMode
    } = long;
    if (discoveredAt !== void 0) {
      result.discoveredAt = discoveredAt;
    }
    if (ttl !== void 0) {
      result.ttl = ttl;
    }
    if (deviceIdentifier !== void 0) {
      result.deviceIdentifier = deviceIdentifier;
    }
    if (vendorId !== void 0) {
      if (productId !== void 0) {
        result.VP = `${vendorId}+${productId}`;
      } else {
        result.VP = `${vendorId}`;
      }
    }
    if (deviceType !== void 0) {
      result.DT = deviceType;
    }
    if (deviceName !== void 0) {
      result.DN = deviceName;
    }
    if (rotatingIdentifier !== void 0) {
      result.RI = rotatingIdentifier;
    }
    if (pairingHint !== void 0) {
      result.PH = pairingHint;
      if (pairingInstructions !== void 0) {
        result.PI = pairingInstructions;
      }
    }
    if (sessionParameters !== void 0) {
      const { idleInterval, activeInterval, activeThreshold } = sessionParameters;
      if (idleInterval !== void 0) {
        result.SII = idleInterval;
      }
      if (activeInterval !== void 0) {
        result.SAI = activeInterval;
      }
      if (activeThreshold !== void 0) {
        result.SAT = activeThreshold;
      }
    }
    if (tcpSupport !== void 0) {
      result.T = import_protocol.SupportedTransportsSchema.decode(tcpSupport);
    }
    if (longIdleTimeOperatingMode !== void 0) {
      result.ICD = longIdleTimeOperatingMode ? 1 : 0;
    }
    const isOperational = long.peerAddress !== void 0;
    if (isOperational) {
      if (addresses !== void 0) {
        result.addresses = addresses?.filter((address) => import_general.ServerAddress.isIp(address)).map(import_general.ServerAddress);
      }
    } else {
      if (addresses !== void 0) {
        result.addresses = addresses.map((address) => ({ ...address })).map(import_general.ServerAddress);
      }
      if (discriminator !== void 0) {
        result.D = discriminator;
      }
      if (commissioningMode !== void 0) {
        result.CM = commissioningMode;
      }
    }
    return result;
  }
  RemoteDescriptor2.fromLongForm = fromLongForm;
  function toLongForm(descriptor, long = {}) {
    if (!descriptor) {
      return long;
    }
    const { addresses, discoveredAt, ttl, deviceIdentifier, VP, DT, DN, RI, PH, PI, SII, SAI, SAT, T, ICD } = descriptor;
    if (discoveredAt !== void 0) {
      long.discoveredAt = discoveredAt;
    }
    if (ttl !== void 0) {
      long.ttl = ttl;
    }
    if (addresses?.length) {
      long.addresses = addresses;
    }
    if (deviceIdentifier !== void 0) {
      long.deviceIdentifier = deviceIdentifier;
    }
    if (VP !== void 0) {
      const [vendor, product] = VP.split("+").map((part) => Number.parseInt(part, 10));
      long.vendorId = Number.isFinite(vendor) ? (0, import_types.VendorId)(vendor, false) : void 0;
      long.productId = Number.isFinite(product) ? product : void 0;
    }
    if (SII !== void 0 && !isCappedBelow(SII, long.sessionParameters?.idleInterval)) {
      (long.sessionParameters ??= {}).idleInterval = SII;
    }
    if (SAI !== void 0 && !isCappedBelow(SAI, long.sessionParameters?.activeInterval)) {
      (long.sessionParameters ??= {}).activeInterval = SAI;
    }
    if (SAT !== void 0) {
      (long.sessionParameters ??= {}).activeThreshold = SAT;
    }
    long.deviceType = DT === void 0 ? void 0 : (0, import_types.DeviceTypeId)(DT, false);
    long.deviceName = DN;
    long.rotatingIdentifier = RI;
    long.pairingHint = PH;
    long.pairingInstructions = PI;
    long.tcpSupport = T === void 0 ? void 0 : import_protocol.SupportedTransportsSchema.encode(T);
    long.longIdleTimeOperatingMode = ICD === void 0 ? void 0 : ICD === 1;
    if ("D" in descriptor) {
      long.discriminator = descriptor.D;
    }
    if ("CM" in descriptor) {
      long.commissioningMode = descriptor.CM;
    }
    return long;
  }
  RemoteDescriptor2.toLongForm = toLongForm;
})(RemoteDescriptor || (RemoteDescriptor = {}));
function isCappedBelow(advertised, current) {
  return current !== void 0 && advertised === import_protocol.SessionIntervals.maxAdvertisedInterval && current > advertised;
}
//# sourceMappingURL=RemoteDescriptor.js.map
