"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Waveform = void 0;
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var style = _interopRequireWildcard(require("./Aura.module.css"));
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 SENSITIVITY = 1.2;
const Waveform = _ref => {
  let {
    active = false,
    conversation
  } = _ref;
  const [audioLevel, setAudioLevel] = (0, _react.useState)(0);
  const [inputAudioLevel, setInputAudioLevel] = (0, _react.useState)(0);
  const animationFrameRef = (0, _react.useRef)(null);
  const isActiveRef = (0, _react.useRef)(false);
  const analyzeAudio = (0, _react.useCallback)(() => {
    if (!isActiveRef.current) return;
    const outputDataArray = conversation.getOutputByteFrequencyData();
    const inputDataArray = conversation.getInputByteFrequencyData();
    let outputLevel = 0;
    let inputLevel = 0;
    if (outputDataArray && outputDataArray.length > 0) {
      const sum = outputDataArray.reduce((acc, value) => acc + value, 0);
      const average = sum / outputDataArray.length;
      outputLevel = Math.min(average / 128 * SENSITIVITY, 1);
    }
    if (inputDataArray && inputDataArray.length > 0) {
      const sum = inputDataArray.reduce((acc, value) => acc + value, 0);
      const average = sum / inputDataArray.length;
      inputLevel = Math.min(average / 128 * SENSITIVITY, 1);
    }
    setAudioLevel(outputLevel);
    setInputAudioLevel(inputLevel);
    animationFrameRef.current = requestAnimationFrame(analyzeAudio);
  }, [conversation]);
  const stopAudioAnalysis = (0, _react.useCallback)(() => {
    isActiveRef.current = false;
    if (animationFrameRef.current) {
      cancelAnimationFrame(animationFrameRef.current);
      animationFrameRef.current = null;
    }
    setAudioLevel(0);
    setInputAudioLevel(0);
  }, []);
  const startAudioAnalysis = (0, _react.useCallback)(() => {
    isActiveRef.current = true;
    analyzeAudio();
  }, [analyzeAudio]);
  (0, _react.useEffect)(() => {
    if (active) startAudioAnalysis();else stopAudioAnalysis();
    return () => {
      stopAudioAnalysis();
    };
  }, [active, startAudioAnalysis, stopAudioAnalysis]);
  const isUserSpeaking = inputAudioLevel > audioLevel && inputAudioLevel > 0.1;
  const currentAudioLevel = isUserSpeaking ? inputAudioLevel : audioLevel;
  const getAuraScale = () => {
    if (!active || currentAudioLevel === 0) return 1;
    return 1 + currentAudioLevel * 0.5;
  };
  return /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: (0, _ui.styles)(style.waveform, active && style.active),
    style: {
      '--aura-scale': getAuraScale(),
      '--aura-opacity': !active ? 0 : Math.max(0.33, currentAudioLevel * 0.66)
    }
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.aura
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.pulse
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.glow
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.content
  }));
};
exports.Waveform = Waveform;
Waveform.displayName = 'Mirai:Core:Chat.Aura.Waveform';
Waveform.propTypes = {
  active: _propTypes.default.bool,
  conversation: _propTypes.default.shape({
    getOutputByteFrequencyData: _propTypes.default.func,
    getInputByteFrequencyData: _propTypes.default.func
  })
};
//# sourceMappingURL=Aura.waveform.js.map