"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 = _interopRequireDefault(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 _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

/* global URL */
const propTypes = {
  location: _propTypes.default.object,
  match: _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 contentPath = (0, _get.default)(this.props.match, ['params', 'contentPath']);
    const prevContentPath = (0, _get.default)(prevProps.match, ['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 _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 _react.default.createElement("iframe", {
      src: blobUrl,
      style: style
    });
  }

  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 ? _react.default.createElement(_reactHelmet.default, null, _react.default.createElement("title", null, filename)) : null;
    let viewer;

    if (contentType.startsWith('image/')) {
      viewer = this.renderImageViewer();
    } else {
      viewer = this.renderBlobViewer();
    }

    return _react.default.createElement("div", null, title, viewer);
  }

}

exports.default = ContentViewerPage;
ContentViewerPage.propTypes = propTypes;