"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 StorageContext_exports = {};
__export(StorageContext_exports, {
  StorageContext: () => StorageContext
});
module.exports = __toCommonJS(StorageContext_exports);
var import_MatterError = require("#MatterError.js");
var import_Promises = require("../util/Promises.js");
var import_Storage = require("./Storage.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class StorageContext {
  constructor(storage, thisContexts) {
    this.storage = storage;
    this.thisContexts = thisContexts;
  }
  get(key, defaultValue) {
    const value = this.storage.get(this.thisContexts, key);
    if (value !== void 0) {
      if (import_Promises.MaybePromise.is(value)) {
        return value.then((v) => {
          if (v !== void 0) return v;
          if (defaultValue === void 0) {
            throw new import_Storage.StorageError(
              `No value found for key ${key} in context ${this.thisContexts} and no default value specified`
            );
          }
          return defaultValue;
        });
      }
      return value;
    }
    if (defaultValue === void 0) {
      throw new import_Storage.StorageError(
        `No value found for key ${key} in context ${this.thisContexts} and no default value specified`
      );
    }
    return defaultValue;
  }
  has(key) {
    return this.storage.has(this.thisContexts, key);
  }
  set(keyOrValues, value) {
    if (typeof keyOrValues === "string") {
      if (keyOrValues.includes(".")) {
        throw new import_Storage.StorageError("Storage keys must not contain dots");
      }
      return this.storage.set(this.thisContexts, keyOrValues, value);
    } else if (keyOrValues === null || typeof keyOrValues !== "object") {
      throw new import_Storage.StorageError("Storage keys must be string or object ok string-value pairs");
    }
    for (const key of Object.keys(keyOrValues)) {
      if (!key) {
        throw new import_Storage.StorageError("Storage keys can not be empty");
      }
      if (key.includes(".")) {
        throw new import_Storage.StorageError("Storage keys must not contain dots");
      }
    }
    return this.storage.set(this.thisContexts, keyOrValues);
  }
  delete(key) {
    return this.storage.delete(this.thisContexts, key);
  }
  createContext(context) {
    if (context.length === 0) throw new import_Storage.StorageError("Context must not be an empty string");
    if (context.includes(".")) throw new import_Storage.StorageError("Context must not contain dots");
    return new StorageContext(this.storage, [...this.thisContexts, context]);
  }
  keys() {
    return this.storage.keys(this.thisContexts);
  }
  values() {
    return this.storage.values(this.thisContexts);
  }
  contexts() {
    return this.storage.contexts(this.thisContexts);
  }
  /** Clears all keys in this context */
  clear() {
    const keys = this.keys();
    if (import_Promises.MaybePromise.is(keys)) {
      return keys.then((keys2) => {
        return import_MatterError.MatterAggregateError.allSettled(
          keys2.map((key) => this.delete(key)),
          "Error while clearing storage"
        ).then(() => {
        });
      });
    }
    const promises = new Array();
    keys.forEach((key) => {
      const promise = this.delete(key);
      if (promise !== void 0 && import_Promises.MaybePromise.is(promise)) {
        promises.push(promise);
      }
    });
    if (promises.length > 0) {
      return import_MatterError.MatterAggregateError.allSettled(promises, "Error while clearing storage").then(() => {
      });
    }
    return void 0;
  }
  /** Clears all keys in this context and all created sub-contexts. */
  clearAll() {
    return this.storage.clearAll(this.thisContexts);
  }
  openBlob(key) {
    return this.storage.openBlob(this.thisContexts, key);
  }
  writeBlobFromStream(key, stream) {
    if (!key) {
      throw new import_Storage.StorageError("Storage keys can not be empty");
    }
    if (key.includes(".")) {
      throw new import_Storage.StorageError("Storage keys must not contain dots");
    }
    return this.storage.writeBlobFromStream(this.thisContexts, key, stream);
  }
}
//# sourceMappingURL=StorageContext.js.map
