"use strict";

require("core-js/modules/es.object.define-property.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FcrLiveStreamingContainer = void 0;
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.regexp.test.js");
require("core-js/modules/es.string.trim.js");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _input = require("../input");
var _inputPassword = require("../input/input-password");
require("./container.css");
var _button = require("../button");
var _i18n = require("../../i18n");
var _jsxRuntime = require("react/jsx-runtime");
var FcrLiveStreamingContainer = exports.FcrLiveStreamingContainer = function FcrLiveStreamingContainer(props) {
  var defaultPlayUrl = props.defaultPlayUrl,
    defaultVideoStreamingUrl = props.defaultVideoStreamingUrl,
    defaultVideoStreamingKey = props.defaultVideoStreamingKey,
    isStarted = props.isStarted,
    loading = props.loading,
    onLiveButtonClick = props.onLiveButtonClick,
    isStartError = props.isStartError;
  var _useState = (0, _react.useState)(defaultVideoStreamingUrl),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    videoSteamingUrl = _useState2[0],
    setVideoSteamingUrl = _useState2[1];
  var _useState3 = (0, _react.useState)(defaultVideoStreamingKey),
    _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
    videoStreamingKey = _useState4[0],
    setVideoStreamingKey = _useState4[1];
  var _useState5 = (0, _react.useState)(defaultPlayUrl),
    _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
    playUrl = _useState6[0],
    setPlayUrl = _useState6[1];
  var _useState7 = (0, _react.useState)(false),
    _useState8 = (0, _slicedToArray2["default"])(_useState7, 2),
    isVideoSteamingUrlError = _useState8[0],
    setIsVideoSteamingUrlError = _useState8[1];
  var _useState9 = (0, _react.useState)(false),
    _useState0 = (0, _slicedToArray2["default"])(_useState9, 2),
    isVideoStreamingKeyError = _useState0[0],
    setIsVideoStreamingKeyError = _useState0[1];
  // 判断是否为空
  var _useState1 = (0, _react.useState)(false),
    _useState10 = (0, _slicedToArray2["default"])(_useState1, 2),
    isPlayUrlEmptyTip = _useState10[0],
    setIsPlayUrlEmptyTip = _useState10[1];
  var _useState11 = (0, _react.useState)(false),
    _useState12 = (0, _slicedToArray2["default"])(_useState11, 2),
    isPlayUrlError = _useState12[0],
    setIsPlayUrlError = _useState12[1];
  (0, _react.useEffect)(function () {
    setVideoSteamingUrl(defaultVideoStreamingUrl);
    setVideoStreamingKey(defaultVideoStreamingKey);
    setPlayUrl(defaultPlayUrl);
  }, [defaultVideoStreamingKey, defaultVideoStreamingUrl, defaultPlayUrl]);
  var isValidUrl = function isValidUrl(url) {
    var urlRegex = /^(|http|https|rtmps|rtmp):\/\/[^ "]+$/;
    return urlRegex.test(url);
  };
  var transI18n = (0, _i18n.useI18n)();
  var checkUrl = function checkUrl(url, setError) {
    if (!isValidUrl(url)) {
      setError(true);
    } else {
      setError(false);
    }
  };
  var handleLiveButtonClick = function handleLiveButtonClick() {
    checkUrl(videoSteamingUrl, setIsVideoSteamingUrlError);
    checkUrl(playUrl, setIsPlayUrlError);
    if (!videoStreamingKey.trim() && !isStarted) {
      setIsVideoStreamingKeyError(true);
      return;
    }
    setIsVideoStreamingKeyError(false);
    if (!playUrl.trim() && !isStarted) {
      setIsPlayUrlEmptyTip(true);
      setIsPlayUrlError(false);
      return;
    }
    setIsPlayUrlEmptyTip(false);
    onLiveButtonClick(videoSteamingUrl, videoStreamingKey, playUrl);
  };
  var t = (0, _i18n.useI18n)();
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "fcr-live-streaming-container",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_input.FcrInput, {
        label: t('fmt_live_label_videostreamingurl'),
        placeholder: t('fmt_live_inputtips_videostreamingurl'),
        shape: "rounded",
        size: "small",
        value: videoSteamingUrl,
        onChange: function onChange(value) {
          return setVideoSteamingUrl(value);
        },
        onBlur: function onBlur() {
          return checkUrl(videoSteamingUrl, setIsVideoSteamingUrlError);
        },
        disabled: isStarted
      }), isVideoSteamingUrlError && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-live-streaming-container__error-tip",
        children: t('fmt_broadcast_tips_righturl')
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "fcr-live-streaming-password-input",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_inputPassword.FcrInputPassword, {
        label: t('fmt_live_label_videostreamingkey'),
        placeholder: t('fmt_live_inputtips_videostreamingkey'),
        shape: "rounded",
        size: "small",
        value: videoStreamingKey,
        onChange: function onChange(value) {
          return setVideoStreamingKey(value);
        },
        disabled: isStarted
      }), isVideoStreamingKeyError && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-live-streaming-container__error-tip",
        children: t('fmt_PSTN_tips_unempty')
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "fcr-live-streaming-container-title",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          className: "fcr-live-streaming-container-main-title",
          children: t('fmt_live_label_ivepage')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          className: "fcr-live-streaming-container-secondary-title",
          children: t('fmt_broadcast_tips_anyonewatch')
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_input.FcrInput, {
        placeholder: t('fmt_broadcast_tips_streamingwebpage'),
        shape: "rounded",
        size: "small",
        value: playUrl,
        onChange: function onChange(value) {
          return setPlayUrl(value);
        },
        onBlur: function onBlur() {
          return checkUrl(playUrl, setIsPlayUrlError);
        },
        disabled: isStarted
      }), isPlayUrlError && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-live-streaming-container__error-tip",
        children: t('fmt_broadcast_tips_rightstreamingwebpage')
      }), isPlayUrlEmptyTip && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-live-streaming-container__error-tip",
        children: t('fmt_PSTN_tips_unempty')
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "fcr-live-streaming-container__start-live",
      children: [isStartError && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-live-streaming-container__error",
        children: t('fmt_broadcast_tips_initiatefailed')
      }), isStarted && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-live-streaming-container__started-tips",
        children: t('fmt_broadcast_tips_alreadybroadcasting')
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
        size: "S",
        type: "primary",
        shape: "rounded",
        styleType: isStarted ? 'danger' : undefined,
        loading: loading,
        onClick: handleLiveButtonClick,
        children: isStarted ? transI18n('fmt_tips_button_Stopolive') : transI18n('fmt_tips_button_Golive')
      })]
    })]
  });
};