"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 MultilevelSwitch_exports = {};
__export(MultilevelSwitch_exports, {
  MultilevelSwitchCCBehaviors: () => MultilevelSwitchCCBehaviors
});
module.exports = __toCommonJS(MultilevelSwitch_exports);
var import_cc = require("@zwave-js/cc");
var import_MultilevelSwitchCC = require("@zwave-js/cc/MultilevelSwitchCC");
var import_core = require("@zwave-js/core");
var import_shared = require("@zwave-js/shared");
var import_MockTransition = require("./MockTransition.js");
const defaultCapabilities = {
  defaultValue: 0,
  primarySwitchType: import_cc.SwitchType["Down/Up"],
  travelTime: 0
};
const STATE_KEY_PREFIX = "MultilevelSwitch_";
const StateKeys = {
  currentValue: `${STATE_KEY_PREFIX}currentValue`,
  transition: `${STATE_KEY_PREFIX}transition`
};
function stopCurrentTransition(self) {
  const existing = self.state.get(StateKeys.transition);
  if (existing) {
    const value = (0, import_MockTransition.stopTransition)(existing);
    self.state.set(StateKeys.currentValue, value);
    self.state.delete(StateKeys.transition);
    return { wasSupervised: existing.supervised };
  }
}
__name(stopCurrentTransition, "stopCurrentTransition");
function beginTransition(controller, self, receivedCC, targetValue, travelTime, defaultValue, supervised) {
  stopCurrentTransition(self);
  const currentValue = self.state.get(StateKeys.currentValue) ?? defaultValue ?? 0;
  const transition = (0, import_MockTransition.startTransition)({
    currentValue,
    targetValue,
    duration: travelTime,
    supervised,
    onComplete: /* @__PURE__ */ __name(async () => {
      self.state.set(StateKeys.currentValue, targetValue);
      self.state.delete(StateKeys.transition);
      if (supervised) {
        await self.sendResponse(receivedCC, {
          action: "ok"
        });
        return;
      }
      const cc = new import_MultilevelSwitchCC.MultilevelSwitchCCReport({
        nodeId: controller.ownNodeId,
        currentValue: targetValue,
        targetValue,
        duration: new import_core.Duration(0, "seconds")
      });
      await self.sendResponse(receivedCC, {
        action: "sendCC",
        cc
      });
    }, "onComplete"),
    onAbort: /* @__PURE__ */ __name(async () => {
      if (!supervised)
        return;
      await self.sendResponse(receivedCC, {
        action: "fail"
      });
    }, "onAbort")
  });
  if (transition) {
    self.state.set(StateKeys.transition, transition);
    return transition.durationMs;
  } else {
    self.state.set(StateKeys.currentValue, targetValue);
    return 0;
  }
}
__name(beginTransition, "beginTransition");
const respondToMultilevelSwitchGet = {
  handleCC(controller, self, receivedCC) {
    if (receivedCC instanceof import_MultilevelSwitchCC.MultilevelSwitchCCGet) {
      const capabilities = {
        ...defaultCapabilities,
        ...self.getCCCapabilities(import_core.CommandClasses["Multilevel Switch"], receivedCC.endpointIndex)
      };
      const transition = self.state.get(StateKeys.transition);
      let currentValue;
      let targetValue;
      let duration;
      if (transition) {
        currentValue = (0, import_MockTransition.getTransitionCurrentValue)(transition);
        targetValue = transition.targetValue;
        duration = (0, import_MockTransition.getTransitionRemainingDuration)(transition);
      } else {
        currentValue = self.state.get(StateKeys.currentValue) ?? capabilities.defaultValue ?? import_core.UNKNOWN_STATE;
        targetValue = currentValue;
        duration = new import_core.Duration(0, "seconds");
      }
      const cc = new import_MultilevelSwitchCC.MultilevelSwitchCCReport({
        nodeId: controller.ownNodeId,
        currentValue,
        targetValue,
        duration
      });
      return { action: "sendCC", cc };
    }
  }
};
const respondToMultilevelSwitchSet = {
  handleCC(controller, self, receivedCC) {
    if (receivedCC instanceof import_MultilevelSwitchCC.MultilevelSwitchCCSet) {
      const capabilities = {
        ...defaultCapabilities,
        ...self.getCCCapabilities(import_core.CommandClasses["Multilevel Switch"], receivedCC.endpointIndex)
      };
      const durationMs = beginTransition(controller, self, receivedCC, receivedCC.targetValue, capabilities.travelTime, capabilities.defaultValue, receivedCC.isEncapsulatedWith(import_core.CommandClasses.Supervision));
      return {
        action: "ok",
        durationMs
      };
    }
  }
};
const respondToMultilevelSwitchSupportedGet = {
  handleCC(controller, self, receivedCC) {
    if (receivedCC instanceof import_MultilevelSwitchCC.MultilevelSwitchCCSupportedGet) {
      const capabilities = {
        ...defaultCapabilities,
        ...self.getCCCapabilities(import_core.CommandClasses["Multilevel Switch"], receivedCC.endpointIndex)
      };
      const cc = new import_MultilevelSwitchCC.MultilevelSwitchCCSupportedReport({
        nodeId: controller.ownNodeId,
        switchType: capabilities.primarySwitchType
      });
      return { action: "sendCC", cc };
    }
  }
};
const respondToMultilevelSwitchStartLevelChange = {
  handleCC(controller, self, receivedCC) {
    if (receivedCC instanceof import_MultilevelSwitchCC.MultilevelSwitchCCStartLevelChange) {
      const capabilities = {
        ...defaultCapabilities,
        ...self.getCCCapabilities(import_core.CommandClasses["Multilevel Switch"], receivedCC.endpointIndex)
      };
      const targetValue = receivedCC.direction === "up" ? 99 : 0;
      const durationMs = beginTransition(controller, self, receivedCC, targetValue, capabilities.travelTime, capabilities.defaultValue, receivedCC.isEncapsulatedWith(import_core.CommandClasses.Supervision));
      return {
        action: "ok",
        durationMs
      };
    }
  }
};
const respondToMultilevelSwitchStopLevelChange = {
  handleCC(controller, self, receivedCC) {
    if (receivedCC instanceof import_MultilevelSwitchCC.MultilevelSwitchCCStopLevelChange) {
      const stoppedTransition = stopCurrentTransition(self);
      const currentValue = self.state.get(StateKeys.currentValue) ?? 0;
      if (!stoppedTransition?.wasSupervised) {
        (0, import_shared.setTimer)(async () => {
          const cc = new import_MultilevelSwitchCC.MultilevelSwitchCCReport({
            nodeId: controller.ownNodeId,
            currentValue,
            targetValue: currentValue,
            duration: new import_core.Duration(0, "seconds")
          });
          await self.sendResponse(receivedCC, {
            action: "sendCC",
            cc
          });
        }, 100).unref();
      }
      return { action: "ok" };
    }
  }
};
const MultilevelSwitchCCBehaviors = [
  respondToMultilevelSwitchGet,
  respondToMultilevelSwitchSet,
  respondToMultilevelSwitchSupportedGet,
  respondToMultilevelSwitchStartLevelChange,
  respondToMultilevelSwitchStopLevelChange
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  MultilevelSwitchCCBehaviors
});
//# sourceMappingURL=MultilevelSwitch.js.map
