var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var utils_common_exports = {};
__export(utils_common_exports, {
  appendToURLPath: () => appendToURLPath,
  appendToURLQuery: () => appendToURLQuery,
  assertResponse: () => assertResponse,
  delay: () => delay,
  extractConnectionStringParts: () => extractConnectionStringParts,
  getAccountNameFromUrl: () => getAccountNameFromUrl,
  getURLParameter: () => getURLParameter,
  getURLPath: () => getURLPath,
  getURLQueries: () => getURLQueries,
  getValueInConnString: () => getValueInConnString,
  isIpEndpointStyle: () => isIpEndpointStyle,
  sanitizeHeaders: () => sanitizeHeaders,
  sanitizeURL: () => sanitizeURL,
  setURLHost: () => setURLHost,
  setURLParameter: () => setURLParameter,
  truncatedISO8061Date: () => truncatedISO8061Date
});
module.exports = __toCommonJS(utils_common_exports);
var import_core_rest_pipeline = require("@azure/core-rest-pipeline");
var import_constants = require("./constants.js");
function appendToURLPath(url, name) {
  const urlParsed = new URL(url);
  let path = urlParsed.pathname;
  path = path ? path.endsWith("/") ? `${path}${name}` : `${path}/${name}` : name;
  urlParsed.pathname = path;
  return urlParsed.toString();
}
function setURLParameter(url, name, value) {
  const urlParsed = new URL(url);
  const encodedName = encodeURIComponent(name);
  const encodedValue = value ? encodeURIComponent(value) : void 0;
  const searchString = urlParsed.search === "" ? "?" : urlParsed.search;
  const searchPieces = [];
  for (const pair of searchString.slice(1).split("&")) {
    if (pair) {
      const [key] = pair.split("=", 2);
      if (key !== encodedName) {
        searchPieces.push(pair);
      }
    }
  }
  if (encodedValue) {
    searchPieces.push(`${encodedName}=${encodedValue}`);
  }
  urlParsed.search = searchPieces.length ? `?${searchPieces.join("&")}` : "";
  return urlParsed.toString();
}
function getURLParameter(url, name) {
  const urlParsed = new URL(url);
  return urlParsed.searchParams.get(name) ?? void 0;
}
function setURLHost(url, host) {
  const urlParsed = new URL(url);
  urlParsed.hostname = host;
  return urlParsed.toString();
}
function getURLPath(url) {
  try {
    const urlParsed = new URL(url);
    return urlParsed.pathname;
  } catch (e) {
    return void 0;
  }
}
function getURLQueries(url) {
  let queryString = new URL(url).search;
  if (!queryString) {
    return {};
  }
  queryString = queryString.trim();
  queryString = queryString.startsWith("?") ? queryString.substring(1) : queryString;
  let querySubStrings = queryString.split("&");
  querySubStrings = querySubStrings.filter((value) => {
    const indexOfEqual = value.indexOf("=");
    const lastIndexOfEqual = value.lastIndexOf("=");
    return indexOfEqual > 0 && indexOfEqual === lastIndexOfEqual && lastIndexOfEqual < value.length - 1;
  });
  const queries = {};
  for (const querySubString of querySubStrings) {
    const splitResults = querySubString.split("=");
    const key = splitResults[0];
    const value = splitResults[1];
    queries[key] = value;
  }
  return queries;
}
function getProxyUriFromDevConnString(connectionString) {
  let proxyUri = "";
  if (connectionString.search("DevelopmentStorageProxyUri=") !== -1) {
    const matchCredentials = connectionString.split(";");
    for (const element of matchCredentials) {
      if (element.trim().startsWith("DevelopmentStorageProxyUri=")) {
        proxyUri = element.trim().match("DevelopmentStorageProxyUri=(.*)")[1];
      }
    }
  }
  return proxyUri;
}
function getValueInConnString(connectionString, argument) {
  const elements = connectionString.split(";");
  for (const element of elements) {
    if (element.trim().startsWith(argument)) {
      return element.trim().match(argument + "=(.*)")[1];
    }
  }
  return "";
}
function extractConnectionStringParts(connectionString) {
  let proxyUri = "";
  if (connectionString.startsWith("UseDevelopmentStorage=true")) {
    proxyUri = getProxyUriFromDevConnString(connectionString);
    connectionString = import_constants.DevelopmentConnectionString;
  }
  let queueEndpoint = getValueInConnString(connectionString, "QueueEndpoint");
  queueEndpoint = queueEndpoint.endsWith("/") ? queueEndpoint.slice(0, -1) : queueEndpoint;
  if (connectionString.search("DefaultEndpointsProtocol=") !== -1 && connectionString.search("AccountKey=") !== -1) {
    let defaultEndpointsProtocol = "";
    let accountName = "";
    let accountKey = Buffer.from("accountKey", "base64");
    let endpointSuffix = "";
    accountName = getValueInConnString(connectionString, "AccountName");
    accountKey = Buffer.from(getValueInConnString(connectionString, "AccountKey"), "base64");
    if (!queueEndpoint) {
      defaultEndpointsProtocol = getValueInConnString(connectionString, "DefaultEndpointsProtocol");
      const protocol = defaultEndpointsProtocol.toLowerCase();
      if (protocol !== "https" && protocol !== "http") {
        throw new Error(
          "Invalid DefaultEndpointsProtocol in the provided Connection String. Expecting 'https' or 'http'"
        );
      }
      endpointSuffix = getValueInConnString(connectionString, "EndpointSuffix");
      if (!endpointSuffix) {
        throw new Error("Invalid EndpointSuffix in the provided Connection String");
      }
      queueEndpoint = `${defaultEndpointsProtocol}://${accountName}.queue.${endpointSuffix}`;
    }
    if (!accountName) {
      throw new Error("Invalid AccountName in the provided Connection String");
    } else if (accountKey.length === 0) {
      throw new Error("Invalid AccountKey in the provided Connection String");
    }
    return {
      kind: "AccountConnString",
      url: queueEndpoint,
      accountName,
      accountKey,
      proxyUri
    };
  } else {
    const accountSas = getValueInConnString(connectionString, "SharedAccessSignature");
    let accountName = getValueInConnString(connectionString, "AccountName");
    if (!accountName) {
      accountName = getAccountNameFromUrl(queueEndpoint);
    }
    if (!queueEndpoint) {
      throw new Error("Invalid QueueEndpoint in the provided SAS Connection String");
    } else if (!accountSas) {
      throw new Error("Invalid SharedAccessSignature in the provided SAS Connection String");
    }
    return { kind: "SASConnString", url: queueEndpoint, accountName, accountSas };
  }
}
function truncatedISO8061Date(date, withMilliseconds = true) {
  const dateString = date.toISOString();
  return withMilliseconds ? dateString.substring(0, dateString.length - 1) + "0000Z" : dateString.substring(0, dateString.length - 5) + "Z";
}
async function delay(timeInMs, aborter, abortError) {
  return new Promise((resolve, reject) => {
    let timeout;
    const abortHandler = () => {
      if (timeout !== void 0) {
        clearTimeout(timeout);
      }
      reject(abortError);
    };
    const resolveHandler = () => {
      if (aborter !== void 0) {
        aborter.removeEventListener("abort", abortHandler);
      }
      resolve();
    };
    timeout = setTimeout(resolveHandler, timeInMs);
    if (aborter !== void 0) {
      aborter.addEventListener("abort", abortHandler);
    }
  });
}
function sanitizeURL(url) {
  let safeURL = url;
  if (getURLParameter(safeURL, import_constants.URLConstants.Parameters.SIGNATURE)) {
    safeURL = setURLParameter(safeURL, import_constants.URLConstants.Parameters.SIGNATURE, "*****");
  }
  return safeURL;
}
function sanitizeHeaders(originalHeader) {
  const headers = (0, import_core_rest_pipeline.createHttpHeaders)();
  for (const [name, value] of originalHeader) {
    if (name.toLowerCase() === import_constants.HeaderConstants.AUTHORIZATION) {
      headers.set(name, "*****");
    } else if (name.toLowerCase() === import_constants.HeaderConstants.X_MS_COPY_SOURCE) {
      headers.set(name, sanitizeURL(value));
    } else {
      headers.set(name, value);
    }
  }
  return headers;
}
const accountNameSuffixes = [
  "-secondary-ipv6",
  "-secondary-dualstack",
  "-ipv6",
  "-dualstack",
  "-secondary"
];
function getAccountNameFromUrl(url) {
  const parsedUrl = new URL(url);
  let accountName;
  try {
    if (parsedUrl.hostname.split(".")[1] === "queue") {
      accountName = parsedUrl.hostname.split(".")[0];
      for (let i = 0; i < accountNameSuffixes.length; ++i) {
        const suffix = accountNameSuffixes[i];
        if (accountName.endsWith(suffix)) {
          accountName = accountName.substring(0, accountName.length - suffix.length);
        }
      }
    } else if (isIpEndpointStyle(parsedUrl)) {
      accountName = parsedUrl.pathname.split("/")[1];
    } else {
      accountName = "";
    }
    return accountName;
  } catch (error) {
    throw new Error("Unable to extract accountName with provided information.");
  }
}
function isIpEndpointStyle(parsedUrl) {
  const host = parsedUrl.host;
  return /^.*:.*:.*$|^(localhost|host.docker.internal)(:[0-9]+)?$|^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])){3}(:[0-9]+)?$/.test(
    host
  ) || Boolean(parsedUrl.port) && import_constants.PathStylePorts.includes(parsedUrl.port);
}
function appendToURLQuery(url, queryParts) {
  const urlParsed = new URL(url);
  let query = urlParsed.search;
  if (query) {
    query += "&" + queryParts;
  } else {
    query = queryParts;
  }
  urlParsed.search = query;
  return urlParsed.toString();
}
function assertResponse(response) {
  if (`_response` in response) {
    return response;
  }
  throw new TypeError(`Unexpected response object ${response}`);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  appendToURLPath,
  appendToURLQuery,
  assertResponse,
  delay,
  extractConnectionStringParts,
  getAccountNameFromUrl,
  getURLParameter,
  getURLPath,
  getURLQueries,
  getValueInConnString,
  isIpEndpointStyle,
  sanitizeHeaders,
  sanitizeURL,
  setURLHost,
  setURLParameter,
  truncatedISO8061Date
});
//# sourceMappingURL=utils.common.js.map
