(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "crypto", "qs"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("crypto"), require("qs"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.crypto, global.qs);
    global.createAwsSignedUrl = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _crypto, _qs) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.default = void 0;
  _crypto = _interopRequireDefault(_crypto);
  _qs = _interopRequireDefault(_qs);

  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

  function toTime(time) {
    return new Date(time).toISOString().replace(/[:\-]|\.\d{3}/g, '');
  }

  function toDate(time) {
    return toTime(time).substring(0, 8);
  }

  function hash(string, encoding) {
    return _crypto.default.createHash('sha256').update(string, 'utf8').digest(encoding);
  }

  function hmac(key, string, encoding) {
    return _crypto.default.createHmac('sha256', key).update(string, 'utf8').digest(encoding);
  }

  function createCredentialScope(time, region, service) {
    return [toDate(time), region, service, 'aws4_request'].join('/');
  }

  function createCanonicalQueryString(params) {
    return Object.keys(params).sort().map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`).join('&');
  }

  function createCanonicalHeaders(headers) {
    return Object.keys(headers).sort().map(name => `${name.toLowerCase().trim()}:${headers[name].toString().trim()}\n`).join('');
  }

  function createSignedHeaders(headers) {
    return Object.keys(headers).sort().map(name => name.toLowerCase().trim()).join(';');
  }

  function createStringToSign(time, region, service, request) {
    return ['AWS4-HMAC-SHA256', toTime(time), createCredentialScope(time, region, service), hash(request, 'hex')].join('\n');
  }

  function createSignature(secret, time, region, service, stringToSign) {
    let h1 = hmac(`AWS4${secret}`, toDate(time));
    let h2 = hmac(h1, region);
    let h3 = hmac(h2, service);
    let h4 = hmac(h3, 'aws4_request');
    return hmac(h4, stringToSign, 'hex');
  }

  function createCanonicalRequest(method, pathname, query, headers, payload) {
    return [method.toUpperCase(), pathname, createCanonicalQueryString(query), createCanonicalHeaders(headers), createSignedHeaders(headers), payload].join('\n');
  }

  function createAwsSignedUrl(method, host, path, service, payload, options = {}) {
    const optionsCopy = { ...options
    };
    optionsCopy.headers = options.headers || {};
    optionsCopy.timestamp = options.timestamp || Date.now();
    optionsCopy.headers = options.headers || {};
    optionsCopy.headers.Host = host;
    let query = optionsCopy.query ? _qs.default.parse(optionsCopy.query) : {};
    const credentialScope = createCredentialScope(optionsCopy.timestamp, optionsCopy.region, service);
    query['X-Amz-Algorithm'] = 'AWS4-HMAC-SHA256';
    query['X-Amz-Credential'] = `${optionsCopy.key}/${credentialScope}`;
    query['X-Amz-Date'] = toTime(optionsCopy.timestamp);
    query['X-Amz-Expires'] = optionsCopy.expires;
    query['X-Amz-SignedHeaders'] = createSignedHeaders(optionsCopy.headers);
    if (optionsCopy.sessionToken) query['X-Amz-Security-Token'] = optionsCopy.sessionToken;
    const canonicalRequest = createCanonicalRequest(method, path, query, optionsCopy.headers, payload);
    const stringToSign = createStringToSign(optionsCopy.timestamp, optionsCopy.region, service, canonicalRequest);
    const signature = createSignature(optionsCopy.secret, optionsCopy.timestamp, optionsCopy.region, service, stringToSign);
    query['X-Amz-Signature'] = signature;
    return `${optionsCopy.protocol}://${host}${path}?${_qs.default.stringify(query)}`;
  }

  const getAwsSignedUrl = function getAwsSignedUrl({
    region,
    accessKeyId,
    secretAccessKey,
    languageCode = 'en-US',
    sampleRate = 16000,
    _createAwsSignedUrl = createAwsSignedUrl,
    isMedical = false,
    specialty = 'PRIMARYCARE',
    type = 'CONVERSATION'
  }) {
    const endpoint = `transcribestreaming.${region}.amazonaws.com:8443`;
    const path = isMedical ? '/medical-stream-transcription-websocket' : '/stream-transcription-websocket';
    let query = `language-code=${languageCode}&media-encoding=pcm&sample-rate=${sampleRate}`;
    if (isMedical) query += `&specialty=${specialty}&type=${type}`;
    const options = {
      region,
      query,
      key: accessKeyId,
      secret: secretAccessKey,
      protocol: 'wss',
      expires: 15
    };
    return _createAwsSignedUrl('GET', endpoint, path, 'transcribe', _crypto.default.createHash('sha256').update('', 'utf8').digest('hex'), options);
  };

  var _default = getAwsSignedUrl;
  _exports.default = _default;
});
//# sourceMappingURL=createAwsSignedUrl.js.map