"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
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 Lock_exports = {};
__export(Lock_exports, {
  LockCCBehaviors: () => LockCCBehaviors
});
module.exports = __toCommonJS(Lock_exports);
var import_LockCC = require("@zwave-js/cc/LockCC");
var import_core = require("@zwave-js/core");
var import_shared = require("@zwave-js/shared");
const defaultCapabilities = {
  travelTime: 0
};
const STATE_KEY_PREFIX = "Lock_";
const StateKeys = {
  locked: `${STATE_KEY_PREFIX}locked`,
  transition: `${STATE_KEY_PREFIX}transition`
};
function stopCurrentTransition(self) {
  const existing = self.state.get(StateKeys.transition);
  if (existing) {
    existing.timer.clear();
    self.state.set(StateKeys.locked, existing.targetLocked);
    self.state.delete(StateKeys.transition);
    return { wasSupervised: existing.supervised };
  }
}
__name(stopCurrentTransition, "stopCurrentTransition");
function beginTransition(controller, self, receivedCC, targetLocked, travelTime, supervised, onCompleteSupervised) {
  stopCurrentTransition(self);
  const currentLocked = self.state.get(StateKeys.locked) ?? false;
  if (currentLocked === targetLocked || travelTime === 0) {
    self.state.set(StateKeys.locked, targetLocked);
    return 0;
  }
  const timer = (0, import_shared.setTimer)(async () => {
    self.state.set(StateKeys.locked, targetLocked);
    self.state.delete(StateKeys.transition);
    if (supervised) {
      await onCompleteSupervised?.();
      return;
    }
    const cc = new import_LockCC.LockCCReport({
      nodeId: controller.ownNodeId,
      locked: targetLocked
    });
    await self.sendResponse(receivedCC, {
      action: "sendCC",
      cc
    });
  }, travelTime).unref();
  const transition = {
    targetLocked,
    timer,
    supervised
  };
  self.state.set(StateKeys.transition, transition);
  return travelTime;
}
__name(beginTransition, "beginTransition");
const respondToLockGet = {
  handleCC(controller, self, receivedCC) {
    if (receivedCC instanceof import_LockCC.LockCCGet) {
      const locked = self.state.get(StateKeys.locked) ?? false;
      const cc = new import_LockCC.LockCCReport({
        nodeId: controller.ownNodeId,
        locked
      });
      return { action: "sendCC", cc };
    }
  }
};
const respondToLockSet = {
  handleCC(controller, self, receivedCC) {
    if (receivedCC instanceof import_LockCC.LockCCSet) {
      const capabilities = {
        ...defaultCapabilities,
        ...self.getCCCapabilities(import_core.CommandClasses.Lock, receivedCC.endpointIndex)
      };
      const supervised = receivedCC.isEncapsulatedWith(import_core.CommandClasses.Supervision);
      const durationMs = beginTransition(controller, self, receivedCC, receivedCC.locked, capabilities.travelTime, supervised, async () => self.sendResponse(receivedCC, {
        action: "ok"
      }));
      return {
        action: "ok",
        durationMs
      };
    }
  }
};
const LockCCBehaviors = [
  respondToLockGet,
  respondToLockSet
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  LockCCBehaviors
});
//# sourceMappingURL=Lock.js.map
