"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("#general");
var import_protocol = require("#protocol");
var import_types = require("#types");
/**
 * @license
 * Copyright 2022-2025 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) {
      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,
      sessionIntervals,
      tcpSupport,
      longIdleTimeOperatingMode
    } = long;
    if (discoveredAt !== void 0) {
      result.discoveredAt = discoveredAt;
    }
    if (ttl !== void 0) {
      result.ttl = (0, import_general.Seconds)(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 (sessionIntervals !== void 0) {
      const { idleInterval, activeInterval, activeThreshold } = sessionIntervals;
      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 = tcpSupport;
    }
    if (longIdleTimeOperatingMode !== void 0) {
      result.ICD = 1;
    }
    const isOperational = long.peerAddress !== void 0;
    if (isOperational) {
      if (addresses !== void 0) {
        result.addresses = addresses?.filter((address) => address.type === "udp").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.map(import_general.ServerAddress.definitionOf);
    }
    if (deviceIdentifier !== void 0) {
      long.deviceIdentifier = deviceIdentifier;
    }
    if (VP !== void 0) {
      const [vendor, product] = VP.split("+").map(Number.parseInt);
      long.vendorId = Number.isFinite(vendor) ? (0, import_types.VendorId)(vendor) : void 0;
      long.productId = Number.isFinite(product) ? product : void 0;
    }
    let sessionParameters;
    if (SII !== void 0) {
      (sessionParameters ??= {}).idleInterval = SII;
    }
    if (SAI !== void 0) {
      (sessionParameters ??= {}).activeInterval = SAI;
    }
    if (SAT !== void 0) {
      (sessionParameters ??= {}).activeThreshold = SAT;
    }
    long.sessionIntervals = sessionParameters;
    long.deviceType = DT === void 0 ? void 0 : (0, import_types.DeviceTypeId)(DT);
    long.deviceName = DN;
    long.rotatingIdentifier = RI;
    long.pairingHint = PH;
    long.pairingInstructions = PI;
    long.tcpSupport = 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 = {}));
//# sourceMappingURL=RemoteDescriptor.js.map
