var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : /* @__PURE__ */ Symbol.for("Symbol." + name);
var __typeError = (msg) => {
  throw TypeError(msg);
};
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __decoratorStart = (base) => [, , , __create(base?.[__knownSymbol("metadata")] ?? null)];
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
var __runInitializers = (array, flags, self, value) => {
  for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
  return value;
};
var __decorateElement = (array, flags, name, decorators, target, extra) => {
  var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
  var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
  var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
  var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
    return __privateGet(this, extra);
  }, set [name](x) {
    return __privateSet(this, extra, x);
  } }, name));
  k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
  for (var i = decorators.length - 1; i >= 0; i--) {
    ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
    if (k) {
      ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
      if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
      if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
    }
    it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
    if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
    else if (typeof it !== "object" || it === null) __typeError("Object expected");
    else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
  }
  return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
};
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Bytes } from "@matter/general";
import { fabricIdx, field, listOf, nullable, octstr, string, uint16, uint32, uint8 } from "@matter/model";
import { DoorLock } from "@matter/types/clusters/door-lock";
const CredentialType = DoorLock.CredentialType;
const LockDataType = DoorLock.LockDataType;
var LockAuth;
((LockAuth2) => {
  var _credentialIndex_dec, _credentialType_dec, _init, _lastModifiedFabricIndex_dec, _creatorFabricIndex_dec, _credentials_dec, _credentialRule_dec, _userType_dec, _userStatus_dec, _userUniqueId_dec, _userName_dec, _userIndex_dec, _init2, _lastModifiedFabricIndex_dec2, _creatorFabricIndex_dec2, _credentialData_dec, _credentialIndex_dec2, _credentialType_dec2, _init3;
  _credentialType_dec = [field(uint8)], _credentialIndex_dec = [field(uint16)];
  class CredentialRef {
    constructor() {
      __publicField(this, "credentialType", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
      __publicField(this, "credentialIndex", __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
    }
  }
  _init = __decoratorStart(null);
  __decorateElement(_init, 5, "credentialType", _credentialType_dec, CredentialRef);
  __decorateElement(_init, 5, "credentialIndex", _credentialIndex_dec, CredentialRef);
  __decoratorMetadata(_init, CredentialRef);
  LockAuth2.CredentialRef = CredentialRef;
  _userIndex_dec = [field(uint16)], _userName_dec = [field(string)], _userUniqueId_dec = [nullable, field(uint32)], _userStatus_dec = [field(uint8)], _userType_dec = [field(uint8)], _credentialRule_dec = [field(uint8)], _credentials_dec = [field(listOf(CredentialRef))], _creatorFabricIndex_dec = [field(fabricIdx)], _lastModifiedFabricIndex_dec = [field(fabricIdx)];
  class User {
    constructor() {
      __publicField(this, "userIndex", __runInitializers(_init2, 8, this)), __runInitializers(_init2, 11, this);
      __publicField(this, "userName", __runInitializers(_init2, 12, this, "")), __runInitializers(_init2, 15, this);
      __publicField(this, "userUniqueId", __runInitializers(_init2, 16, this, null)), __runInitializers(_init2, 19, this);
      __publicField(this, "userStatus", __runInitializers(_init2, 20, this, DoorLock.UserStatus.Available)), __runInitializers(_init2, 23, this);
      __publicField(this, "userType", __runInitializers(_init2, 24, this, DoorLock.UserType.UnrestrictedUser)), __runInitializers(_init2, 27, this);
      __publicField(this, "credentialRule", __runInitializers(_init2, 28, this, DoorLock.CredentialRule.Single)), __runInitializers(_init2, 31, this);
      __publicField(this, "credentials", __runInitializers(_init2, 32, this, [])), __runInitializers(_init2, 35, this);
      __publicField(this, "creatorFabricIndex", __runInitializers(_init2, 36, this)), __runInitializers(_init2, 39, this);
      __publicField(this, "lastModifiedFabricIndex", __runInitializers(_init2, 40, this)), __runInitializers(_init2, 43, this);
    }
  }
  _init2 = __decoratorStart(null);
  __decorateElement(_init2, 5, "userIndex", _userIndex_dec, User);
  __decorateElement(_init2, 5, "userName", _userName_dec, User);
  __decorateElement(_init2, 5, "userUniqueId", _userUniqueId_dec, User);
  __decorateElement(_init2, 5, "userStatus", _userStatus_dec, User);
  __decorateElement(_init2, 5, "userType", _userType_dec, User);
  __decorateElement(_init2, 5, "credentialRule", _credentialRule_dec, User);
  __decorateElement(_init2, 5, "credentials", _credentials_dec, User);
  __decorateElement(_init2, 5, "creatorFabricIndex", _creatorFabricIndex_dec, User);
  __decorateElement(_init2, 5, "lastModifiedFabricIndex", _lastModifiedFabricIndex_dec, User);
  __decoratorMetadata(_init2, User);
  LockAuth2.User = User;
  _credentialType_dec2 = [field(uint8)], _credentialIndex_dec2 = [field(uint16)], _credentialData_dec = [field(octstr)], _creatorFabricIndex_dec2 = [field(fabricIdx)], _lastModifiedFabricIndex_dec2 = [field(fabricIdx)];
  class Credential {
    constructor() {
      __publicField(this, "credentialType", __runInitializers(_init3, 8, this)), __runInitializers(_init3, 11, this);
      __publicField(this, "credentialIndex", __runInitializers(_init3, 12, this)), __runInitializers(_init3, 15, this);
      __publicField(this, "credentialData", __runInitializers(_init3, 16, this)), __runInitializers(_init3, 19, this);
      __publicField(this, "creatorFabricIndex", __runInitializers(_init3, 20, this)), __runInitializers(_init3, 23, this);
      __publicField(this, "lastModifiedFabricIndex", __runInitializers(_init3, 24, this)), __runInitializers(_init3, 27, this);
    }
  }
  _init3 = __decoratorStart(null);
  __decorateElement(_init3, 5, "credentialType", _credentialType_dec2, Credential);
  __decorateElement(_init3, 5, "credentialIndex", _credentialIndex_dec2, Credential);
  __decorateElement(_init3, 5, "credentialData", _credentialData_dec, Credential);
  __decorateElement(_init3, 5, "creatorFabricIndex", _creatorFabricIndex_dec2, Credential);
  __decorateElement(_init3, 5, "lastModifiedFabricIndex", _lastModifiedFabricIndex_dec2, Credential);
  __decoratorMetadata(_init3, Credential);
  LockAuth2.Credential = Credential;
  function credentialTypeToLockDataType(type) {
    switch (type) {
      case CredentialType.ProgrammingPin:
        return LockDataType.ProgrammingCode;
      case CredentialType.Pin:
        return LockDataType.Pin;
      case CredentialType.Rfid:
        return LockDataType.Rfid;
      case CredentialType.Fingerprint:
        return LockDataType.Fingerprint;
      case CredentialType.FingerVein:
        return LockDataType.FingerVein;
      case CredentialType.Face:
        return LockDataType.Face;
      default:
        return LockDataType.Unspecified;
    }
  }
  LockAuth2.credentialTypeToLockDataType = credentialTypeToLockDataType;
  class Store {
    #state;
    #cipher;
    constructor(state, cipher) {
      this.#state = state;
      this.#cipher = cipher;
    }
    // ── User queries ──
    findUser(userIndex) {
      return this.#state.users.find((u) => u.userIndex === userIndex);
    }
    findNextOccupiedUserIndex(afterIndex) {
      let next = null;
      for (const u of this.#state.users) {
        if (u.userIndex > afterIndex && (next === null || u.userIndex < next)) {
          next = u.userIndex;
        }
      }
      return next;
    }
    findAvailableUserIndex(maxUsers) {
      const occupied = new Set(this.#state.users.map((u) => u.userIndex));
      for (let i = 1; i <= maxUsers; i++) {
        if (!occupied.has(i)) return i;
      }
      return null;
    }
    findUserIndexForCredential(type, index) {
      for (const user of this.#state.users) {
        if (user.credentials.some((c) => c.credentialType === type && c.credentialIndex === index)) {
          return user.userIndex;
        }
      }
      return null;
    }
    // ── User mutations ──
    addUser(user) {
      this.#state.users = [...this.#state.users, user];
    }
    replaceUser(userIndex, user) {
      this.#state.users = this.#state.users.map((u) => u.userIndex === userIndex ? user : u);
    }
    removeUser(userIndex) {
      this.#state.users = this.#state.users.filter((u) => u.userIndex !== userIndex);
    }
    clearUsers() {
      this.#state.users = [];
    }
    // ── Credential queries ──
    findCredential(type, index) {
      return this.#state.credentials.find((c) => c.credentialType === type && c.credentialIndex === index);
    }
    findNextAvailableCredentialIndex(type, afterIndex, max) {
      const occupied = new Set(
        this.#state.credentials.filter((c) => c.credentialType === type).map((c) => c.credentialIndex)
      );
      for (let i = afterIndex + 1; i <= max; i++) {
        if (!occupied.has(i)) return i;
      }
      return null;
    }
    findNextOccupiedCredentialIndex(type, afterIndex) {
      let next = null;
      for (const c of this.#state.credentials) {
        if (c.credentialType === type && c.credentialIndex > afterIndex) {
          if (next === null || c.credentialIndex < next) {
            next = c.credentialIndex;
          }
        }
      }
      return next;
    }
    isDuplicateCredential(type, data, excludeIndex) {
      for (const cred of this.#state.credentials) {
        if (cred.credentialType !== type) continue;
        if (excludeIndex !== void 0 && cred.credentialIndex === excludeIndex) continue;
        const decrypted = this.#cipher.decrypt(cred.credentialData);
        if (Bytes.areEqual(decrypted, data)) return true;
      }
      return false;
    }
    // ── Credential mutations (encrypt/decrypt handled internally) ──
    addCredential(type, index, data, fabricIndex) {
      const credRecord = {
        credentialType: type,
        credentialIndex: index,
        credentialData: this.#cipher.encrypt(data),
        creatorFabricIndex: fabricIndex,
        lastModifiedFabricIndex: fabricIndex
      };
      this.#state.credentials = [...this.#state.credentials, credRecord];
    }
    updateCredentialData(type, index, data, fabricIndex) {
      const encrypted = this.#cipher.encrypt(data);
      this.#state.credentials = this.#state.credentials.map(
        (c) => c.credentialType === type && c.credentialIndex === index ? { ...c, credentialData: encrypted, lastModifiedFabricIndex: fabricIndex } : c
      );
    }
    removeCredential(type, index) {
      this.#state.credentials = this.#state.credentials.filter(
        (c) => !(c.credentialType === type && c.credentialIndex === index)
      );
    }
    removeAllCredentialsOfType(type) {
      this.#state.credentials = this.#state.credentials.filter((c) => c.credentialType !== type);
    }
    // ── Direct crypto (for PIN validation, getNonUserCode responses) ──
    encrypt(data) {
      return this.#cipher.encrypt(data);
    }
    decrypt(data) {
      return this.#cipher.decrypt(data);
    }
    // ── State access (for iteration in PIN validation, etc.) ──
    get users() {
      return this.#state.users;
    }
    get credentials() {
      return this.#state.credentials;
    }
  }
  LockAuth2.Store = Store;
})(LockAuth || (LockAuth = {}));
export {
  LockAuth
};
//# sourceMappingURL=LockAuth.js.map
