"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 UserCredentialCC_exports = {};
__export(UserCredentialCC_exports, {
  handleUserCredentialAssociationReport: () => handleUserCredentialAssociationReport,
  handleUserCredentialCredentialLearnReport: () => handleUserCredentialCredentialLearnReport,
  handleUserCredentialCredentialReport: () => handleUserCredentialCredentialReport,
  handleUserCredentialUserReport: () => handleUserCredentialUserReport
});
module.exports = __toCommonJS(UserCredentialCC_exports);
var import_cc = require("@zwave-js/cc");
var import_UserCredentialCC = require("@zwave-js/cc/UserCredentialCC");
function buildUserArgs(report) {
  return {
    userId: report.userId,
    active: report.active,
    userType: report.userType,
    userName: report.userName || void 0,
    credentialRule: report.credentialRule,
    expiringTimeoutMinutes: report.expiringTimeoutMinutes || void 0
  };
}
__name(buildUserArgs, "buildUserArgs");
function handleUserCredentialUserReport(node, report) {
  const endpoint = node.getEndpoint(report.endpointIndex) ?? node;
  switch (report.reportType) {
    case import_cc.UserCredentialUserReportType.UserAdded:
    // The device rejected an Add because the slot was already occupied,
    // but reported the actual user data. Notify applications about the
    // previously-unknown user.
    case import_cc.UserCredentialUserReportType.UserAddRejectedLocationOccupied:
      node.emit("user added", endpoint, buildUserArgs(report));
      break;
    case import_cc.UserCredentialUserReportType.UserModified:
      node.emit("user modified", endpoint, buildUserArgs(report));
      break;
    case import_cc.UserCredentialUserReportType.UserDeleted:
    // The device rejected a Modify because the slot was empty, meaning
    // our cache was stale. Notify applications that the user is gone.
    case import_cc.UserCredentialUserReportType.UserModifyRejectedLocationEmpty:
      node.emit("user deleted", endpoint, {
        userId: report.userId
      });
      break;
  }
}
__name(handleUserCredentialUserReport, "handleUserCredentialUserReport");
function handleUserCredentialCredentialReport(node, report) {
  const endpoint = node.getEndpoint(report.endpointIndex) ?? node;
  const normalizedData = report.credentialData ? (0, import_UserCredentialCC.normalizeCredentialData)(report.credentialType, report.credentialData) : void 0;
  switch (report.reportType) {
    case import_cc.UserCredentialCredentialReportType.CredentialAdded:
      node.emit("credential added", endpoint, {
        userId: report.userId,
        credentialType: report.credentialType,
        credentialSlot: report.credentialSlot,
        data: normalizedData
      });
      break;
    case import_cc.UserCredentialCredentialReportType.CredentialModified:
      node.emit("credential modified", endpoint, {
        userId: report.userId,
        credentialType: report.credentialType,
        credentialSlot: report.credentialSlot,
        data: normalizedData
      });
      break;
    case import_cc.UserCredentialCredentialReportType.CredentialDeleted:
    // The device rejected a Modify because the slot was empty, meaning
    // our cache was stale. Notify applications that the credential is gone.
    case import_cc.UserCredentialCredentialReportType.CredentialModifyRejectedLocationEmpty:
      node.emit("credential deleted", endpoint, {
        userId: report.userId,
        credentialType: report.credentialType,
        credentialSlot: report.credentialSlot
      });
      break;
    case import_cc.UserCredentialCredentialReportType.CredentialAddRejectedLocationOccupied:
      if (report.credentialReadBack) {
        node.emit("credential added", endpoint, {
          userId: report.userId,
          credentialType: report.credentialType,
          credentialSlot: report.credentialSlot,
          data: normalizedData
        });
      }
      break;
  }
}
__name(handleUserCredentialCredentialReport, "handleUserCredentialCredentialReport");
function handleUserCredentialAssociationReport(node, report) {
  if (report.status !== 0)
    return;
  const endpoint = node.getEndpoint(report.endpointIndex) ?? node;
  node.emit("credential modified", endpoint, {
    userId: report.destinationUserId,
    credentialType: report.credentialType,
    credentialSlot: report.credentialSlot
  });
}
__name(handleUserCredentialAssociationReport, "handleUserCredentialAssociationReport");
function handleUserCredentialCredentialLearnReport(node, report) {
  const endpoint = node.getEndpoint(report.endpointIndex) ?? node;
  if (report.stepsRemaining > 0) {
    node.emit("credential learn progress", endpoint, {
      userId: report.userId,
      credentialType: report.credentialType,
      credentialSlot: report.credentialSlot,
      stepsRemaining: report.stepsRemaining,
      status: report.learnStatus
    });
  } else {
    node.emit("credential learn completed", endpoint, {
      userId: report.userId,
      credentialType: report.credentialType,
      credentialSlot: report.credentialSlot,
      status: report.learnStatus,
      success: report.learnStatus === import_cc.UserCredentialLearnStatus.Success
    });
  }
}
__name(handleUserCredentialCredentialLearnReport, "handleUserCredentialCredentialLearnReport");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  handleUserCredentialAssociationReport,
  handleUserCredentialCredentialLearnReport,
  handleUserCredentialCredentialReport,
  handleUserCredentialUserReport
});
//# sourceMappingURL=UserCredentialCC.js.map
