"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _openseadragon = _interopRequireDefault(require("openseadragon"));

var _blank = _interopRequireDefault(require("../../../images/blank.svg"));

var _imageFullscreen = _interopRequireDefault(require("../../../images/imageFullscreen.svg"));

var _imageFullscreenActive = _interopRequireDefault(require("../../../images/imageFullscreenActive.svg"));

var _imageHome = _interopRequireDefault(require("../../../images/imageHome.svg"));

var _imageHomeActive = _interopRequireDefault(require("../../../images/imageHomeActive.svg"));

var _imageZoomIn = _interopRequireDefault(require("../../../images/imageZoomIn.svg"));

var _imageZoomInActive = _interopRequireDefault(require("../../../images/imageZoomInActive.svg"));

var _imageZoomOut = _interopRequireDefault(require("../../../images/imageZoomOut.svg"));

var _imageZoomOutActive = _interopRequireDefault(require("../../../images/imageZoomOutActive.svg"));

var _ImageViewer = _interopRequireDefault(require("../../../styles/cspace-ui/ImageViewer.css"));

var _ImageViewerSaveLink = _interopRequireDefault(require("../../../styles/cspace-ui/ImageViewerSaveLink.css"));

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 document */
const propTypes = {
  blobUrl: _propTypes.default.string.isRequired,
  filename: _propTypes.default.string
};
const defaultProps = {
  filename: ''
};

class ImageViewer extends _react.Component {
  constructor() {
    super();
    this.handleContainerRef = this.handleContainerRef.bind(this);
  }

  componentDidMount() {
    const {
      blobUrl,
      filename
    } = this.props;
    const viewer = (0, _openseadragon.default)({
      element: this.containerElement,
      navImages: {
        zoomIn: {
          REST: _imageZoomIn.default,
          GROUP: _blank.default,
          HOVER: _imageZoomInActive.default,
          DOWN: _imageZoomInActive.default
        },
        zoomOut: {
          REST: _imageZoomOut.default,
          GROUP: _blank.default,
          HOVER: _imageZoomOutActive.default,
          DOWN: _imageZoomOutActive.default
        },
        home: {
          REST: _imageHome.default,
          GROUP: _blank.default,
          HOVER: _imageHomeActive.default,
          DOWN: _imageHomeActive.default
        },
        fullpage: {
          REST: _imageFullscreen.default,
          GROUP: _blank.default,
          HOVER: _imageFullscreenActive.default,
          DOWN: _imageFullscreenActive.default
        }
      },
      prefixUrl: '',
      showNavigator: true,
      tileSources: {
        type: 'image',
        url: blobUrl
      }
    });
    const saveLink = document.createElement('a');
    saveLink.className = _ImageViewerSaveLink.default.common;
    saveLink.href = blobUrl;
    saveLink.download = filename;
    saveLink.title = 'Save';
    viewer.buttons.element.appendChild(saveLink);
    this.viewer = viewer;
  }

  componentWillUnmount() {
    if (this.viewer) {
      this.viewer.destroy();
    }

    this.viewer = null;
  }

  handleContainerRef(ref) {
    this.containerElement = ref;
  }

  render() {
    return _react.default.createElement("div", {
      className: _ImageViewer.default.common
    }, _react.default.createElement("div", {
      ref: this.handleContainerRef
    }));
  }

}

exports.default = ImageViewer;
ImageViewer.propTypes = propTypes;
ImageViewer.defaultProps = defaultProps;