import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.regexp.test.js";
import "core-js/modules/es.string.trim.js";
import { useEffect, useState } from 'react';
import { FcrInput } from '../input';
import { FcrInputPassword } from '../input/input-password';
import './container.css';
import { FcrButton } from '../button';
import { useI18n } from '../../i18n';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export var 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 = useState(defaultVideoStreamingUrl),
    _useState2 = _slicedToArray(_useState, 2),
    videoSteamingUrl = _useState2[0],
    setVideoSteamingUrl = _useState2[1];
  var _useState3 = useState(defaultVideoStreamingKey),
    _useState4 = _slicedToArray(_useState3, 2),
    videoStreamingKey = _useState4[0],
    setVideoStreamingKey = _useState4[1];
  var _useState5 = useState(defaultPlayUrl),
    _useState6 = _slicedToArray(_useState5, 2),
    playUrl = _useState6[0],
    setPlayUrl = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    isVideoSteamingUrlError = _useState8[0],
    setIsVideoSteamingUrlError = _useState8[1];
  var _useState9 = useState(false),
    _useState0 = _slicedToArray(_useState9, 2),
    isVideoStreamingKeyError = _useState0[0],
    setIsVideoStreamingKeyError = _useState0[1];
  // 判断是否为空
  var _useState1 = useState(false),
    _useState10 = _slicedToArray(_useState1, 2),
    isPlayUrlEmptyTip = _useState10[0],
    setIsPlayUrlEmptyTip = _useState10[1];
  var _useState11 = useState(false),
    _useState12 = _slicedToArray(_useState11, 2),
    isPlayUrlError = _useState12[0],
    setIsPlayUrlError = _useState12[1];
  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 = 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 = useI18n();
  return /*#__PURE__*/_jsxs("div", {
    className: "fcr-live-streaming-container",
    children: [/*#__PURE__*/_jsxs("div", {
      children: [/*#__PURE__*/_jsx(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__*/_jsx("div", {
        className: "fcr-live-streaming-container__error-tip",
        children: t('fmt_broadcast_tips_righturl')
      })]
    }), /*#__PURE__*/_jsxs("div", {
      className: "fcr-live-streaming-password-input",
      children: [/*#__PURE__*/_jsx(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__*/_jsx("div", {
        className: "fcr-live-streaming-container__error-tip",
        children: t('fmt_PSTN_tips_unempty')
      })]
    }), /*#__PURE__*/_jsxs("div", {
      children: [/*#__PURE__*/_jsxs("div", {
        className: "fcr-live-streaming-container-title",
        children: [/*#__PURE__*/_jsx("span", {
          className: "fcr-live-streaming-container-main-title",
          children: t('fmt_live_label_ivepage')
        }), /*#__PURE__*/_jsx("span", {
          className: "fcr-live-streaming-container-secondary-title",
          children: t('fmt_broadcast_tips_anyonewatch')
        })]
      }), /*#__PURE__*/_jsx(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__*/_jsx("div", {
        className: "fcr-live-streaming-container__error-tip",
        children: t('fmt_broadcast_tips_rightstreamingwebpage')
      }), isPlayUrlEmptyTip && /*#__PURE__*/_jsx("div", {
        className: "fcr-live-streaming-container__error-tip",
        children: t('fmt_PSTN_tips_unempty')
      })]
    }), /*#__PURE__*/_jsxs("div", {
      className: "fcr-live-streaming-container__start-live",
      children: [isStartError && /*#__PURE__*/_jsx("div", {
        className: "fcr-live-streaming-container__error",
        children: t('fmt_broadcast_tips_initiatefailed')
      }), isStarted && /*#__PURE__*/_jsx("div", {
        className: "fcr-live-streaming-container__started-tips",
        children: t('fmt_broadcast_tips_alreadybroadcasting')
      }), /*#__PURE__*/_jsx(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')
      })]
    })]
  });
};