"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Aura = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _Aura = require("./Aura.l10n");
var style = _interopRequireWildcard(require("./Aura.module.css"));
var _Aura2 = require("./Aura.waveform");
var _useOpenAiRealtimeSession = require("./hooks/useOpenAiRealtimeSession");
var _useXaiRealtimeSession = require("./hooks/useXaiRealtimeSession");
var _Core = require("../../../../Core.constants");
var _helpers = require("../../../helpers");
var _Chat = require("../../Chat.constants");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const VOICE_MODEL_XAI = 'xai';
const Aura = _ref => {
  var _urlParams$voiceMode;
  let {
    hotelId,
    onCallEnd = () => {},
    settings
  } = _ref;
  const openAiSession = (0, _useOpenAiRealtimeSession.useOpenAiRealtimeSession)();
  const xaiSession = (0, _useXaiRealtimeSession.useXaiRealtimeSession)();
  const {
    value: {
      id,
      locale,
      type,
      urlParams = {}
    } = {}
  } = (0, _dataSources.useStore)();
  const useXai = ((_urlParams$voiceMode = urlParams['voice-model']) === null || _urlParams$voiceMode === void 0 ? void 0 : _urlParams$voiceMode.toLowerCase()) === VOICE_MODEL_XAI;
  const {
    startSession,
    endSession,
    audioAnalyzers
  } = useXai ? xaiSession : openAiSession;
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    ai: {
      name: assistantName = _Chat.DEFAULT_NAME
    } = {}
  } = settings || {};
  const [context, setContext] = (0, _react.useState)(null);
  const [isConnecting, setIsConnecting] = (0, _react.useState)(false);
  const [ready, setReady] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    if (type === _Core.TYPE.CHAIN && !hotelId) return;
    setContext({
      hotelId
    });
  }, [hotelId, type]);
  const handleCallEnd = (0, _react.useCallback)(() => {
    endSession();
    setReady(false);
    onCallEnd();
    _dataSources.Event.publish(_helpers.EVENT.METRICS, {
      id: _helpers.METRICS_EVENTS.AURA_CALL_END
    });
  }, [endSession, onCallEnd]);
  (0, _react.useEffect)(() => {
    handleCallStart();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [context]);
  (0, _react.useEffect)(() => {
    const callback = () => {
      if (ready) handleCallEnd();
    };
    _dataSources.Event.subscribe(_helpers.EVENT.CHAT_CLOSE, callback);
    return () => {
      _dataSources.Event.unsubscribe(_helpers.EVENT.CHAT_CLOSE, callback);
    };
  }, [handleCallEnd, ready]);
  const handleCallStart = async () => {
    const isChain = type === _Core.TYPE.CHAIN && !hotelId;
    const chainId = isChain ? id : undefined;
    if (!navigator.mediaDevices || !context && !chainId) {
      _dataSources.Event.publish(_helpers.EVENT.METRICS, {
        id: _helpers.METRICS_EVENTS.AURA_ERROR
      });
      return;
    }
    setIsConnecting(true);
    try {
      await startSession({
        assistant_name: assistantName,
        chainId,
        hotelId,
        isChain,
        locale,
        setIsConnecting,
        setReady
      });
    } catch (error) {
      setIsConnecting(false);
    }
  };
  return !(!context && !(type === _Core.TYPE.CHAIN && !hotelId)) ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_Aura2.Waveform, {
    active: ready,
    conversation: audioAnalyzers
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.footer
  }, isConnecting && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, translate(_Aura.L10N.LABEL_CALLING, {
    name: assistantName
  })), /*#__PURE__*/_react.default.createElement(_ui.Button, {
    busy: isConnecting,
    rounded: true,
    squared: true,
    large: true,
    tooltip: translate(_Aura.L10N.LABEL_HANG_UP),
    onPress: handleCallEnd,
    className: (0, _ui.styles)(style.button, ready && style.ready)
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    value: _helpers.ICON.HANG_UP
  })))) : null;
};
exports.Aura = Aura;
Aura.displayName = 'Mirai:Core:Chat.Aura';
Aura.propTypes = {
  hotelId: _propTypes.default.string,
  onCallEnd: _propTypes.default.func,
  settings: _propTypes.default.shape({
    ai: _propTypes.default.shape({
      name: _propTypes.default.string
    })
  })
};
//# sourceMappingURL=Aura.js.map