"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.RecordingTypeSelector = exports.ClientRecordingPermissionSelector = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.object.to-string.js");
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _icon = require("../icon");
var _type = require("../icon/type");
var _i18n = require("../../i18n");
var _button = require("../button");
require("./index.css");
var _jsxRuntime = require("react/jsx-runtime");
// 使用 number 类型，避免与 fcr-core 的枚举值冲突
// FcrRecordingState: Starting = 1, Started = 2, Paused = 3, Stopped = 4
// FcrClientRecordingPermissionType: HostOnly = 1, AllMembers = 2, SpecifiedMembers = 3

// Recording state constants (matching fcr-core values)
// FcrRecordingState: Starting = 1, Started = 2, Paused = 3, Stopped = 4
var RECORDING_STATE_STOPPED = 4;
var RECORDING_STATE_PAUSED = 3;
var RecordingTypeSelector = exports.RecordingTypeSelector = function RecordingTypeSelector(props) {
  var hasStartCloudRecordingPermission = props.hasStartCloudRecordingPermission,
    _props$hasStartClient = props.hasStartClientRecordingPermission,
    hasStartClientRecordingPermission = _props$hasStartClient === void 0 ? true : _props$hasStartClient,
    cloudRecordingState = props.cloudRecordingState,
    isLocalClientRecording = props.isLocalClientRecording,
    _props$isCloudRecordi = props.isCloudRecordingSupported,
    isCloudRecordingSupported = _props$isCloudRecordi === void 0 ? true : _props$isCloudRecordi,
    _props$isClientRecord = props.isClientRecordingSupported,
    isClientRecordingSupported = _props$isClientRecord === void 0 ? true : _props$isClientRecord,
    pauseCloudRecording = props.pauseCloudRecording,
    resumeCloudRecording = props.resumeCloudRecording,
    stopCloudRecording = props.stopCloudRecording,
    startCloudRecording = props.startCloudRecording,
    startClientRecording = props.startClientRecording,
    stopClientRecording = props.stopClientRecording,
    onClose = props.onClose;
  var t = (0, _i18n.useI18n)();
  var isCloudRecording = cloudRecordingState !== RECORDING_STATE_STOPPED;
  var isCloudPaused = cloudRecordingState === RECORDING_STATE_PAUSED;
  var showCloudRecording = isCloudRecordingSupported;
  var showLocalRecording = isClientRecordingSupported;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "fcr-record-type-selector",
    children: [showCloudRecording && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "fcr-record-type-selector__item ".concat(!hasStartCloudRecordingPermission ? 'fcr-record-type-selector__item--disabled' : ''),
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-record-type-selector__item-icon fcr-record-type-selector__item-icon--cloud",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
          type: _type.FcrIconType.FCR_CLOUDRECORD,
          size: 28,
          colors: {
            iconPrimary: 'var(--fcr_ui_scene_white10)'
          }
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "fcr-record-type-selector__item-info",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr-record-type-selector__item-title",
          children: t('fmt_record_button_Cloudrecording')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr-record-type-selector__item-desc",
          children: t('fmt_record_type_cloud_desc')
        })]
      }), hasStartCloudRecordingPermission ? isCloudRecording ? /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "fcr-record-type-selector__item-actions",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_button.FcrButton, {
          size: "XXS",
          styleType: "white",
          className: "fcr-record-type-selector__item-btn--pause",
          onClick: function onClick() {
            if (isCloudPaused) {
              resumeCloudRecording();
            } else {
              pauseCloudRecording();
            }
            onClose();
          },
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
            size: 16,
            type: isCloudPaused ? _type.FcrIconType.FCR_RECORDING_PLAY : _type.FcrIconType.FCR_STOP,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_icontext1, #1d2129)'
            }
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            children: isCloudPaused ? t('fmt_record_popover_resume') : t('fmt_record_popover_pause')
          })]
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_button.FcrButton, {
          size: "XXS",
          shape: "rounded",
          styleType: "danger",
          className: "fcr-record-type-selector__item-btn--stop",
          onClick: function onClick() {
            stopCloudRecording();
            onClose();
          },
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
            size: 16,
            type: _type.FcrIconType.FCR_RECORDING_END,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_white10)'
            }
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            children: t('fmt_record_type_end')
          })]
        })]
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
        size: "XXS",
        className: "fcr-record-type-selector__item-btn",
        onClick: function onClick() {
          startCloudRecording();
          onClose();
        },
        children: t('fmt_localrecord_popup_open_recording')
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
        size: "XXS",
        className: "fcr-record-type-selector__item-btn",
        disabled: true,
        children: t('fmt_localrecord_popup_open_recording')
      })]
    }), showLocalRecording && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [showCloudRecording && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "fcr-record-type-selector__divider"
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "fcr-record-type-selector__item",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr-record-type-selector__item-icon fcr-record-type-selector__item-icon--local",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
            type: _type.FcrIconType.FCR_RECORDING_ON,
            size: 28,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_white10)'
            }
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "fcr-record-type-selector__item-info",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: "fcr-record-type-selector__item-title",
            children: t('fmt_record_button_Localrecording')
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: "fcr-record-type-selector__item-desc",
            children: t('fmt_record_type_local_desc')
          })]
        }), isLocalClientRecording ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "fcr-record-type-selector__item-actions",
          children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_button.FcrButton, {
            size: "XXS",
            styleType: "danger",
            className: "fcr-record-type-selector__item-btn--stop",
            onClick: function onClick() {
              stopClientRecording();
              onClose();
            },
            children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
              size: 12,
              type: _type.FcrIconType.FCR_RECORDING_END,
              colors: {
                iconPrimary: 'var(--fcr_ui_scene_white10)'
              }
            }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
              children: t('fmt_record_type_end')
            })]
          })
        }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.FcrButton, {
          size: "XXS",
          className: "fcr-record-type-selector__item-btn",
          onClick: function onClick() {
            startClientRecording();
            onClose();
          },
          children: t('fmt_localrecord_popup_open_recording')
        })]
      })]
    })]
  });
};
// Permission type constants (matching fcr-core values)
var PERMISSION_HOST_ONLY = 1;
var PERMISSION_ALL_MEMBERS = 2;
var PERMISSION_SPECIFIED_MEMBERS = 3;

