"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var ThermostatOperatingStateCC_exports = {};
__export(ThermostatOperatingStateCC_exports, {
  ThermostatOperatingStateCC: () => ThermostatOperatingStateCC,
  ThermostatOperatingStateCCAPI: () => ThermostatOperatingStateCCAPI,
  ThermostatOperatingStateCCGet: () => ThermostatOperatingStateCCGet,
  ThermostatOperatingStateCCLoggingGet: () => ThermostatOperatingStateCCLoggingGet,
  ThermostatOperatingStateCCLoggingReport: () => ThermostatOperatingStateCCLoggingReport,
  ThermostatOperatingStateCCLoggingSupportedGet: () => ThermostatOperatingStateCCLoggingSupportedGet,
  ThermostatOperatingStateCCLoggingSupportedReport: () => ThermostatOperatingStateCCLoggingSupportedReport,
  ThermostatOperatingStateCCReport: () => ThermostatOperatingStateCCReport,
  ThermostatOperatingStateCCValues: () => import_CCValues_generated.ThermostatOperatingStateCCValues
});
module.exports = __toCommonJS(ThermostatOperatingStateCC_exports);
var import_tslib = require("tslib");
var __validateArgs = __toESM(require("./ThermostatOperatingStateCC._validateArgs.js"), 1);
var import_core = require("@zwave-js/core");
var import_shared = require("@zwave-js/shared");
var import_API = require("../lib/API.js");
var import_CommandClass = require("../lib/CommandClass.js");
var import_CommandClassDecorators = require("../lib/CommandClassDecorators.js");
var import_Types = require("../lib/_Types.js");
var import_CCValues_generated = require("./_CCValues.generated.js");
const { validateArgs_ThermostatOperatingStateCCAPI_getLogging } = __validateArgs;
let ThermostatOperatingStateCCAPI = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.API)(import_core.CommandClasses["Thermostat Operating State"])];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = import_API.PhysicalCCAPI;
  let _instanceExtraInitializers = [];
  let _getLogging_decorators;
  var ThermostatOperatingStateCCAPI2 = class extends _classSuper {
    static {
      __name(this, "ThermostatOperatingStateCCAPI");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      _getLogging_decorators = [validateArgs_ThermostatOperatingStateCCAPI_getLogging()];
      (0, import_tslib.__esDecorate)(this, null, _getLogging_decorators, { kind: "method", name: "getLogging", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "getLogging" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.getLogging, "get") }, metadata: _metadata }, null, _instanceExtraInitializers);
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      ThermostatOperatingStateCCAPI2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    supportsCommand(cmd) {
      switch (cmd) {
        case import_Types.ThermostatOperatingStateCommand.Get:
          return true;
        // This is mandatory
        case import_Types.ThermostatOperatingStateCommand.LoggingSupportedGet:
        case import_Types.ThermostatOperatingStateCommand.LoggingGet:
          return this.version >= 2;
      }
      return super.supportsCommand(cmd);
    }
    get [import_API.POLL_VALUE]() {
      return async function({ property }) {
        switch (property) {
          case "state":
            return this.get();
          default:
            (0, import_API.throwUnsupportedProperty)(this.ccId, property);
        }
      };
    }
    async get() {
      this.assertSupportsCommand(import_Types.ThermostatOperatingStateCommand, import_Types.ThermostatOperatingStateCommand.Get);
      const cc = new ThermostatOperatingStateCCGet({
        nodeId: this.endpoint.nodeId,
        endpointIndex: this.endpoint.index
      });
      const response = await this.host.sendCommand(cc, this.commandOptions);
      return response?.state;
    }
    async getSupportedLoggingTypes() {
      this.assertSupportsCommand(import_Types.ThermostatOperatingStateCommand, import_Types.ThermostatOperatingStateCommand.LoggingSupportedGet);
      const cc = new ThermostatOperatingStateCCLoggingSupportedGet({
        nodeId: this.endpoint.nodeId,
        endpointIndex: this.endpoint.index
      });
      const response = await this.host.sendCommand(cc, this.commandOptions);
      return response?.supportedLoggingTypes;
    }
    async getLogging(states) {
      this.assertSupportsCommand(import_Types.ThermostatOperatingStateCommand, import_Types.ThermostatOperatingStateCommand.LoggingGet);
      const cc = new ThermostatOperatingStateCCLoggingGet({
        nodeId: this.endpoint.nodeId,
        endpointIndex: this.endpoint.index,
        requestedStates: states
      });
      const response = await this.host.sendCommand(cc, this.commandOptions);
      return response?.loggingData;
    }
    constructor() {
      super(...arguments);
      (0, import_tslib.__runInitializers)(this, _instanceExtraInitializers);
    }
  };
  return ThermostatOperatingStateCCAPI2 = _classThis;
})();
let ThermostatOperatingStateCC = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.commandClass)(import_core.CommandClasses["Thermostat Operating State"]), (0, import_CommandClassDecorators.implementedVersion)(2), (0, import_CommandClassDecorators.ccValues)(import_CCValues_generated.ThermostatOperatingStateCCValues)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = import_CommandClass.CommandClass;
  var ThermostatOperatingStateCC2 = class extends _classSuper {
    static {
      __name(this, "ThermostatOperatingStateCC");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      ThermostatOperatingStateCC2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    async interview(ctx) {
      const node = this.getNode(ctx);
      ctx.logNode(node.id, {
        endpoint: this.endpointIndex,
        message: `Interviewing ${this.ccName}...`,
        direction: "none"
      });
      await this.refreshValues(ctx);
      this.setInterviewComplete(ctx, true);
    }
    async refreshValues(ctx, options) {
      const node = this.getNode(ctx);
      const endpoint = this.getEndpoint(ctx);
      const api = import_API.CCAPI.create(import_core.CommandClasses["Thermostat Operating State"], ctx, endpoint).withOptions({
        priority: options?.priority ?? import_core.MessagePriority.NodeQuery
      });
      ctx.logNode(node.id, {
        endpoint: this.endpointIndex,
        message: "querying thermostat operating state...",
        direction: "outbound"
      });
      const state = await api.get();
      if (state != void 0) {
        ctx.logNode(node.id, {
          endpoint: this.endpointIndex,
          message: `received current thermostat operating state: ${(0, import_shared.getEnumMemberName)(import_Types.ThermostatOperatingState, state)}`,
          direction: "inbound"
        });
      }
    }
  };
  return ThermostatOperatingStateCC2 = _classThis;
})();
let ThermostatOperatingStateCCReport = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.ThermostatOperatingStateCommand.Report), (0, import_CommandClassDecorators.ccValueProperty)("state", import_CCValues_generated.ThermostatOperatingStateCCValues.operatingState)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = ThermostatOperatingStateCC;
  var ThermostatOperatingStateCCReport2 = class extends _classSuper {
    static {
      __name(this, "ThermostatOperatingStateCCReport");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      ThermostatOperatingStateCCReport2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    constructor(options) {
      super(options);
      this.state = options.state;
    }
    static from(raw, ctx) {
      (0, import_core.validatePayload)(raw.payload.length >= 1);
      const state = raw.payload[0];
      (0, import_core.validatePayload)(state <= import_Types.ThermostatOperatingState["3rd Stage Aux Heat"]);
      return new this({
        nodeId: ctx.sourceNodeId,
        state
      });
    }
    state;
    serialize(ctx) {
      this.payload = import_shared.Bytes.from([this.state]);
      return super.serialize(ctx);
    }
    toLogEntry(ctx) {
      return {
        ...super.toLogEntry(ctx),
        message: {
          state: (0, import_shared.getEnumMemberName)(import_Types.ThermostatOperatingState, this.state)
        }
      };
    }
  };
  return ThermostatOperatingStateCCReport2 = _classThis;
})();
let ThermostatOperatingStateCCGet = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.ThermostatOperatingStateCommand.Get), (0, import_CommandClassDecorators.expectedCCResponse)(ThermostatOperatingStateCCReport)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = ThermostatOperatingStateCC;
  var ThermostatOperatingStateCCGet2 = class extends _classSuper {
    static {
      __name(this, "ThermostatOperatingStateCCGet");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      ThermostatOperatingStateCCGet2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
  };
  return ThermostatOperatingStateCCGet2 = _classThis;
})();
let ThermostatOperatingStateCCLoggingSupportedReport = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.ThermostatOperatingStateCommand.LoggingSupportedReport)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = ThermostatOperatingStateCC;
  var ThermostatOperatingStateCCLoggingSupportedReport2 = class extends _classSuper {
    static {
      __name(this, "ThermostatOperatingStateCCLoggingSupportedReport");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      ThermostatOperatingStateCCLoggingSupportedReport2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    constructor(options) {
      super(options);
      this.supportedLoggingTypes = options.supportedLoggingTypes;
    }
    static from(raw, ctx) {
      const supportedLoggingTypes = (0, import_core.parseBitMask)(raw.payload, import_Types.ThermostatOperatingState["Idle"]).filter((state) => state !== import_Types.ThermostatOperatingState.Idle);
      return new this({
        nodeId: ctx.sourceNodeId,
        supportedLoggingTypes
      });
    }
    supportedLoggingTypes;
    serialize(ctx) {
      const supportedLoggingTypes = this.supportedLoggingTypes.filter((t) => t !== import_Types.ThermostatOperatingState.Idle);
      this.payload = (0, import_core.encodeBitMask)(supportedLoggingTypes, void 0, import_Types.ThermostatOperatingState["Idle"]);
      return super.serialize(ctx);
    }
    toLogEntry(ctx) {
      return {
        ...super.toLogEntry(ctx),
        message: {
          "supported logging types": this.supportedLoggingTypes.map((t) => `
\xB7 ${(0, import_shared.getEnumMemberName)(import_Types.ThermostatOperatingState, t)}`).join("")
        }
      };
    }
  };
  return ThermostatOperatingStateCCLoggingSupportedReport2 = _classThis;
})();
let ThermostatOperatingStateCCLoggingSupportedGet = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.ThermostatOperatingStateCommand.LoggingSupportedGet), (0, import_CommandClassDecorators.expectedCCResponse)(ThermostatOperatingStateCCLoggingSupportedReport)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = ThermostatOperatingStateCC;
  var ThermostatOperatingStateCCLoggingSupportedGet2 = class extends _classSuper {
    static {
      __name(this, "ThermostatOperatingStateCCLoggingSupportedGet");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      ThermostatOperatingStateCCLoggingSupportedGet2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
  };
  return ThermostatOperatingStateCCLoggingSupportedGet2 = _classThis;
})();
let ThermostatOperatingStateCCLoggingReport = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.ThermostatOperatingStateCommand.LoggingReport)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = ThermostatOperatingStateCC;
  var ThermostatOperatingStateCCLoggingReport2 = class extends _classSuper {
    static {
      __name(this, "ThermostatOperatingStateCCLoggingReport");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      ThermostatOperatingStateCCLoggingReport2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    constructor(options) {
      super(options);
      this.reportsToFollow = options.reportsToFollow;
      this.loggingData = options.loggingData;
    }
    static from(raw, ctx) {
      (0, import_core.validatePayload)(raw.payload.length >= 1);
      const reportsToFollow = raw.payload[0];
      const loggingData = /* @__PURE__ */ new Map();
      let offset = 1;
      while (offset + 5 <= raw.payload.length) {
        const state = raw.payload[offset++] & 15;
        loggingData.set(state, {
          usageTodayHours: raw.payload[offset++],
          usageTodayMinutes: raw.payload[offset++],
          usageYesterdayHours: raw.payload[offset++],
          usageYesterdayMinutes: raw.payload[offset++]
        });
      }
      return new this({
        nodeId: ctx.sourceNodeId,
        reportsToFollow,
        loggingData
      });
    }
    reportsToFollow;
    loggingData;
    getPartialCCSessionId() {
      return {};
    }
    expectMoreMessages() {
      return this.reportsToFollow > 0;
    }
    mergePartialCCs(partials, _ctx) {
      const merged = /* @__PURE__ */ new Map();
      for (const partial of partials) {
        for (const [state, data] of partial.loggingData) {
          merged.set(state, data);
        }
      }
      for (const [state, data] of this.loggingData) {
        merged.set(state, data);
      }
      this.loggingData = merged;
      return Promise.resolve();
    }
    serialize(ctx) {
      this.payload = new import_shared.Bytes(1 + this.loggingData.size * 5);
      this.payload[0] = this.reportsToFollow;
      let offset = 1;
      for (const [state, entry] of this.loggingData) {
        this.payload[offset++] = state & 15;
        this.payload[offset++] = entry.usageTodayHours;
        this.payload[offset++] = entry.usageTodayMinutes;
        this.payload[offset++] = entry.usageYesterdayHours;
        this.payload[offset++] = entry.usageYesterdayMinutes;
      }
      return super.serialize(ctx);
    }
    toLogEntry(ctx) {
      const message = {
        "reports to follow": this.reportsToFollow
      };
      for (const [state, entry] of this.loggingData) {
        const stateName = (0, import_shared.getEnumMemberName)(import_Types.ThermostatOperatingState, state);
        message[`${stateName} today`] = `${entry.usageTodayHours}h ${entry.usageTodayMinutes}m`;
        message[`${stateName} yesterday`] = `${entry.usageYesterdayHours}h ${entry.usageYesterdayMinutes}m`;
      }
      return {
        ...super.toLogEntry(ctx),
        message
      };
    }
  };
  return ThermostatOperatingStateCCLoggingReport2 = _classThis;
})();
let ThermostatOperatingStateCCLoggingGet = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.ThermostatOperatingStateCommand.LoggingGet), (0, import_CommandClassDecorators.expectedCCResponse)(ThermostatOperatingStateCCLoggingReport)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = ThermostatOperatingStateCC;
  var ThermostatOperatingStateCCLoggingGet2 = class extends _classSuper {
    static {
      __name(this, "ThermostatOperatingStateCCLoggingGet");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      ThermostatOperatingStateCCLoggingGet2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    constructor(options) {
      super(options);
      this.requestedStates = options.requestedStates;
    }
    static from(raw, ctx) {
      const requestedStates = (0, import_core.parseBitMask)(raw.payload, import_Types.ThermostatOperatingState["Idle"]);
      return new this({
        nodeId: ctx.sourceNodeId,
        requestedStates
      });
    }
    requestedStates;
    serialize(ctx) {
      this.payload = (0, import_core.encodeBitMask)(this.requestedStates, void 0, import_Types.ThermostatOperatingState["Idle"]);
      return super.serialize(ctx);
    }
    toLogEntry(ctx) {
      return {
        ...super.toLogEntry(ctx),
        message: {
          "requested states": this.requestedStates.map((t) => (0, import_shared.getEnumMemberName)(import_Types.ThermostatOperatingState, t)).join(", ")
        }
      };
    }
  };
  return ThermostatOperatingStateCCLoggingGet2 = _classThis;
})();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ThermostatOperatingStateCC,
  ThermostatOperatingStateCCAPI,
  ThermostatOperatingStateCCGet,
  ThermostatOperatingStateCCLoggingGet,
  ThermostatOperatingStateCCLoggingReport,
  ThermostatOperatingStateCCLoggingSupportedGet,
  ThermostatOperatingStateCCLoggingSupportedReport,
  ThermostatOperatingStateCCReport,
  ThermostatOperatingStateCCValues
});
//# sourceMappingURL=ThermostatOperatingStateCC.js.map
