"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 StorageDriver_exports = {};
__export(StorageDriver_exports, {
  CloneableStorage: () => CloneableStorage,
  FilesystemStorageDriver: () => FilesystemStorageDriver,
  Storage: () => Storage,
  StorageCommitError: () => StorageCommitError,
  StorageDriver: () => StorageDriver,
  StorageError: () => StorageError,
  StorageLockError: () => StorageLockError
});
module.exports = __toCommonJS(StorageDriver_exports);
var import_MatterError = require("../MatterError.js");
var import_Promises = require("../util/Promises.js");
var import_BaseStorageDriver = require("./BaseStorageDriver.js");
var import_DatafileRoot = require("./DatafileRoot.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class StorageError extends import_MatterError.MatterError {
}
class StorageCommitError extends StorageError {
}
class StorageLockError extends StorageError {
}
class StorageDriver extends import_BaseStorageDriver.BaseStorageDriver {
  type = "kv";
  /** @deprecated Use {@link clearAll} instead. */
  clear(_completely) {
    throw new StorageError("clear() is deprecated; use clearAll() instead");
  }
  /**
   * Checks if a key exists in the storage for the given contexts.
   * Important Note: This default implementation just reads the value for the key and checks if it is undefined.
   * Please implement this method in your storage implementation if you want to optimize it.
   */
  has(contexts, key) {
    const value = this.get(contexts, key);
    if (import_Promises.MaybePromise.is(value)) {
      return import_Promises.MaybePromise.then(value, (v) => v !== void 0);
    }
    return value !== void 0;
  }
  begin() {
    return new StorageDriver.Transaction(this);
  }
}
((StorageDriver2) => {
  StorageDriver2.RESERVED_FILENAMES = import_BaseStorageDriver.BaseStorageDriver.RESERVED_FILENAMES;
  class Transaction extends StorageDriver2 {
    constructor(storage) {
      super();
      this.storage = storage;
    }
    storage;
    #committed = false;
    #disposed = false;
    get committed() {
      return this.#committed;
    }
    get disposed() {
      return this.#disposed;
    }
    assertActive() {
      if (this.#disposed) {
        throw new StorageCommitError("Transaction is disposed");
      }
      if (this.#committed) {
        throw new StorageCommitError("Transaction is already committed");
      }
    }
    commit() {
      this.assertActive();
      this.#committed = true;
    }
    rollback() {
    }
    [Symbol.dispose]() {
      if (this.#disposed) {
        return;
      }
      this.#disposed = true;
      if (!this.#committed) {
        this.rollback();
      }
    }
    async [Symbol.asyncDispose]() {
      if (this.#disposed) {
        return;
      }
      this.#disposed = true;
      if (!this.#committed) {
        await this.rollback();
      }
    }
    // Storage abstract method implementations — delegate to wrapped storage
    get initialized() {
      return this.storage.initialized;
    }
    initialize() {
      throw new StorageCommitError("Cannot initialize storage from a transaction");
    }
    close() {
      throw new StorageCommitError("Cannot close storage from a transaction");
    }
    begin() {
      throw new StorageCommitError("Nested transactions are not supported");
    }
    get(contexts, key) {
      return this.storage.get(contexts, key);
    }
    set(contexts, keyOrValues, value) {
      if (typeof keyOrValues === "string") {
        return this.storage.set(contexts, keyOrValues, value);
      }
      return this.storage.set(contexts, keyOrValues);
    }
    delete(contexts, key) {
      return this.storage.delete(contexts, key);
    }
    keys(contexts) {
      return this.storage.keys(contexts);
    }
    values(contexts) {
      return this.storage.values(contexts);
    }
    contexts(contexts) {
      return this.storage.contexts(contexts);
    }
    clearAll(contexts) {
      return this.storage.clearAll(contexts);
    }
    /** @deprecated Use {@link clearAll} instead. */
    clear(_completely) {
      throw new StorageError("clear() is deprecated; use clearAll() instead");
    }
  }
  StorageDriver2.Transaction = Transaction;
})(StorageDriver || (StorageDriver = {}));
class FilesystemStorageDriver extends StorageDriver {
  #root;
  #lock;
  constructor(namespace) {
    super();
    if (namespace !== void 0) {
      if (!(namespace instanceof import_DatafileRoot.DatafileRoot)) {
        throw new import_MatterError.ImplementationError("Filesystem storage driver requires a DatafileRoot namespace");
      }
      this.#root = namespace;
    }
  }
  get root() {
    return this.#root;
  }
  async initialize() {
    if (this.#lock) {
      throw new import_MatterError.ImplementationError("Filesystem storage driver is already initialized");
    }
    if (this.#root) {
      this.#lock = await this.#root.lock();
    }
  }
  async close() {
    await this.#lock?.close();
    this.#lock = void 0;
  }
}
const Storage = StorageDriver;
var CloneableStorage;
((CloneableStorage2) => {
  function is(storage) {
    return "clone" in storage && typeof storage.clone === "function";
  }
  CloneableStorage2.is = is;
  function assert(storage) {
    if (!is(storage)) {
      throw new import_MatterError.ImplementationError("Storage does not support required snapshotting function");
    }
  }
  CloneableStorage2.assert = assert;
})(CloneableStorage || (CloneableStorage = {}));
//# sourceMappingURL=StorageDriver.js.map
