/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ScopedStorage } from "#bdx/ScopedStorage.js";
import { Bytes, ImplementationError, Logger } from "#general";
import { FileDesignator } from "./FileDesignator.js";
const logger = Logger.get("PersistedFileDesignator");
class PersistedFileDesignator extends FileDesignator {
  #storage;
  #blob;
  #blobName;
  constructor(fd, storage) {
    if (fd instanceof FileDesignator) {
      fd = fd.bytes;
    }
    const blobName = Bytes.isBytes(fd) ? Bytes.toString(fd) : fd;
    if (storage instanceof ScopedStorage) {
      fd = Bytes.fromString(`${storage.scope}/${blobName}`);
      storage = storage.context;
    }
    super(fd);
    const subContext = blobName.split(".");
    this.#blobName = subContext.pop();
    for (const parts of subContext) {
      storage = storage.createContext(parts);
    }
    this.#storage = storage;
  }
  exists() {
    return this.#storage.has(this.#blobName);
  }
  get blobName() {
    return this.#blobName;
  }
  async openBlob() {
    if (this.#blob === void 0) {
      const blobName = this.#blobName;
      if (!this.#storage.has(blobName)) {
        throw new ImplementationError(
          `File designator must point to an existing blob "${blobName}" in the storage to send data`
        );
      }
      this.#blob = await this.#storage.openBlob(blobName);
    }
    return this.#blob;
  }
  writeFromStream(stream) {
    logger.debug(`Writing blob "${this.text}" (${this.#blobName}) to storage`);
    return this.#storage.writeBlobFromStream(this.#blobName, stream);
  }
  delete() {
    logger.debug(`Deleting blob "${this.text}" (${this.#blobName}) from storage`);
    return this.#storage.delete(this.#blobName);
  }
}
export {
  PersistedFileDesignator
};
//# sourceMappingURL=PersistedFileDesignator.js.map
