import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import "core-js/modules/es.array.map.js";
import "core-js/modules/es.object.to-string.js";
import { FcrIcon } from '../icon';
import { FcrIconType } from '../icon/type';
import { useI18n } from '../../i18n';
import { FcrButton } from '../button';
import './index.css';

// 使用 number 类型，避免与 fcr-core 的枚举值冲突
// FcrRecordingState: Starting = 1, Started = 2, Paused = 3, Stopped = 4
// FcrClientRecordingPermissionType: HostOnly = 1, AllMembers = 2, SpecifiedMembers = 3
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
// 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;
export var 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 = useI18n();
  var isCloudRecording = cloudRecordingState !== RECORDING_STATE_STOPPED;
  var isCloudPaused = cloudRecordingState === RECORDING_STATE_PAUSED;
  var showCloudRecording = isCloudRecordingSupported;
  var showLocalRecording = isClientRecordingSupported;
  return /*#__PURE__*/_jsxs("div", {
    className: "fcr-record-type-selector",
    children: [showCloudRecording && /*#__PURE__*/_jsxs("div", {
      className: "fcr-record-type-selector__item ".concat(!hasStartCloudRecordingPermission ? 'fcr-record-type-selector__item--disabled' : ''),
      children: [/*#__PURE__*/_jsx("div", {
        className: "fcr-record-type-selector__item-icon fcr-record-type-selector__item-icon--cloud",
        children: /*#__PURE__*/_jsx(FcrIcon, {
          type: FcrIconType.FCR_CLOUDRECORD,
          size: 28,
          colors: {
            iconPrimary: 'var(--fcr_ui_scene_white10)'
          }
        })
      }), /*#__PURE__*/_jsxs("div", {
        className: "fcr-record-type-selector__item-info",
        children: [/*#__PURE__*/_jsx("div", {
          className: "fcr-record-type-selector__item-title",
          children: t('fmt_record_button_Cloudrecording')
        }), /*#__PURE__*/_jsx("div", {
          className: "fcr-record-type-selector__item-desc",
          children: t('fmt_record_type_cloud_desc')
        })]
      }), hasStartCloudRecordingPermission ? isCloudRecording ? /*#__PURE__*/_jsxs("div", {
        className: "fcr-record-type-selector__item-actions",
        children: [/*#__PURE__*/_jsxs(FcrButton, {
          size: "XXS",
          styleType: "white",
          className: "fcr-record-type-selector__item-btn--pause",
          onClick: function onClick() {
            if (isCloudPaused) {
              resumeCloudRecording();
            } else {
              pauseCloudRecording();
            }
            onClose();
          },
          children: [/*#__PURE__*/_jsx(FcrIcon, {
            size: 16,
            type: isCloudPaused ? FcrIconType.FCR_RECORDING_PLAY : FcrIconType.FCR_STOP,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_icontext1, #1d2129)'
            }
          }), /*#__PURE__*/_jsx("span", {
            children: isCloudPaused ? t('fmt_record_popover_resume') : t('fmt_record_popover_pause')
          })]
        }), /*#__PURE__*/_jsxs(FcrButton, {
          size: "XXS",
          shape: "rounded",
          styleType: "danger",
          className: "fcr-record-type-selector__item-btn--stop",
          onClick: function onClick() {
            stopCloudRecording();
            onClose();
          },
          children: [/*#__PURE__*/_jsx(FcrIcon, {
            size: 16,
            type: FcrIconType.FCR_RECORDING_END,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_white10)'
            }
          }), /*#__PURE__*/_jsx("span", {
            children: t('fmt_record_type_end')
          })]
        })]
      }) : /*#__PURE__*/_jsx(FcrButton, {
        size: "XXS",
        className: "fcr-record-type-selector__item-btn",
        onClick: function onClick() {
          startCloudRecording();
          onClose();
        },
        children: t('fmt_localrecord_popup_open_recording')
      }) : /*#__PURE__*/_jsx(FcrButton, {
        size: "XXS",
        className: "fcr-record-type-selector__item-btn",
        disabled: true,
        children: t('fmt_localrecord_popup_open_recording')
      })]
    }), showLocalRecording && /*#__PURE__*/_jsxs(_Fragment, {
      children: [showCloudRecording && /*#__PURE__*/_jsx("div", {
        className: "fcr-record-type-selector__divider"
      }), /*#__PURE__*/_jsxs("div", {
        className: "fcr-record-type-selector__item",
        children: [/*#__PURE__*/_jsx("div", {
          className: "fcr-record-type-selector__item-icon fcr-record-type-selector__item-icon--local",
          children: /*#__PURE__*/_jsx(FcrIcon, {
            type: FcrIconType.FCR_RECORDING_ON,
            size: 28,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_white10)'
            }
          })
        }), /*#__PURE__*/_jsxs("div", {
          className: "fcr-record-type-selector__item-info",
          children: [/*#__PURE__*/_jsx("div", {
            className: "fcr-record-type-selector__item-title",
            children: t('fmt_record_button_Localrecording')
          }), /*#__PURE__*/_jsx("div", {
            className: "fcr-record-type-selector__item-desc",
            children: t('fmt_record_type_local_desc')
          })]
        }), isLocalClientRecording ? /*#__PURE__*/_jsx("div", {
          className: "fcr-record-type-selector__item-actions",
          children: /*#__PURE__*/_jsxs(FcrButton, {
            size: "XXS",
            styleType: "danger",
            className: "fcr-record-type-selector__item-btn--stop",
            onClick: function onClick() {
              stopClientRecording();
              onClose();
            },
            children: [/*#__PURE__*/_jsx(FcrIcon, {
              size: 12,
              type: FcrIconType.FCR_RECORDING_END,
              colors: {
                iconPrimary: 'var(--fcr_ui_scene_white10)'
              }
            }), /*#__PURE__*/_jsx("span", {
              children: t('fmt_record_type_end')
            })]
          })
        }) : /*#__PURE__*/_jsx(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 = _defineProperty(_defineProperty(_defineProperty({}, 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;
};
export var ClientRecordingPermissionSelector = function ClientRecordingPermissionSelector(props) {
  var clientRecordingPermissionType = props.clientRecordingPermissionType,
    setClientRecordingPermission = props.setClientRecordingPermission,
    onClose = props.onClose,
    onConfirmScopeReduction = props.onConfirmScopeReduction;
  var t = 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__*/_jsxs("div", {
    className: "fcr-record-permission-selector",
    children: [/*#__PURE__*/_jsx("div", {
      className: "fcr-record-permission-selector__title",
      children: t('fmt_localrecord_labels_permissions_localrecording')
    }), /*#__PURE__*/_jsx("div", {
      className: "fcr-record-permission-selector__divider"
    }), /*#__PURE__*/_jsx("div", {
      className: "fcr-record-permission-selector__options",
      children: options.map(function (option) {
        return /*#__PURE__*/_jsxs("div", {
          className: "fcr-record-permission-selector__option ".concat(clientRecordingPermissionType === option.value ? 'fcr-record-permission-selector__option--active' : ''),
          onClick: /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
            var confirmed;
            return _regeneratorRuntime.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__*/_jsx("span", {
            className: "fcr-record-permission-selector__option-label",
            children: option.label
          }), clientRecordingPermissionType === option.value && /*#__PURE__*/_jsx(FcrIcon, {
            type: FcrIconType.FCR_CHECKBOX_CHECK,
            size: 20,
            colors: {
              iconPrimary: 'var(--fcr_ui_scene_icontext1, #1d2129)'
            }
          })]
        }, option.value);
      })
    })]
  });
};