/* codeViewerRenderer-compiled.tsx generated by @compiled/babel-plugin v0.38.1 */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CodeViewerHeaderBar = exports.CodeViewWrapper = exports.CodeViewRenderer = void 0;
require("./codeViewerRenderer-compiled.compiled.css");
var _react = _interopRequireWildcard(require("react"));
var React = _react;
var _runtime = require("@compiled/react/runtime");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _domain = require("../../domain");
var _loading = require("../../loading");
var _block = _interopRequireDefault(require("@atlaskit/code/block"));
var _errorMessage = _interopRequireDefault(require("../../errorMessage"));
var _errors = require("../../errors");
var _util = require("./util");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
var codeViewWrapperStyles = null;
var codeViewerHeaderBarStyles = null;
var codeViewerHTMLStyles = null;

// Based on some basic benchmarking with @atlaskit/code it was found that ~10,000 lines took around ~5secs to render, which locks the main thread.
// Therefore we set a hard limit on the amount of lines which we apply formatting to,
// otherwise the "text" language will be used which is plain and more performant
var MAX_FORMATTED_LINES = 10000;
// Use plain html to render code file if their size exceeds 5MB.
// Required by https://product-fabric.atlassian.net/browse/MEX-1788
var MAX_FILE_SIZE_USE_CODE_VIEWER = 5 * 1024 * 1024;
var CodeViewWrapper = exports.CodeViewWrapper = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var children = _ref.children,
    testId = _ref['data-testid'];
  return /*#__PURE__*/React.createElement("div", {
    "data-testid": testId,
    ref: ref,
    className: (0, _runtime.ax)(["_1reo1wug _18m91wug _kqswstnw _1ltvidpf _154iidpf _1xi2idpf _94n5idpf _bfhk15j7"])
  }, children);
});
var CodeViewerHeaderBar = exports.CodeViewerHeaderBar = function CodeViewerHeaderBar() {
  return /*#__PURE__*/React.createElement("div", {
    className: (0, _runtime.ax)(["_4t3i8ved _bfhk1nox"])
  });
};
var initialState = {
  doc: _domain.Outcome.pending()
};
var CodeViewRenderer = exports.CodeViewRenderer = /*#__PURE__*/function (_Component) {
  function CodeViewRenderer() {
    var _this;
    (0, _classCallCheck2.default)(this, CodeViewRenderer);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, CodeViewRenderer, [].concat(args));
    (0, _defineProperty2.default)(_this, "state", initialState);
    return _this;
  }
  (0, _inherits2.default)(CodeViewRenderer, _Component);
  return (0, _createClass2.default)(CodeViewRenderer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.init();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {}
  }, {
    key: "init",
    value: function () {
      var _init = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
        var _this$props, src, onSuccess, onError, mediaError;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _this$props = this.props, src = _this$props.src, onSuccess = _this$props.onSuccess, onError = _this$props.onError;
              try {
                this.setState({
                  doc: _domain.Outcome.successful(src)
                });
                if (onSuccess) {
                  onSuccess();
                }
              } catch (error) {
                mediaError = new _errors.MediaViewerError('codeviewer-load-src', error instanceof Error ? error : undefined);
                this.setState({
                  doc: _domain.Outcome.failed(mediaError)
                });
                if (onError) {
                  onError(mediaError);
                }
              }
            case 2:
            case "end":
              return _context.stop();
          }
        }, _callee, this);
      }));
      function init() {
        return _init.apply(this, arguments);
      }
      return init;
    }()
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
        item = _this$props2.item,
        src = _this$props2.src,
        language = _this$props2.language,
        testId = _this$props2.testId;
      //Use src to measure the real file size
      //item.size is incorrect for archives with mutiple docs inside.
      var fileSize = new Blob([src]).size;
      var codeViewer = (0, _util.lineCount)(src) > MAX_FORMATTED_LINES || fileSize > MAX_FILE_SIZE_USE_CODE_VIEWER ? /*#__PURE__*/React.createElement("code", {
        "data-testid": "code-block",
        className: (0, _runtime.ax)(["_11c8dcr7 _1e0c1txw _1reo1wug _o572qvpr _ca0qu2gc _u5f3u2gc _n3tdu2gc _19bvu2gc"])
      }, src) : /*#__PURE__*/React.createElement(_block.default, {
        language: language,
        text: src,
        testId: "code-block"
      });
      return this.state.doc.match({
        pending: function pending() {
          return /*#__PURE__*/React.createElement(_loading.Spinner, null);
        },
        successful: function successful() {
          return /*#__PURE__*/React.createElement(CodeViewWrapper, {
            "data-testid": testId
          }, /*#__PURE__*/React.createElement(CodeViewerHeaderBar, null), codeViewer);
        },
        failed: function failed(error) {
          return /*#__PURE__*/React.createElement(_errorMessage.default, {
            fileId: item.id,
            fileState: item,
            error: error,
            supressAnalytics: true // item-viewer.tsx will send
          });
        }
      });
    }
  }]);
}(_react.Component);