/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ServerAddress } from "@matter/general";
import {
  PeerAddress,
  SessionIntervals,
  SupportedTransportsSchema
} from "@matter/protocol";
import { DeviceTypeId, VendorId } from "@matter/types";
var RemoteDescriptor;
((RemoteDescriptor2) => {
  function is(subject, object) {
    if (object.peerAddress !== void 0 && subject.peerAddress !== void 0) {
      return 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 = 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) => ServerAddress.isIp(address)).map(ServerAddress);
      }
    } else {
      if (addresses !== void 0) {
        result.addresses = addresses.map((address) => ({ ...address })).map(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) ? 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 : DeviceTypeId(DT, false);
    long.deviceName = DN;
    long.rotatingIdentifier = RI;
    long.pairingHint = PH;
    long.pairingInstructions = PI;
    long.tcpSupport = T === void 0 ? void 0 : 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 === SessionIntervals.maxAdvertisedInterval && current > advertised;
}
export {
  RemoteDescriptor
};
//# sourceMappingURL=RemoteDescriptor.js.map
