(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "qs", "rxjs", "@bottlenose/rxws", "../internals/shortenChunks"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("qs"), require("rxjs"), require("@bottlenose/rxws"), require("../internals/shortenChunks"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.qs, global.rxjs, global.rxws, global.shortenChunks);
    global.toDeepgram = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _qs, _rxjs, _rxws, _shortenChunks) {
  "use strict";

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

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

  const errors = {
    requiredParams: () => new Error('toDeepgram operator requires username and password')
  };

  const getAuthProtocol = ({
    username,
    password
  }) => {
    const base64Auth = Buffer.from(`${username}:${password}`).toString('base64');
    return ['Basic', base64Auth];
  };

  const getFullUrl = ({
    url,
    encoding,
    channels,
    sampleRate,
    interimResults
  }) => {
    const queryString = _qs.default.stringify({
      encoding,
      channels,
      sample_rate: sampleRate,
      interim_results: interimResults
    });

    return `${url}?${queryString}`;
  };

  const toDeepgram = ({
    username = process.env.DEEPGRAM_USERNAME,
    password = process.env.DEEPGRAM_PASSWORD,
    encoding = 'linear16',
    channels = 1,
    sampleRate = 16000,
    interimResults = true,
    url = 'wss://brain.deepgram.com/v2/listen/stream'
  } = {}) => {
    if (!username || !password) return () => (0, _rxjs.throwError)(errors.requiredParams());
    return audioChunk$ => {
      const lastMessage$ = (0, _rxjs.of)(new Uint8Array(0));
      const messageOut$ = (0, _rxjs.concat)(audioChunk$, lastMessage$).pipe((0, _rxws.conduit)({
        url: getFullUrl({
          url,
          encoding,
          channels,
          sampleRate,
          interimResults
        }),
        protocols: getAuthProtocol({
          username,
          password
        }),
        serializer: chunk => chunk,
        deserializer: message => JSON.parse(message)
      }));
      const wordOut$ = messageOut$.pipe();
      return wordOut$;
    };
  };

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