/* codeViewerRenderer-compiled.tsx generated by @compiled/babel-plugin v0.38.1 */
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import "./codeViewerRenderer-compiled.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import _regeneratorRuntime from "@babel/runtime/regenerator";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(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

import { Component, forwardRef } from 'react';
import { Outcome } from '../../domain';
import { Spinner } from '../../loading';
import CodeBlock from '@atlaskit/code/block';
import ErrorMessage from '../../errorMessage';
import { MediaViewerError } from '../../errors';
import { lineCount } from './util';
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;
export var CodeViewWrapper = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var children = _ref.children,
    testId = _ref['data-testid'];
  return /*#__PURE__*/React.createElement("div", {
    "data-testid": testId,
    ref: ref,
    className: ax(["_1reo1wug _18m91wug _kqswstnw _1ltvidpf _154iidpf _1xi2idpf _94n5idpf _bfhk15j7"])
  }, children);
});
export var CodeViewerHeaderBar = function CodeViewerHeaderBar() {
  return /*#__PURE__*/React.createElement("div", {
    className: ax(["_4t3i8ved _bfhk1nox"])
  });
};
var initialState = {
  doc: Outcome.pending()
};
export var CodeViewRenderer = /*#__PURE__*/function (_Component) {
  function CodeViewRenderer() {
    var _this;
    _classCallCheck(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));
    _defineProperty(_this, "state", initialState);
    return _this;
  }
  _inherits(CodeViewRenderer, _Component);
  return _createClass(CodeViewRenderer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.init();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {}
  }, {
    key: "init",
    value: function () {
      var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var _this$props, src, onSuccess, onError, mediaError;
        return _regeneratorRuntime.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: Outcome.successful(src)
                });
                if (onSuccess) {
                  onSuccess();
                }
              } catch (error) {
                mediaError = new MediaViewerError('codeviewer-load-src', error instanceof Error ? error : undefined);
                this.setState({
                  doc: 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 = lineCount(src) > MAX_FORMATTED_LINES || fileSize > MAX_FILE_SIZE_USE_CODE_VIEWER ? /*#__PURE__*/React.createElement("code", {
        "data-testid": "code-block",
        className: ax(["_11c8dcr7 _1e0c1txw _1reo1wug _o572qvpr _ca0qu2gc _u5f3u2gc _n3tdu2gc _19bvu2gc"])
      }, src) : /*#__PURE__*/React.createElement(CodeBlock, {
        language: language,
        text: src,
        testId: "code-block"
      });
      return this.state.doc.match({
        pending: function pending() {
          return /*#__PURE__*/React.createElement(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, {
            fileId: item.id,
            fileState: item,
            error: error,
            supressAnalytics: true // item-viewer.tsx will send
          });
        }
      });
    }
  }]);
}(Component);