"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 NodeNamingCC_exports = {};
__export(NodeNamingCC_exports, {
  NodeNamingAndLocationCC: () => NodeNamingAndLocationCC,
  NodeNamingAndLocationCCAPI: () => NodeNamingAndLocationCCAPI,
  NodeNamingAndLocationCCLocationGet: () => NodeNamingAndLocationCCLocationGet,
  NodeNamingAndLocationCCLocationReport: () => NodeNamingAndLocationCCLocationReport,
  NodeNamingAndLocationCCLocationSet: () => NodeNamingAndLocationCCLocationSet,
  NodeNamingAndLocationCCNameGet: () => NodeNamingAndLocationCCNameGet,
  NodeNamingAndLocationCCNameReport: () => NodeNamingAndLocationCCNameReport,
  NodeNamingAndLocationCCNameSet: () => NodeNamingAndLocationCCNameSet,
  NodeNamingAndLocationCCValues: () => import_CCValues_generated.NodeNamingAndLocationCCValues
});
module.exports = __toCommonJS(NodeNamingCC_exports);
var import_tslib = require("tslib");
var __validateArgs = __toESM(require("./NodeNamingCC._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_NodeNamingAndLocationCCAPI_setName, validateArgs_NodeNamingAndLocationCCAPI_setLocation } = __validateArgs;
function isASCII(str) {
  return /^[\x00-\x7F]*$/.test(str);
}
__name(isASCII, "isASCII");
function decodeNodeTextPayload(raw) {
  (0, import_core.validatePayload)(raw.payload.length >= 1);
  const encoding = raw.payload[0] === 2 ? "utf16le" : "ascii";
  const textBuffer = raw.payload.subarray(1);
  if (encoding === "utf16le") {
    (0, import_core.validatePayload)(textBuffer.length % 2 === 0);
    return (0, import_shared.uint8ArrayToStringUTF16BE)(textBuffer);
  }
  return textBuffer.toString("ascii");
}
__name(decodeNodeTextPayload, "decodeNodeTextPayload");
function encodeNodeTextPayload(text) {
  const encoding = isASCII(text) ? "ascii" : "utf16le";
  let textBuffer;
  if (encoding === "utf16le") {
    textBuffer = (0, import_shared.stringToUint8ArrayUTF16BE)(text);
  } else {
    textBuffer = import_shared.Bytes.from(text, "ascii");
  }
  const textLength = Math.min(16, textBuffer.length);
  return import_shared.Bytes.concat([
    [encoding === "ascii" ? 0 : 2],
    textBuffer.subarray(0, textLength)
  ]);
}
__name(encodeNodeTextPayload, "encodeNodeTextPayload");
let NodeNamingAndLocationCCAPI = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.API)(import_core.CommandClasses["Node Naming and Location"])];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = import_API.PhysicalCCAPI;
  let _instanceExtraInitializers = [];
  let _setName_decorators;
  let _setLocation_decorators;
  var NodeNamingAndLocationCCAPI2 = class extends _classSuper {
    static {
      __name(this, "NodeNamingAndLocationCCAPI");
    }
    static {
      _classThis = this;
    }
    static {
      const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
      _setName_decorators = [validateArgs_NodeNamingAndLocationCCAPI_setName()];
      _setLocation_decorators = [validateArgs_NodeNamingAndLocationCCAPI_setLocation()];
      (0, import_tslib.__esDecorate)(this, null, _setName_decorators, { kind: "method", name: "setName", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "setName" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.setName, "get") }, metadata: _metadata }, null, _instanceExtraInitializers);
      (0, import_tslib.__esDecorate)(this, null, _setLocation_decorators, { kind: "method", name: "setLocation", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "setLocation" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.setLocation, "get") }, metadata: _metadata }, null, _instanceExtraInitializers);
      (0, import_tslib.__esDecorate)(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
      NodeNamingAndLocationCCAPI2 = _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.NodeNamingAndLocationCommand.NameGet:
        case import_Types.NodeNamingAndLocationCommand.NameSet:
        case import_Types.NodeNamingAndLocationCommand.LocationGet:
        case import_Types.NodeNamingAndLocationCommand.LocationSet:
          return true;
      }
      return super.supportsCommand(cmd);
    }
    get [import_API.SET_VALUE]() {
      return async function({ property }, value) {
        if (property !== "name" && property !== "location") {
          (0, import_API.throwUnsupportedProperty)(this.ccId, property);
        }
        if (typeof value !== "string") {
          (0, import_API.throwWrongValueType)(this.ccId, property, "string", typeof value);
        }
        switch (property) {
          case "name":
            return this.setName(value);
          case "location":
            return this.setLocation(value);
        }
        return void 0;
      };
    }
    get [import_API.POLL_VALUE]() {
      return async function({ property }) {
        switch (property) {
          case "name":
            return this.getName();
          case "location":
            return this.getLocation();
          default:
            (0, import_API.throwUnsupportedProperty)(this.ccId, property);
        }
      };
    }
    async getName() {
      this.assertSupportsCommand(import_Types.NodeNamingAndLocationCommand, import_Types.NodeNamingAndLocationCommand.NameGet);
      const cc = new NodeNamingAndLocationCCNameGet({
        nodeId: this.endpoint.nodeId,
        endpointIndex: this.endpoint.index
      });
      const response = await this.host.sendCommand(cc, this.commandOptions);
      return response?.name;
    }
    async setName(name) {
      this.assertSupportsCommand(import_Types.NodeNamingAndLocationCommand, import_Types.NodeNamingAndLocationCommand.NameSet);
      const cc = new NodeNamingAndLocationCCNameSet({
        nodeId: this.endpoint.nodeId,
        endpointIndex: this.endpoint.index,
        name
      });
      return this.host.sendCommand(cc, this.commandOptions);
    }
    async getLocation() {
      this.assertSupportsCommand(import_Types.NodeNamingAndLocationCommand, import_Types.NodeNamingAndLocationCommand.LocationGet);
      const cc = new NodeNamingAndLocationCCLocationGet({
        nodeId: this.endpoint.nodeId,
        endpointIndex: this.endpoint.index
      });
      const response = await this.host.sendCommand(cc, this.commandOptions);
      return response?.location;
    }
    async setLocation(location) {
      this.assertSupportsCommand(import_Types.NodeNamingAndLocationCommand, import_Types.NodeNamingAndLocationCommand.LocationSet);
      const cc = new NodeNamingAndLocationCCLocationSet({
        nodeId: this.endpoint.nodeId,
        endpointIndex: this.endpoint.index,
        location
      });
      return this.host.sendCommand(cc, this.commandOptions);
    }
    constructor() {
      super(...arguments);
      (0, import_tslib.__runInitializers)(this, _instanceExtraInitializers);
    }
  };
  return NodeNamingAndLocationCCAPI2 = _classThis;
})();
let NodeNamingAndLocationCC = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.commandClass)(import_core.CommandClasses["Node Naming and Location"]), (0, import_CommandClassDecorators.implementedVersion)(1), (0, import_CommandClassDecorators.ccValues)(import_CCValues_generated.NodeNamingAndLocationCCValues)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = import_CommandClass.CommandClass;
  var NodeNamingAndLocationCC2 = class extends _classSuper {
    static {
      __name(this, "NodeNamingAndLocationCC");
    }
    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);
      NodeNamingAndLocationCC2 = _classThis = _classDescriptor.value;
      if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
      (0, import_tslib.__runInitializers)(_classThis, _classExtraInitializers);
    }
    skipEndpointInterview() {
      return true;
    }
    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["Node Naming and Location"], ctx, endpoint).withOptions({
        priority: options?.priority ?? import_core.MessagePriority.NodeQuery
      });
      ctx.logNode(node.id, {
        message: "retrieving node name...",
        direction: "outbound"
      });
      const name = await api.getName();
      if (name != void 0) {
        ctx.logNode(node.id, {
          message: `is named "${name}"`,
          direction: "inbound"
        });
      }
      ctx.logNode(node.id, {
        message: "retrieving node location...",
        direction: "outbound"
      });
      const location = await api.getLocation();
      if (location != void 0) {
        ctx.logNode(node.id, {
          message: `received location: ${location}`,
          direction: "inbound"
        });
      }
    }
  };
  return NodeNamingAndLocationCC2 = _classThis;
})();
let NodeNamingAndLocationCCNameSet = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.NodeNamingAndLocationCommand.NameSet), (0, import_CommandClassDecorators.useSupervision)()];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = NodeNamingAndLocationCC;
  var NodeNamingAndLocationCCNameSet2 = class extends _classSuper {
    static {
      __name(this, "NodeNamingAndLocationCCNameSet");
    }
    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);
      NodeNamingAndLocationCCNameSet2 = _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.name = options.name;
    }
    static from(raw, ctx) {
      const name = decodeNodeTextPayload(raw);
      return new this({
        nodeId: ctx.sourceNodeId,
        name
      });
    }
    name;
    serialize(ctx) {
      this.payload = encodeNodeTextPayload(this.name);
      return super.serialize(ctx);
    }
    toLogEntry(ctx) {
      return {
        ...super.toLogEntry(ctx),
        message: { name: this.name }
      };
    }
  };
  return NodeNamingAndLocationCCNameSet2 = _classThis;
})();
let NodeNamingAndLocationCCNameReport = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.NodeNamingAndLocationCommand.NameReport), (0, import_CommandClassDecorators.ccValueProperty)("name", import_CCValues_generated.NodeNamingAndLocationCCValues.name)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = NodeNamingAndLocationCC;
  var NodeNamingAndLocationCCNameReport2 = class extends _classSuper {
    static {
      __name(this, "NodeNamingAndLocationCCNameReport");
    }
    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);
      NodeNamingAndLocationCCNameReport2 = _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.name = options.name;
    }
    static from(raw, ctx) {
      (0, import_core.validatePayload)(raw.payload.length >= 1);
      const encoding = raw.payload[0] === 2 ? "utf16le" : "ascii";
      const nameBuffer = raw.payload.subarray(1);
      let name;
      if (encoding === "utf16le") {
        (0, import_core.validatePayload)(nameBuffer.length % 2 === 0);
        name = (0, import_shared.uint8ArrayToStringUTF16BE)(nameBuffer);
      } else {
        name = nameBuffer.toString("ascii");
      }
      return new this({
        nodeId: ctx.sourceNodeId,
        name
      });
    }
    name;
    serialize(ctx) {
      this.payload = encodeNodeTextPayload(this.name);
      return super.serialize(ctx);
    }
    toLogEntry(ctx) {
      return {
        ...super.toLogEntry(ctx),
        message: { name: this.name }
      };
    }
  };
  return NodeNamingAndLocationCCNameReport2 = _classThis;
})();
let NodeNamingAndLocationCCNameGet = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.NodeNamingAndLocationCommand.NameGet), (0, import_CommandClassDecorators.expectedCCResponse)(NodeNamingAndLocationCCNameReport)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = NodeNamingAndLocationCC;
  var NodeNamingAndLocationCCNameGet2 = class extends _classSuper {
    static {
      __name(this, "NodeNamingAndLocationCCNameGet");
    }
    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);
      NodeNamingAndLocationCCNameGet2 = _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 NodeNamingAndLocationCCNameGet2 = _classThis;
})();
let NodeNamingAndLocationCCLocationSet = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.NodeNamingAndLocationCommand.LocationSet), (0, import_CommandClassDecorators.useSupervision)()];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = NodeNamingAndLocationCC;
  var NodeNamingAndLocationCCLocationSet2 = class extends _classSuper {
    static {
      __name(this, "NodeNamingAndLocationCCLocationSet");
    }
    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);
      NodeNamingAndLocationCCLocationSet2 = _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.location = options.location;
    }
    static from(raw, ctx) {
      const location = decodeNodeTextPayload(raw);
      return new this({
        nodeId: ctx.sourceNodeId,
        location
      });
    }
    location;
    serialize(ctx) {
      this.payload = encodeNodeTextPayload(this.location);
      return super.serialize(ctx);
    }
    toLogEntry(ctx) {
      return {
        ...super.toLogEntry(ctx),
        message: { location: this.location }
      };
    }
  };
  return NodeNamingAndLocationCCLocationSet2 = _classThis;
})();
let NodeNamingAndLocationCCLocationReport = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.NodeNamingAndLocationCommand.LocationReport), (0, import_CommandClassDecorators.ccValueProperty)("location", import_CCValues_generated.NodeNamingAndLocationCCValues.location)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = NodeNamingAndLocationCC;
  var NodeNamingAndLocationCCLocationReport2 = class extends _classSuper {
    static {
      __name(this, "NodeNamingAndLocationCCLocationReport");
    }
    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);
      NodeNamingAndLocationCCLocationReport2 = _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.location = options.location;
    }
    static from(raw, ctx) {
      (0, import_core.validatePayload)(raw.payload.length >= 1);
      const encoding = raw.payload[0] === 2 ? "utf16le" : "ascii";
      const locationBuffer = raw.payload.subarray(1);
      let location;
      if (encoding === "utf16le") {
        (0, import_core.validatePayload)(locationBuffer.length % 2 === 0);
        location = (0, import_shared.uint8ArrayToStringUTF16BE)(locationBuffer);
      } else {
        location = locationBuffer.toString("ascii");
      }
      return new this({
        nodeId: ctx.sourceNodeId,
        location
      });
    }
    location;
    serialize(ctx) {
      this.payload = encodeNodeTextPayload(this.location);
      return super.serialize(ctx);
    }
    toLogEntry(ctx) {
      return {
        ...super.toLogEntry(ctx),
        message: { location: this.location }
      };
    }
  };
  return NodeNamingAndLocationCCLocationReport2 = _classThis;
})();
let NodeNamingAndLocationCCLocationGet = (() => {
  let _classDecorators = [(0, import_CommandClassDecorators.CCCommand)(import_Types.NodeNamingAndLocationCommand.LocationGet), (0, import_CommandClassDecorators.expectedCCResponse)(NodeNamingAndLocationCCLocationReport)];
  let _classDescriptor;
  let _classExtraInitializers = [];
  let _classThis;
  let _classSuper = NodeNamingAndLocationCC;
  var NodeNamingAndLocationCCLocationGet2 = class extends _classSuper {
    static {
      __name(this, "NodeNamingAndLocationCCLocationGet");
    }
    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);
      NodeNamingAndLocationCCLocationGet2 = _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 NodeNamingAndLocationCCLocationGet2 = _classThis;
})();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  NodeNamingAndLocationCC,
  NodeNamingAndLocationCCAPI,
  NodeNamingAndLocationCCLocationGet,
  NodeNamingAndLocationCCLocationReport,
  NodeNamingAndLocationCCLocationSet,
  NodeNamingAndLocationCCNameGet,
  NodeNamingAndLocationCCNameReport,
  NodeNamingAndLocationCCNameSet,
  NodeNamingAndLocationCCValues
});
//# sourceMappingURL=NodeNamingCC.js.map
