/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Duration, Instant, Logger, Millis, Minutes, Seconds } from "#general";
const logger = Logger.get("PhysicalDeviceProperties");
const DEFAULT_SUBSCRIPTION_FLOOR_DEFAULT = Seconds(1);
const DEFAULT_SUBSCRIPTION_FLOOR_ICD = Instant;
const DEFAULT_SUBSCRIPTION_CEILING_WIFI = Minutes(1);
const DEFAULT_SUBSCRIPTION_CEILING_THREAD = Minutes(1);
const DEFAULT_SUBSCRIPTION_CEILING_THREAD_SLEEPY = Minutes(3);
const DEFAULT_SUBSCRIPTION_CEILING_BATTERY_POWERED = Minutes(10);
const THREAD_SUBSCRIPTION_CEILING_JITTER = 0.05;
var PhysicalDeviceProperties;
((PhysicalDeviceProperties2) => {
  function subscriptionIntervalBoundsFor(options) {
    const { properties, request } = options ?? {};
    let { description } = options ?? {};
    let minIntervalFloor, maxIntervalCeiling;
    if (request) {
      ({ minIntervalFloor, maxIntervalCeiling } = request);
    }
    if (description === void 0) {
      description = "Node";
    }
    const {
      isMainsPowered,
      isBatteryPowered,
      isIntermittentlyConnected,
      threadConnected,
      isThreadSleepyEndDevice
    } = properties ?? {};
    if (isIntermittentlyConnected) {
      if (minIntervalFloor !== void 0 && minIntervalFloor !== DEFAULT_SUBSCRIPTION_FLOOR_ICD) {
        logger.info(
          `${description}: Overwriting minIntervalFloorSeconds for intermittently connected device to ${Duration.format(DEFAULT_SUBSCRIPTION_FLOOR_ICD)}`
        );
        minIntervalFloor = DEFAULT_SUBSCRIPTION_FLOOR_ICD;
      }
    }
    if (minIntervalFloor === void 0) {
      minIntervalFloor = DEFAULT_SUBSCRIPTION_FLOOR_DEFAULT;
    }
    const defaultCeiling = isBatteryPowered && !isMainsPowered ? DEFAULT_SUBSCRIPTION_CEILING_BATTERY_POWERED : isThreadSleepyEndDevice ? DEFAULT_SUBSCRIPTION_CEILING_THREAD_SLEEPY : threadConnected ? DEFAULT_SUBSCRIPTION_CEILING_THREAD : DEFAULT_SUBSCRIPTION_CEILING_WIFI;
    if (maxIntervalCeiling === void 0) {
      maxIntervalCeiling = defaultCeiling;
    }
    if (maxIntervalCeiling < defaultCeiling) {
      logger.debug(
        `${description}: maxIntervalCeilingSeconds ideally is ${Duration.format(defaultCeiling)} instead of ${Duration.format(maxIntervalCeiling)} due to device type`
      );
    }
    if (threadConnected) {
      const maxJitter = maxIntervalCeiling * THREAD_SUBSCRIPTION_CEILING_JITTER;
      const jitter = Math.round(maxJitter * Math.random() * 2 - maxJitter);
      maxIntervalCeiling = Seconds(Seconds.of(Millis(maxIntervalCeiling + jitter)));
    }
    return {
      minIntervalFloor,
      maxIntervalCeiling
    };
  }
  PhysicalDeviceProperties2.subscriptionIntervalBoundsFor = subscriptionIntervalBoundsFor;
})(PhysicalDeviceProperties || (PhysicalDeviceProperties = {}));
export {
  PhysicalDeviceProperties
};
//# sourceMappingURL=PhysicalDeviceProperties.js.map