// 权限范围大小映射：数值越大范围越大
// 所有人(2) > 指定人(3) > 主持人(1)
var PERMISSION_SCOPE_ORDER = (0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, PERMISSION_ALL_MEMBERS, 3), PERMISSION_SPECIFIED_MEMBERS, 2), PERMISSION_HOST_ONLY, 1);

/**
 * 判断权限范围是否变小
 */
var isScopeReduced = function isScopeReduced(currentType, newType) {
  var _PERMISSION_SCOPE_ORD2, _PERMISSION_SCOPE_ORD3;
  var currentScope = (_PERMISSION_SCOPE_ORD2 = PERMISSION_SCOPE_ORDER[currentType]) !== null && _PERMISSION_SCOPE_ORD2 !== void 0 ? _PERMISSION_SCOPE_ORD2 : 0;
  var newScope = (_PERMISSION_SCOPE_ORD3 = PERMISSION_SCOPE_ORDER[newType]) !== null && _PERMISSION_SCOPE_ORD3 !== void 0 ? _PERMISSION_SCOPE_ORD3 : 0;
  return newScope < currentScope;
};
var ClientRecordingPermissionSelector = exports.ClientRecordingPermissionSelector = function ClientRecordingPermissionSelector(props) {
  var clientRecordingPermissionType = props.clientRecordingPermissionType,
    setClientRecordingPermission = props.setClientRecordingPermission,
    onClose = props.onClose,
    onConfirmScopeReduction = props.onConfirmScopeReduction;
  var t = (0, _i18n.useI18n)();
  var options = [{
    label: t('fmt_localrecord_options_onlyadmin_record'),
    value: PERMISSION_HOST_ONLY
  }, {
    label: t('fmt_localrecord_options_allmember_canrecord'),
    value: PERMISSION_ALL_MEMBERS
  }, {
    label: t('fmt_localrecord_options_specified_members_record'),
    value: PERMISSION_SPECIFIED_MEMBERS
  }];
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "fcr-record-permission-selector",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "fcr-record-permission-selector__title",
      children: t('fmt_localrecord_labels_permissions_localrecording')
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "fcr-record-permission-selector__divider"
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "fcr-record-permission-selector__options",
      children: options.map(function (option) {
        return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "fcr-record-permission-selector__option ".concat(clientRecordingPermissionType === option.value ? 'fcr-record-permission-selector__option--active' : ''),
          onClick: /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
            var confirmed;
            return _regenerator["default"].wrap(function (_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  if (!(option.value !== clientRecordingPermissionType)) {
                    _context.next = 3;
                    break;
                  }
                  if (!(onConfirmScopeReduction && isScopeReduced(clientRecordingPermissionType, option.value))) {
                    _context.next = 2;
                    break;
                  }
                  _context.next = 1;
                  return onConfirmScopeReduction(option.value);
                case 1:
                  confirmed = _context.sent;
                  if (confirmed) {
                    _context.next = 2;
                    break;
                  }
                  return _context.abrupt("return");
                case 2:
                  setClientRecordingPermission(option.value);
                case 3:
                  onClose();
                case 4:
                case "end":
                  return _context.stop();
              }
            }, _callee);
          })),
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
            className: "fcr-record-permission-selector__option-label",
            children: option.label
          }), clientRecordingPermissionType === option.value && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.FcrIcon, {
            type: _type.FcrIconType.FCR_CHECKBOX_CHECK,
            size: 20,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_icontext1, #1d2129)'
            }
          })]
        }, option.value);
      })
    })]
  });
};