"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _title = _interopRequireDefault(require("../title"));

var _context = _interopRequireDefault(require("../context"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * 全屏切换
 *
 * @class FullscreenSwitch
 * @extends {PureComponent<IProps, IState>}
 */
var FullscreenSwitch = /*#__PURE__*/function (_PureComponent) {
  (0, _inheritsLoose2["default"])(FullscreenSwitch, _PureComponent);

  function FullscreenSwitch(props) {
    var _this;

    _this = _PureComponent.call(this, props) || this;

    _this.openFullscreen = function (elem) {
      if (elem.requestFullscreen) {
        elem.requestFullscreen();
      } else if (elem.mozRequestFullScreen) {
        /* Firefox */
        elem.mozRequestFullScreen();
      } else if (elem.webkitRequestFullscreen) {
        /* Chrome, Safari and Opera */
        elem.webkitRequestFullscreen();
      } else if (elem.msRequestFullscreen) {
        /* IE/Edge */
        elem.msRequestFullscreen();
      }
    };

    _this.closeFullscreen = function () {
      if (document.exitFullscreen) {
        document.exitFullscreen();
      } else if (document.mozCancelFullScreen) {
        /* Firefox */
        document.mozCancelFullScreen();
      } else if (document.webkitExitFullscreen) {
        /* Chrome, Safari and Opera */
        document.webkitExitFullscreen();
      } else if (document.msExitFullscreen) {
        /* IE/Edge */
        document.msExitFullscreen();
      }
    };

    _this.handleListenFullScreenChange = function () {
      var isFullscreen = document.fullscreenElement || document.msFullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || false;

      _this.setState({
        isFullscreen: !!isFullscreen
      }, function () {
        if (_this.props.onChange) _this.props.onChange(_this.state.isFullscreen);
      });
    };

    _this.handleToggleFullScreen = function () {
      var isFullscreen = _this.state.isFullscreen;
      var _this$props = _this.props,
          target = _this$props.target,
          onClick = _this$props.onClick;
      var elem = typeof target === 'string' ? document.querySelector(target) : target;

      if (onClick) {
        onClick();
      }

      if (!isFullscreen) {
        _this.openFullscreen(elem);
      } else {
        _this.closeFullscreen();
      }
    };

    _this.state = {
      isFullscreen: false // 是否全屏

    };
    return _this;
  }

  var _proto = FullscreenSwitch.prototype;

  _proto.componentDidMount = function componentDidMount() {
    document.addEventListener('webkitfullscreenchange', this.handleListenFullScreenChange);
    document.addEventListener('mozfullscreenchange', this.handleListenFullScreenChange);
    document.addEventListener('fullscreenchange', this.handleListenFullScreenChange);
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    document.removeEventListener('webkitfullscreenchange', this.handleListenFullScreenChange);
    document.removeEventListener('mozfullscreenchange', this.handleListenFullScreenChange);
    document.removeEventListener('fullscreenchange', this.handleListenFullScreenChange);
  };

  _proto.render = function render() {
    var _cssPrefix = this.context._cssPrefix;
    var _this$props2 = this.props,
        className = _this$props2.className,
        local = _this$props2.local;
    var titleOptions = (0, _extends2["default"])({}, this.props);
    var isFullscreen = this.state.isFullscreen;
    delete titleOptions.target;
    delete titleOptions.local;
    delete titleOptions.onChange;
    return /*#__PURE__*/_react["default"].createElement(_title["default"], (0, _extends2["default"])({
      align: "middle"
    }, titleOptions, {
      className: (0, _classnames["default"])(className, _cssPrefix + "fullscreen-switch"),
      onClick: this.handleToggleFullScreen
    }), /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])(_cssPrefix + "iconfont-ability", {
        active: isFullscreen
      }),
      dangerouslySetInnerHTML: {
        __html: isFullscreen ? '&#xe82e;' : '&#xe82f;'
      }
    }), isFullscreen ? local.active : local["default"]);
  };

  return FullscreenSwitch;
}(_react.PureComponent); // @ts-ignore


FullscreenSwitch.defaultProps = {
  className: null,
  target: null,
  onChange: function onChange() {},
  local: {
    "default": '全屏模式',
    active: '退出全屏'
  },
  textStyle: {
    fontWeight: 400
  }
};
FullscreenSwitch.contextType = _context["default"];
var _default = FullscreenSwitch;
exports["default"] = _default;