"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 CommissionableMdnsAdvertisement_exports = {};
__export(CommissionableMdnsAdvertisement_exports, {
  CommissionableMdnsAdvertisement: () => CommissionableMdnsAdvertisement
});
module.exports = __toCommonJS(CommissionableMdnsAdvertisement_exports);
var import_PairingHintBitmap = require("#advertisement/PairingHintBitmap.js");
var import_MdnsConsts = require("#mdns/MdnsConsts.js");
var import_general = require("@matter/general");
var import_MdnsAdvertisement = require("./MdnsAdvertisement.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class CommissionableMdnsAdvertisement extends import_MdnsAdvertisement.MdnsAdvertisement {
  instanceId;
  constructor(advertiser, description) {
    const instanceId = advertiser.createInstanceId();
    const qname = (0, import_MdnsConsts.getCommissionableDeviceQname)(instanceId);
    super(advertiser, qname, description);
    this.instanceId = instanceId;
    this.#validatePairingInstructions();
  }
  get ptrRecords() {
    const { discriminator, deviceType, vendorId } = this.description;
    const shortDiscriminator = discriminator >> 8 & 15;
    const vendorQname = (0, import_MdnsConsts.getVendorQname)(vendorId);
    const deviceTypeQname = (0, import_MdnsConsts.getDeviceTypeQname)(deviceType);
    const shortDiscriminatorQname = (0, import_MdnsConsts.getShortDiscriminatorQname)(shortDiscriminator);
    const longDiscriminatorQname = (0, import_MdnsConsts.getLongDiscriminatorQname)(discriminator);
    const commissionModeQname = (0, import_MdnsConsts.getCommissioningModeQname)();
    const records = [
      (0, import_general.PtrRecord)(import_MdnsConsts.SERVICE_DISCOVERY_QNAME, import_MdnsConsts.MATTER_COMMISSION_SERVICE_QNAME),
      (0, import_general.PtrRecord)(import_MdnsConsts.SERVICE_DISCOVERY_QNAME, deviceTypeQname),
      (0, import_general.PtrRecord)(import_MdnsConsts.SERVICE_DISCOVERY_QNAME, shortDiscriminatorQname),
      (0, import_general.PtrRecord)(import_MdnsConsts.SERVICE_DISCOVERY_QNAME, longDiscriminatorQname),
      (0, import_general.PtrRecord)(import_MdnsConsts.SERVICE_DISCOVERY_QNAME, commissionModeQname),
      (0, import_general.PtrRecord)(import_MdnsConsts.MATTER_COMMISSION_SERVICE_QNAME, this.qname),
      (0, import_general.PtrRecord)(deviceTypeQname, this.qname),
      (0, import_general.PtrRecord)(shortDiscriminatorQname, this.qname),
      (0, import_general.PtrRecord)(longDiscriminatorQname, this.qname),
      (0, import_general.PtrRecord)(commissionModeQname, this.qname)
    ];
    if (!this.isPrivacyMasked) {
      records.push((0, import_general.PtrRecord)(import_MdnsConsts.SERVICE_DISCOVERY_QNAME, vendorQname), (0, import_general.PtrRecord)(vendorQname, this.qname));
    }
    return records;
  }
  get txtValues() {
    const {
      vendorId,
      productId,
      deviceType,
      name,
      discriminator,
      mode,
      pairingHint = import_MdnsConsts.DEFAULT_PAIRING_HINT,
      pairingInstructions
    } = this.description;
    const values = {
      DN: name,
      DT: deviceType,
      D: discriminator,
      CM: mode,
      PH: import_PairingHintBitmap.PairingHintBitmapSchema.encode(pairingHint),
      PI: pairingInstructions
    };
    if (!this.isPrivacyMasked) {
      values.VP = `${vendorId}+${productId}`;
    }
    return values;
  }
  isDuplicate(other) {
    return other.service === this.service && other.isCommissioning();
  }
  #validatePairingInstructions() {
    const { pairingHint, pairingInstructions } = this.description;
    if (pairingHint) {
      if (Object.values(pairingHint).every((v) => !v)) {
        throw new import_general.ImplementationError("At least one pairing hint must be set if pairing hint is provided");
      }
      const needsInstructionsCount = import_MdnsConsts.PAIRING_HINTS_REQUIRING_INSTRUCTION.reduce(
        (count, hint) => pairingHint[hint] === true ? count + 1 : count,
        0
      );
      if (needsInstructionsCount > 1) {
        throw new import_general.ImplementationError(
          `Only one pairing hint requiring instructions can be set, but got ${needsInstructionsCount}`
        );
      } else if (needsInstructionsCount === 1 && !pairingInstructions) {
        throw new import_general.ImplementationError(
          `Pairing instructions are required when a pairing hint requiring instructions is set`
        );
      } else if (needsInstructionsCount === 0 && pairingInstructions) {
        throw new import_general.ImplementationError(
          `Pairing instructions provided but no pairing hint requiring instructions set`
        );
      }
    } else if (pairingInstructions) {
      throw new import_general.ImplementationError("Pairing instructions provided but no pairing hint set");
    }
  }
}
//# sourceMappingURL=CommissionableMdnsAdvertisement.js.map
