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

// ../../adapters/azure-blob/src/container.ts
var InMemoryBlobContainer = class {
  blobs = /* @__PURE__ */ new Map();
  cacheControls = /* @__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, contentType, cacheControl) {
    void contentType;
    this.blobs.set(name, data);
    if (cacheControl !== void 0) this.cacheControls.set(name, cacheControl);
  }
  async remove(name) {
    this.blobs.delete(name);
  }
  /** Test helper: the current blob names. */
  get names() {
    return [...this.blobs.keys()].sort();
  }
  /** Test helper: the `Cache-Control` a blob was written with, if any. */
  cacheControlOf(name) {
    return this.cacheControls.get(name);
  }
};

// ../../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, cacheControl) {
    await this.client.getBlockBlobClient(name).uploadData(Buffer.from(data), {
      blobHTTPHeaders: {
        blobContentType: contentType,
        ...cacheControl !== void 0 && { blobCacheControl: cacheControl }
      }
    });
  }
  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, cacheControl) {
    const name = normalizeBlobName(key);
    await this.container.write(name, bytes, contentTypeFor(name), cacheControl);
  }
  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, cacheControl) {
    const name = normalizeBlobName(path);
    const data = typeof contents === "string" ? new TextEncoder().encode(contents) : contents;
    await this.container.write(name, data, contentTypeFor(name), cacheControl);
  }
};

// ../../adapters/azure-blob/src/azure-storage.ts
function azureStorage(container) {
  const store = new BlobContainerStore(container);
  return {
    get: (key) => store.get(key),
    // Forward `cacheControl` (DESIGN §11): this is the published-site sink `createEditor`
    // actually wires (`asSink(azureStorage(website))`), so dropping the arg here silently
    // strips the publish Cache-Control before it reaches the blob.
    put: (key, bytes, cacheControl) => store.put(key, bytes, cacheControl),
    delete: (key) => store.delete(key),
    wipe: async () => {
      for (const name of await container.list("")) await container.remove(name);
    }
  };
}
export {
  AzureBlobContainer,
  BlobArtifactSink,
  BlobContainerStore,
  InMemoryBlobContainer,
  azureStorage,
  contentTypeFor,
  normalizeBlobName
};
