"use client";
function normalizeHeaderMap(input) {
  const normalized = {};
  for (const [key, value] of Object.entries(input)) {
    const trimmedKey = key.trim();
    if (!trimmedKey) continue;
    const trimmedValue = typeof value === "string" ? value.trim() : "";
    if (!trimmedValue) continue;
    normalized[trimmedKey] = trimmedValue;
  }
  return normalized;
}
function createScopedHeaderStack(options) {
  const stack = [];
  const normalize = options?.normalize !== false;
  function resolveScopedHeaders() {
    if (!stack.length) return {};
    const merged = {};
    for (const item of stack) {
      Object.assign(merged, item);
    }
    return merged;
  }
  async function withScopedHeaders(headers, run) {
    const normalized = normalize ? normalizeHeaderMap(headers) : headers;
    stack.push(normalized);
    try {
      return await run();
    } finally {
      const index = stack.lastIndexOf(normalized);
      if (index >= 0) {
        stack.splice(index, 1);
      }
    }
  }
  return {
    withScopedHeaders,
    resolveScopedHeaders
  };
}
export {
  createScopedHeaderStack
};
//# sourceMappingURL=scopedHeaderStack.js.map
