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 FileSASSignatureValues_exports = {};
__export(FileSASSignatureValues_exports, {
  generateFileSASQueryParameters: () => generateFileSASQueryParameters,
  generateFileSASQueryParametersDefault: () => generateFileSASQueryParametersDefault,
  generateFileSASQueryParametersInternal: () => generateFileSASQueryParametersInternal,
  generateFileSASQueryParametersUDK20250705: () => generateFileSASQueryParametersUDK20250705
});
module.exports = __toCommonJS(FileSASSignatureValues_exports);
var import_storage_common = require("@azure/storage-common");
var import_FileSASPermissions = require("./FileSASPermissions.js");
var import_SasIPRange = require("./SasIPRange.js");
var import_SASQueryParameters = require("./SASQueryParameters.js");
var import_ShareSASPermissions = require("./ShareSASPermissions.js");
var import_constants = require("./utils/constants.js");
var import_utils_common = require("./utils/utils.common.js");
var import_storage_common2 = require("@azure/storage-common");
function generateFileSASQueryParameters(fileSASSignatureValues, sharedKeyCredentialOrUserDelegationKey, accountName) {
  return generateFileSASQueryParametersInternal(
    fileSASSignatureValues,
    sharedKeyCredentialOrUserDelegationKey,
    accountName
  ).sasQueryParameters;
}
function generateFileSASQueryParametersInternal(fileSASSignatureValues, sharedKeyCredentialOrUserDelegationKey, accountName) {
  const version = fileSASSignatureValues.version ? fileSASSignatureValues.version : import_constants.SERVICE_VERSION;
  const sharedKeyCredential = sharedKeyCredentialOrUserDelegationKey instanceof import_storage_common.StorageSharedKeyCredential ? sharedKeyCredentialOrUserDelegationKey : void 0;
  let userDelegationKeyCredential;
  if (sharedKeyCredential === void 0 && accountName !== void 0) {
    userDelegationKeyCredential = new import_storage_common2.UserDelegationKeyCredential(
      accountName,
      sharedKeyCredentialOrUserDelegationKey
    );
  }
  if (sharedKeyCredential !== void 0) {
    return generateFileSASQueryParametersDefault(fileSASSignatureValues, sharedKeyCredential);
  } else {
    if (version >= "2025-07-05") {
      return generateFileSASQueryParametersUDK20250705(
        fileSASSignatureValues,
        userDelegationKeyCredential,
        accountName
      );
    } else {
      throw new RangeError(
        "'version' must be >= '2025-07-05' when generating user delegation SAS using user delegation key."
      );
    }
  }
}
function generateFileSASQueryParametersDefault(fileSASSignatureValues, sharedKeyCredential) {
  if (!fileSASSignatureValues.identifier && !(fileSASSignatureValues.permissions && fileSASSignatureValues.expiresOn)) {
    throw new RangeError(
      "Must provide 'permissions' and 'expiresOn' for File SAS generation when 'identifier' is not provided."
    );
  }
  const version = fileSASSignatureValues.version ? fileSASSignatureValues.version : import_constants.SERVICE_VERSION;
  let resource = "s";
  if (fileSASSignatureValues.filePath) {
    resource = "f";
  }
  let verifiedPermissions;
  if (fileSASSignatureValues.permissions) {
    if (fileSASSignatureValues.filePath) {
      verifiedPermissions = import_FileSASPermissions.FileSASPermissions.parse(
        fileSASSignatureValues.permissions.toString()
      ).toString();
    } else {
      verifiedPermissions = import_ShareSASPermissions.ShareSASPermissions.parse(
        fileSASSignatureValues.permissions.toString()
      ).toString();
    }
  }
  const stringToSign = [
    verifiedPermissions,
    fileSASSignatureValues.startsOn ? (0, import_utils_common.truncatedISO8061Date)(fileSASSignatureValues.startsOn, false) : "",
    fileSASSignatureValues.expiresOn ? (0, import_utils_common.truncatedISO8061Date)(fileSASSignatureValues.expiresOn, false) : "",
    getCanonicalName(
      sharedKeyCredential.accountName,
      fileSASSignatureValues.shareName,
      fileSASSignatureValues.filePath
    ),
    fileSASSignatureValues.identifier,
    fileSASSignatureValues.ipRange ? (0, import_SasIPRange.ipRangeToString)(fileSASSignatureValues.ipRange) : "",
    fileSASSignatureValues.protocol,
    version,
    fileSASSignatureValues.cacheControl,
    fileSASSignatureValues.contentDisposition,
    fileSASSignatureValues.contentEncoding,
    fileSASSignatureValues.contentLanguage,
    fileSASSignatureValues.contentType
  ].join("\n");
  const signature = sharedKeyCredential.computeHMACSHA256(stringToSign);
  return {
    sasQueryParameters: new import_SASQueryParameters.SASQueryParameters(
      version,
      signature,
      verifiedPermissions,
      void 0,
      void 0,
      fileSASSignatureValues.protocol,
      fileSASSignatureValues.startsOn,
      fileSASSignatureValues.expiresOn,
      fileSASSignatureValues.ipRange,
      fileSASSignatureValues.identifier,
      resource,
      fileSASSignatureValues.cacheControl,
      fileSASSignatureValues.contentDisposition,
      fileSASSignatureValues.contentEncoding,
      fileSASSignatureValues.contentLanguage,
      fileSASSignatureValues.contentType
    ),
    stringToSign
  };
}
function generateFileSASQueryParametersUDK20250705(fileSASSignatureValues, userDelegationKeyCredential, accountName) {
  if (!(fileSASSignatureValues.permissions && fileSASSignatureValues.expiresOn)) {
    throw new RangeError(
      "Must provide 'permissions' and 'expiresOn' for File SAS generation when generating user delegation SAS."
    );
  }
  const version = fileSASSignatureValues.version ? fileSASSignatureValues.version : import_constants.SERVICE_VERSION;
  let resource = "s";
  if (fileSASSignatureValues.filePath) {
    resource = "f";
  }
  let verifiedPermissions;
  if (fileSASSignatureValues.permissions) {
    if (fileSASSignatureValues.filePath) {
      verifiedPermissions = import_FileSASPermissions.FileSASPermissions.parse(
        fileSASSignatureValues.permissions.toString()
      ).toString();
    } else {
      verifiedPermissions = import_ShareSASPermissions.ShareSASPermissions.parse(
        fileSASSignatureValues.permissions.toString()
      ).toString();
    }
  }
  const stringToSign = [
    verifiedPermissions,
    fileSASSignatureValues.startsOn ? (0, import_utils_common.truncatedISO8061Date)(fileSASSignatureValues.startsOn, false) : "",
    fileSASSignatureValues.expiresOn ? (0, import_utils_common.truncatedISO8061Date)(fileSASSignatureValues.expiresOn, false) : "",
    getCanonicalName(
      accountName,
      fileSASSignatureValues.shareName,
      fileSASSignatureValues.filePath
    ),
    userDelegationKeyCredential.userDelegationKey.signedObjectId,
    userDelegationKeyCredential.userDelegationKey.signedTenantId,
    userDelegationKeyCredential.userDelegationKey.signedStartsOn ? (0, import_utils_common.truncatedISO8061Date)(userDelegationKeyCredential.userDelegationKey.signedStartsOn, false) : "",
    userDelegationKeyCredential.userDelegationKey.signedExpiresOn ? (0, import_utils_common.truncatedISO8061Date)(userDelegationKeyCredential.userDelegationKey.signedExpiresOn, false) : "",
    userDelegationKeyCredential.userDelegationKey.signedService,
    userDelegationKeyCredential.userDelegationKey.signedVersion,
    userDelegationKeyCredential.userDelegationKey.signedDelegatedUserTenantId,
    // shared key delegation signed tenant id.
    fileSASSignatureValues.delegatedUserObjectId,
    fileSASSignatureValues.ipRange ? (0, import_SasIPRange.ipRangeToString)(fileSASSignatureValues.ipRange) : "",
    fileSASSignatureValues.protocol,
    version,
    fileSASSignatureValues.cacheControl,
    fileSASSignatureValues.contentDisposition,
    fileSASSignatureValues.contentEncoding,
    fileSASSignatureValues.contentLanguage,
    fileSASSignatureValues.contentType
  ].join("\n");
  const signature = userDelegationKeyCredential.computeHMACSHA256(stringToSign);
  return {
    sasQueryParameters: new import_SASQueryParameters.SASQueryParameters(
      version,
      signature,
      verifiedPermissions,
      void 0,
      void 0,
      fileSASSignatureValues.protocol,
      fileSASSignatureValues.startsOn,
      fileSASSignatureValues.expiresOn,
      fileSASSignatureValues.ipRange,
      fileSASSignatureValues.identifier,
      resource,
      fileSASSignatureValues.cacheControl,
      fileSASSignatureValues.contentDisposition,
      fileSASSignatureValues.contentEncoding,
      fileSASSignatureValues.contentLanguage,
      fileSASSignatureValues.contentType,
      userDelegationKeyCredential.userDelegationKey,
      fileSASSignatureValues.delegatedUserObjectId
    ),
    stringToSign
  };
}
function getCanonicalName(accountName, shareName, filePath) {
  const elements = [`/file/${accountName}/${shareName}`];
  if (filePath) {
    elements.push(`/${filePath}`);
  }
  return elements.join("");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  generateFileSASQueryParameters,
  generateFileSASQueryParametersDefault,
  generateFileSASQueryParametersInternal,
  generateFileSASQueryParametersUDK20250705
});
//# sourceMappingURL=FileSASSignatureValues.js.map
