/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Bytes } from "#util/Bytes.js";
import { BlobStorageDriver } from "./BlobStorageDriver.js";
import { StorageError } from "./StorageDriver.js";
class MemoryBlobStorageDriver extends BlobStorageDriver {
  static id = "memory-blob";
  #store = /* @__PURE__ */ new Map();
  get initialized() {
    return true;
  }
  initialize() {
  }
  close() {
    this.#store.clear();
  }
  #createContextKey(contexts) {
    for (const ctx of contexts) {
      if (!ctx.length || ctx.includes(".")) {
        throw new StorageError("Context must not contain empty segments or leading or trailing dots.");
      }
    }
    return contexts.join(".");
  }
  #contextMap(contexts) {
    return this.#store.get(this.#createContextKey(contexts));
  }
  #ensureContextMap(contexts) {
    const key = this.#createContextKey(contexts);
    let map = this.#store.get(key);
    if (map === void 0) {
      map = /* @__PURE__ */ new Map();
      this.#store.set(key, map);
    }
    return map;
  }
  openBlob(contexts, key) {
    const map = this.#contextMap(contexts);
    if (map === void 0) {
      return new Blob([]);
    }
    const value = map.get(key);
    if (value === void 0) {
      return new Blob([]);
    }
    if (!Bytes.isBytes(value)) {
      throw new StorageError("Value must be Bytes to read as blob.");
    }
    return new Blob([Bytes.exclusive(value)]);
  }
  async writeBlobFromStream(contexts, key, stream) {
    const bytes = new Uint8Array(await new Response(stream).arrayBuffer());
    this.#ensureContextMap(contexts).set(key, bytes);
  }
  /**
   * Synchronously set bytes for a key, bypassing stream-based write.
   * Useful in tests where async stream operations may not work with MockTime.
   */
  setBytes(contexts, key, data) {
    this.#ensureContextMap(contexts).set(key, Bytes.of(data));
  }
  /**
   * Synchronously get bytes for a key.
   * Useful in tests where async blob reading may not work with MockTime.
   */
  getBytes(contexts, key) {
    return this.#contextMap(contexts)?.get(key);
  }
  delete(contexts, key) {
    this.#contextMap(contexts)?.delete(key);
  }
  has(contexts, key) {
    return this.#contextMap(contexts)?.has(key) ?? false;
  }
  keys(contexts) {
    const map = this.#contextMap(contexts);
    return map ? [...map.keys()] : [];
  }
  contexts(contexts) {
    const contextKey = contexts.length ? this.#createContextKey(contexts) : "";
    const startContextKey = contextKey.length ? `${contextKey}.` : "";
    const foundContexts = new Array();
    for (const key of this.#store.keys()) {
      if (key.startsWith(startContextKey)) {
        const subKeys = key.substring(startContextKey.length).split(".");
        if (subKeys.length < 1) continue;
        const context = subKeys[0];
        if (context.length && !foundContexts.includes(context)) {
          foundContexts.push(context);
        }
      }
    }
    return foundContexts;
  }
  clearAll(contexts) {
    const contextKey = this.#createContextKey(contexts);
    this.#store.delete(contextKey);
    const startContextKey = contextKey.length ? `${contextKey}.` : "";
    for (const key of [...this.#store.keys()]) {
      if (key.startsWith(startContextKey)) {
        this.#store.delete(key);
      }
    }
  }
}
export {
  MemoryBlobStorageDriver
};
//# sourceMappingURL=MemoryBlobStorageDriver.js.map
