import {
  contentTypeFor
} from "./chunk-CBLBQP27.js";

// ../../adapters/azure-blob/src/container.ts
var InMemoryBlobContainer = class {
  blobs = /* @__PURE__ */ new Map();
  constructor(seed = {}) {
    for (const [name, value] of Object.entries(seed)) {
      this.blobs.set(name, typeof value === "string" ? new TextEncoder().encode(value) : value);
    }
  }
  async list(prefix) {
    const names = [];
    for (const name of this.blobs.keys()) {
      if (name.startsWith(prefix)) names.push(name);
    }
    return names.sort();
  }
  async read(name) {
    return this.blobs.get(name) ?? null;
  }
  async write(name, data) {
    this.blobs.set(name, data);
  }
  async remove(name) {
    this.blobs.delete(name);
  }
  /** Test helper: the current blob names. */
  get names() {
    return [...this.blobs.keys()].sort();
  }
};

// ../../adapters/azure-blob/src/azure-container.ts
import { BlobServiceClient } from "@azure/storage-blob";
function isNotFound(error) {
  return typeof error === "object" && error !== null && "statusCode" in error && error.statusCode === 404;
}
var AzureBlobContainer = class _AzureBlobContainer {
  constructor(client) {
    this.client = client;
  }
  client;
  /** Build from a connection string (e.g. `UseDevelopmentStorage=true` for Azurite). */
  static fromConnectionString(connectionString, containerName) {
    const service = BlobServiceClient.fromConnectionString(connectionString);
    return new _AzureBlobContainer(service.getContainerClient(containerName));
  }
  /** Create the container if it does not exist (idempotent host start-up step). */
  async ensureContainer() {
    await this.client.createIfNotExists();
  }
  async list(prefix) {
    const names = [];
    for await (const blob of this.client.listBlobsFlat({ prefix })) {
      names.push(blob.name);
    }
    return names.sort();
  }
  async read(name) {
    try {
      const buffer = await this.client.getBlockBlobClient(name).downloadToBuffer();
      return new Uint8Array(buffer);
    } catch (error) {
      if (isNotFound(error)) return null;
      throw error;
    }
  }
  async write(name, data, contentType) {
    await this.client.getBlockBlobClient(name).uploadData(Buffer.from(data), {
      blobHTTPHeaders: { blobContentType: contentType }
    });
  }
  async remove(name) {
    await this.client.getBlockBlobClient(name).deleteIfExists();
  }
};

// ../../adapters/azure-blob/src/paths.ts
function normalizeBlobName(path) {
  return path.replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "").replace(/\/+$/, "");
}

// ../../adapters/azure-blob/src/blob-container-store.ts
var BlobContainerStore = class {
  constructor(container) {
    this.container = container;
  }
  container;
  async get(key) {
    return await this.container.read(normalizeBlobName(key)) ?? void 0;
  }
  async put(key, bytes) {
    const name = normalizeBlobName(key);
    await this.container.write(name, bytes, contentTypeFor(name));
  }
  async delete(key) {
    await this.container.remove(normalizeBlobName(key));
  }
};

// ../../adapters/azure-blob/src/blob-sink.ts
var BlobArtifactSink = class {
  constructor(container) {
    this.container = container;
  }
  container;
  async wipe() {
    for (const name of await this.container.list("")) {
      await this.container.remove(name);
    }
  }
  async write(path, contents) {
    const name = normalizeBlobName(path);
    const data = typeof contents === "string" ? new TextEncoder().encode(contents) : contents;
    await this.container.write(name, data, contentTypeFor(name));
  }
};
export {
  AzureBlobContainer,
  BlobArtifactSink,
  BlobContainerStore,
  InMemoryBlobContainer,
  contentTypeFor,
  normalizeBlobName
};
