"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var React = _interopRequireWildcard(require("react"));
var _uiCore = require("@megafon/ui-core");
var _uiHelpers = require("@megafon/ui-helpers");
var _AudioProgress = _interopRequireDefault(require("./AudioProgress"));
var _AudioVolume = _interopRequireDefault(require("./AudioVolume"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var IconPause = function IconPause(props) {
  return /*#__PURE__*/React.createElement("svg", (0, _extends2["default"])({
    viewBox: "0 0 32 32"
  }, props), /*#__PURE__*/React.createElement("path", {
    d: "M11 10.5v11a.5.5 0 00.5.5h2a.5.5 0 00.5-.5v-11a.5.5 0 00-.5-.5h-2a.5.5 0 00-.5.5zm7 0v11a.5.5 0 00.5.5h2a.5.5 0 00.5-.5v-11a.5.5 0 00-.5-.5h-2a.5.5 0 00-.5.5z"
  }));
};
var IconPlay = function IconPlay(props) {
  return /*#__PURE__*/React.createElement("svg", (0, _extends2["default"])({
    viewBox: "0 0 32 32"
  }, props), /*#__PURE__*/React.createElement("path", {
    d: "M11.5 22.13V9.87a.5.5 0 01.752-.431l10.508 6.13a.5.5 0 010 .863l-10.508 6.13a.5.5 0 01-.752-.433z"
  }));
};
var cn = (0, _uiHelpers.cnCreate)('mfui-audio-player');
var AudioPlayer = function AudioPlayer(_ref) {
  var audioSrc = _ref.audioSrc,
    audioTitle = _ref.audioTitle,
    _ref$position = _ref.position,
    position = _ref$position === void 0 ? 'center' : _ref$position,
    isFullWidth = _ref.isFullWidth;
  var audioRef = React.useRef(null);
  var _React$useState = React.useState(false),
    _React$useState2 = (0, _slicedToArray2["default"])(_React$useState, 2),
    isPlaying = _React$useState2[0],
    setIsPlaying = _React$useState2[1];
  var _React$useState3 = React.useState(false),
    _React$useState4 = (0, _slicedToArray2["default"])(_React$useState3, 2),
    isPause = _React$useState4[0],
    setIsPause = _React$useState4[1];
  var handlePLay = function handlePLay() {
    var _a;
    setIsPlaying(true);
    setIsPause(false);
    (_a = audioRef.current) === null || _a === void 0 ? void 0 : _a.play();
  };
  var handlePause = function handlePause() {
    var _a;
    setIsPlaying(false);
    setIsPause(true);
    (_a = audioRef.current) === null || _a === void 0 ? void 0 : _a.pause();
  };
  var handleChangeAudioCurrentTime = function handleChangeAudioCurrentTime(currentTime) {
    if (!audioRef || !audioRef.current) {
      return;
    }
    audioRef.current.currentTime = currentTime;
  };
  var handleChangeAudioVolume = function handleChangeAudioVolume(volume) {
    if (!audioRef || !audioRef.current) {
      return;
    }
    audioRef.current.volume = volume;
    audioRef.current.muted = !volume;
  };
  var playingIconHandleClick = React.useMemo(function () {
    return isPlaying ? handlePause : handlePLay;
  }, [isPlaying]);
  return /*#__PURE__*/React.createElement("div", {
    className: cn({
      'full-width': isFullWidth,
      position: position
    }),
    "data-testid": "AudioPlayer"
  }, /*#__PURE__*/React.createElement("div", {
    className: cn('player')
  }, /*#__PURE__*/React.createElement("audio", {
    className: cn('audio'),
    ref: audioRef,
    controls: false,
    "data-testid": "AudioPlayer-audio"
  }, /*#__PURE__*/React.createElement("source", {
    src: audioSrc,
    type: "audio/mpeg"
  })), /*#__PURE__*/React.createElement("div", {
    className: cn('playing-controls')
  }, /*#__PURE__*/React.createElement(_uiCore.Button, {
    icon: isPlaying ? /*#__PURE__*/React.createElement(IconPause, null) : /*#__PURE__*/React.createElement(IconPlay, null),
    onClick: playingIconHandleClick,
    sizeAll: "small",
    className: cn('button'),
    dataAttrs: {
      root: {
        'data-testid': 'AudioPlayer-btn'
      }
    }
  })), /*#__PURE__*/React.createElement(_AudioProgress["default"], {
    audioRef: audioRef,
    audioTitle: audioTitle,
    isPlaying: isPlaying,
    isPause: isPause,
    onChangeAudioCurrentTime: handleChangeAudioCurrentTime,
    onPlay: handlePLay,
    onSetIsPlaying: setIsPlaying
  }), /*#__PURE__*/React.createElement(_AudioVolume["default"], {
    onChangeAudioVolume: handleChangeAudioVolume
  })));
};
var _default = exports["default"] = AudioPlayer;