/* doc-viewer.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.default = exports.DocViewer = void 0;
require("./doc-viewer.compiled.css");
var _runtime = require("@compiled/react/runtime");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _mediaClient = require("@atlaskit/media-client");
var _mediaDocumentViewer = require("@atlaskit/media-document-viewer");
var _mediaCommon = require("@atlaskit/media-common");
var _errors = require("../../errors");
var _zoomControls = require("../../zoomControls");
var _zoomLevel = require("../../domain/zoomLevel");
var _passwordInput = require("./passwordInput");
var _spinner = _interopRequireDefault(require("@atlaskit/spinner"));
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var initialState = {
  isMissingPassword: false,
  hasPasswordError: false,
  password: '',
  zoomLevel: new _zoomLevel.ZoomLevel(1.75),
  isLoading: true
};
var reducer = function reducer(state, action) {
  switch (action.type) {
    case 'submit_password':
      return _objectSpread(_objectSpread({}, state), {}, {
        password: action.password
      });
    case 'set_zoom_level':
      return _objectSpread(_objectSpread({}, state), {}, {
        zoomLevel: action.zoomLevel
      });
    case 'password_validation_failed':
      return _objectSpread(_objectSpread({}, state), {}, {
        hasPasswordError: true
      });
    case 'password_validation_succeeded':
      return _objectSpread(_objectSpread({}, state), {}, {
        isMissingPassword: false,
        hasPasswordError: false
      });
    case 'content_fetch_failed_password_required':
      return _objectSpread(_objectSpread({}, state), {}, {
        isMissingPassword: true
      });
    case 'loading_completed':
      return _objectSpread(_objectSpread({}, state), {}, {
        isLoading: false
      });
    default:
      return state;
  }
};
var documentViewerStyles = null;
var spinnerStyles = null;
var DocViewer = exports.DocViewer = function DocViewer(_ref) {
  var mediaClient = _ref.mediaClient,
    fileState = _ref.fileState,
    collectionName = _ref.collectionName,
    onError = _ref.onError,
    onSuccess = _ref.onSuccess,
    traceContext = _ref.traceContext;
  var _useReducer = (0, _react.useReducer)(reducer, initialState),
    _useReducer2 = (0, _slicedToArray2.default)(_useReducer, 2),
    state = _useReducer2[0],
    dispatch = _useReducer2[1];
  var getContent = (0, _mediaCommon.useStaticCallback)( /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(pageStart, pageEnd) {
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            _context.next = 3;
            return mediaClient.mediaStore.getDocumentContent(fileState.id, {
              pageStart: pageStart,
              pageEnd: pageEnd,
              collectionName: collectionName,
              password: state.password
            }, traceContext);
          case 3:
            return _context.abrupt("return", _context.sent);
          case 6:
            _context.prev = 6;
            _context.t0 = _context["catch"](0);
            if ((0, _mediaClient.isCommonMediaClientError)(_context.t0) && _context.t0.reason === 'serverEntityLocked') {
              dispatch({
                type: 'content_fetch_failed_password_required'
              });
            } else {
              onError(new _errors.MediaViewerError('docviewer-content-fetch-failed', _context.t0 instanceof Error ? _context.t0 : undefined));
            }
            throw _context.t0;
          case 10:
          case "end":
            return _context.stop();
        }
      }, _callee, null, [[0, 6]]);
    }));
    return function (_x, _x2) {
      return _ref2.apply(this, arguments);
    };
  }());
  var onPasswordSubmit = (0, _mediaCommon.useStaticCallback)( /*#__PURE__*/function () {
    var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(data) {
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            dispatch({
              type: 'submit_password',
              password: data.password
            });
            _context2.prev = 1;
            _context2.next = 4;
            return mediaClient.mediaStore.getDocumentContent(fileState.id, {
              pageStart: 0,
              pageEnd: 1,
              collectionName: collectionName,
              password: data.password
            }, traceContext);
          case 4:
            dispatch({
              type: 'password_validation_succeeded'
            });
            _context2.next = 10;
            break;
          case 7:
            _context2.prev = 7;
            _context2.t0 = _context2["catch"](1);
            if ((0, _mediaClient.isCommonMediaClientError)(_context2.t0) && _context2.t0.reason === 'serverEntityLocked') {
              dispatch({
                type: 'password_validation_failed'
              });
            } else {
              onError(new _errors.MediaViewerError('docviewer-content-fetch-failed', _context2.t0 instanceof Error ? _context2.t0 : undefined));
            }
          case 10:
          case "end":
            return _context2.stop();
        }
      }, _callee2, null, [[1, 7]]);
    }));
    return function (_x3) {
      return _ref3.apply(this, arguments);
    };
  }());
  var getPageImageUrl = (0, _mediaCommon.useStaticCallback)( /*#__PURE__*/function () {
    var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(pageNumber, zoom) {
      var src;
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            _context3.next = 2;
            return mediaClient.mediaStore.getDocumentPageImage(fileState.id, {
              page: pageNumber,
              zoom: zoom,
              collectionName: collectionName,
              password: state.password
            }, traceContext);
          case 2:
            src = _context3.sent;
            return _context3.abrupt("return", URL.createObjectURL(src));
          case 4:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    }));
    return function (_x4, _x5) {
      return _ref4.apply(this, arguments);
    };
  }());
  var onSuccessHandler = (0, _mediaCommon.useStaticCallback)(function () {
    dispatch({
      type: 'loading_completed'
    });
    onSuccess === null || onSuccess === void 0 || onSuccess();
  });
  var onZoomChange = (0, _mediaCommon.useStaticCallback)(function (newZoomLevel) {
    dispatch({
      type: 'set_zoom_level',
      zoomLevel: newZoomLevel
    });
  });
  if (state.isMissingPassword) {
    return /*#__PURE__*/_react.default.createElement(_passwordInput.PasswordInput, {
      onSubmit: onPasswordSubmit,
      hasPasswordError: state.hasPasswordError
    });
  }
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, state.isLoading && /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _runtime.ax)(["_kqswstnw _1e0c1txw _1bah1h6o _4cvr1h6o"])
  }, /*#__PURE__*/_react.default.createElement(_spinner.default, {
    size: "large",
    appearance: "invert"
  })), /*#__PURE__*/_react.default.createElement("div", {
    style: {
      opacity: state.isLoading ? 0 : 1
    },
    id: _mediaDocumentViewer.DOCUMENT_SCROLL_ROOT_ID,
    className: (0, _runtime.ax)(["_1reo1wug _18m91wug _4t3i1kxc _1bsbauwl"])
  }, /*#__PURE__*/_react.default.createElement(_mediaDocumentViewer.DocumentViewer, {
    getContent: getContent,
    getPageImageUrl: getPageImageUrl,
    zoom: state.zoomLevel.value,
    onSuccess: onSuccessHandler
  }), /*#__PURE__*/_react.default.createElement(_zoomControls.ZoomControls, {
    onChange: onZoomChange,
    zoomLevel: state.zoomLevel
  })));
};
var _default = exports.default = DocViewer;