"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactHelmet = require("react-helmet");
var _get = _interopRequireDefault(require("lodash/get"));
var _ImageViewer = _interopRequireDefault(require("../media/ImageViewer"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (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; }
const propTypes = {
  location: _propTypes.default.shape({
    search: _propTypes.default.string
  }),
  match: _propTypes.default.shape({
    params: _propTypes.default.object
  }),
  readContent: _propTypes.default.func.isRequired,
  renderError: _propTypes.default.func,
  renderLoading: _propTypes.default.func
};
class ContentViewerPage extends _react.Component {
  constructor() {
    super();
    this.state = {
      blobUrl: null
    };
  }
  componentDidMount() {
    this.readContent();
  }
  componentDidUpdate(prevProps) {
    const {
      match
    } = this.props;
    const {
      match: prevMatch
    } = prevProps;
    const contentPath = (0, _get.default)(match, ['params', 'contentPath']);
    const prevContentPath = (0, _get.default)(prevMatch, ['params', 'contentPath']);
    if (contentPath !== prevContentPath) {
      this.reset();
      this.readContent();
    }
  }
  componentWillUnmount() {
    this.reset();
    this.isUnmounted = true;
  }
  reset() {
    const {
      blobUrl
    } = this.state;
    if (blobUrl) {
      URL.revokeObjectURL(blobUrl);
      this.setState({
        blobUrl: null
      });
    }
  }
  readContent() {
    const {
      location,
      match,
      readContent
    } = this.props;
    readContent(location, match).then(response => {
      if (response && response.status === 200 && response.data && !this.isUnmounted) {
        const contentDisposition = response.headers['content-disposition'];
        const filename = contentDisposition && contentDisposition.match(/filename="(.*?)"/) ? RegExp.$1 : null;
        this.setState({
          filename,
          blobUrl: URL.createObjectURL(response.data),
          contentType: response.headers['content-type']
        });
      }
    }).catch(error => {
      this.setState({
        error
      });
    });
  }
  renderImageViewer() {
    const {
      blobUrl,
      filename
    } = this.state;
    return /*#__PURE__*/_react.default.createElement(_ImageViewer.default, {
      blobUrl: blobUrl,
      filename: filename
    });
  }
  renderBlobViewer() {
    const {
      blobUrl
    } = this.state;
    const style = {
      position: 'fixed',
      left: '0',
      top: '0',
      width: '100%',
      height: '100%',
      border: 'none'
    };

    // Load the blob into an iframe to get the default viewer.

    return (
      /*#__PURE__*/
      // TODO: Make title a message.
      _react.default.createElement("iframe", {
        src: blobUrl,
        style: style,
        title: "File content"
      })
    );
  }
  render() {
    const {
      renderError,
      renderLoading
    } = this.props;
    const {
      blobUrl,
      contentType,
      error,
      filename
    } = this.state;
    if (error) {
      return renderError ? renderError(error) : null;
    }
    if (!blobUrl) {
      return renderLoading ? renderLoading() : null;
    }
    const title = filename ? /*#__PURE__*/_react.default.createElement(_reactHelmet.Helmet, null, /*#__PURE__*/_react.default.createElement("title", null, filename)) : null;
    let viewer;
    if (contentType.startsWith('image/')) {
      viewer = this.renderImageViewer();
    } else {
      viewer = this.renderBlobViewer();
    }
    return /*#__PURE__*/_react.default.createElement("div", null, title, viewer);
  }
}
exports.default = ContentViewerPage;
ContentViewerPage.propTypes = propTypes;