"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 BleAdvertisement_exports = {};
__export(BleAdvertisement_exports, {
  BleAdvertisement: () => BleAdvertisement
});
module.exports = __toCommonJS(BleAdvertisement_exports);
var import_BtpCodec = require("#codec/BtpCodec.js");
var import_general = require("@matter/general");
var import_Advertisement = require("../Advertisement.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const EARLY_INTERVAL_SLEEP = (0, import_general.Seconds)(30);
const LATE_INTERVAL_SLEEP = (0, import_general.Millis)((0, import_general.Minutes)(15) - EARLY_INTERVAL_SLEEP);
const EXTENDED_INTERVAL_SLEEP = (0, import_general.Millis)((0, import_general.Days)(2) - LATE_INTERVAL_SLEEP);
class BleAdvertisement extends import_Advertisement.Advertisement {
  constructor(advertiser, description) {
    super(advertiser, "ble:commissioning", description);
  }
  async run(context) {
    const {
      peripheral,
      config: { earlyInterval, lateInterval, extendedInterval }
    } = this.advertiser;
    let aad = this.advertiser.config.aad;
    let timeout = this.advertiser.config.timeout;
    let advertisementData = this.#encodedAdvertisement;
    let isExtended = false;
    const intervals = [
      { sleepTime: EARLY_INTERVAL_SLEEP, broadcastInterval: earlyInterval },
      { sleepTime: LATE_INTERVAL_SLEEP, broadcastInterval: lateInterval },
      { sleepTime: EXTENDED_INTERVAL_SLEEP, broadcastInterval: extendedInterval }
    ];
    try {
      for (const { sleepTime, broadcastInterval } of intervals) {
        if (!isExtended && this.isPrivacyMasked) {
          isExtended = true;
          advertisementData = this.#encodedAdvertisement;
          aad = void 0;
        }
        await peripheral.advertise(advertisementData, aad, broadcastInterval);
        await context.sleep("BLE advertisement interval", import_general.Duration.min(timeout, sleepTime));
        timeout = (0, import_general.Millis)(timeout - sleepTime);
        if (timeout <= 0) {
          break;
        }
      }
    } finally {
      await peripheral.stopAdvertising();
    }
  }
  get #encodedAdvertisement() {
    const { discriminator, vendorId, productId } = this.description;
    const { isPrivacyMasked: isExtendedAnnouncement } = this;
    return import_BtpCodec.BtpCodec.encodeBleAdvertisementData(
      discriminator,
      isExtendedAnnouncement ? 0 : vendorId,
      isExtendedAnnouncement ? 0 : productId,
      !isExtendedAnnouncement && !!this.advertiser.config.aad?.byteLength,
      isExtendedAnnouncement
    );
  }
  isDuplicate(other) {
    return other !== this && other.service === this.service;
  }
}
//# sourceMappingURL=BleAdvertisement.js.map
