"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = Footer;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _immutable = _interopRequireDefault(require("immutable"));
var _reactIntl = require("react-intl");
var _Footer = _interopRequireDefault(require("../../../styles/cspace-ui/Footer.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } /* global cspaceUI */ /* The cspaceUI global variable is set by webpack. See webpack.config.js and karma.conf.js. */
const messages = (0, _reactIntl.defineMessages)({
  copyright: {
    "id": "footer.copyright",
    "defaultMessage": "Copyright \xA9 {startYear}\u2013{endYear} CollectionSpace"
  },
  about: {
    "id": "footer.about",
    "defaultMessage": "About CollectionSpace"
  },
  feedback: {
    "id": "footer.feedback",
    "defaultMessage": "Leave Feedback"
  },
  feedbackUrl: {
    "id": "footer.feedbackUrl",
    "defaultMessage": "http://www.collectionspace.org/contact"
  },
  release: {
    "id": "footer.release",
    "defaultMessage": "Release {version}"
  },
  appName: {
    "id": "footer.appName",
    "defaultMessage": "UI"
  },
  version: {
    "id": "footer.version",
    "defaultMessage": "{name} version {version}"
  },
  notConnected: {
    "id": "footer.notConnected",
    "defaultMessage": "Not connected to {serverUrl}"
  }
});
const propTypes = {
  config: _propTypes.default.shape({
    pluginInfo: _propTypes.default.object,
    serverUrl: _propTypes.default.string
  }),
  intl: _reactIntl.intlShape.isRequired,
  systemInfo: _propTypes.default.instanceOf(_immutable.default.Map)
};
const isSnapshot = systemInfo => {
  const patch = systemInfo && systemInfo.getIn(['ns2:system_info_common', 'version', 'patch']);
  return patch && patch.includes('-SNAPSHOT');
};
const renderServicesReleaseVersion = systemInfo => {
  const version = systemInfo && systemInfo.getIn(['ns2:system_info_common', 'version']);
  if (!version) {
    return '';
  }
  const major = version.get('major');
  const minor = version.get('minor');
  const patch = version.get('patch');
  let buildLink = null;
  if (!cspaceUI.isProduction || isSnapshot(systemInfo)) {
    const commitHash = version.get('build');
    buildLink = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, ' ', "(", /*#__PURE__*/_react.default.createElement("a", {
      href: `https://github.com/collectionspace/services/commits/${commitHash}`,
      target: "_blank",
      rel: "noreferrer"
    }, commitHash), ")");
  }
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, major, ".", minor, ".", patch, buildLink);
};
const renderUIVersion = systemInfo => {
  let buildLink = null;
  if (!cspaceUI.isProduction || isSnapshot(systemInfo)) {
    const commitHash = cspaceUI.buildNum;
    const repoUrl = cspaceUI.repositoryUrl.replace(/\.git$/, '');
    buildLink = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, ' ', "(", /*#__PURE__*/_react.default.createElement("a", {
      href: `${repoUrl}/commits/${commitHash}`,
      target: "_blank",
      rel: "noreferrer"
    }, commitHash), ")");
  }
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, cspaceUI.packageVersion, buildLink);
};
const renderPluginVersion = (systemInfo, pluginInfo) => {
  const {
    packageVersion,
    buildNum,
    repositoryUrl,
    version
  } = pluginInfo;
  let buildLink = null;
  if (buildNum && (!cspaceUI.isProduction || isSnapshot(systemInfo))) {
    const commitHash = buildNum;
    const repoUrl = repositoryUrl ? repositoryUrl.replace(/\.git$/, '') : '';
    const commitLink = repoUrl ? /*#__PURE__*/_react.default.createElement("a", {
      href: `${repoUrl}/commits/${commitHash}`,
      target: "_blank",
      rel: "noreferrer"
    }, commitHash) : commitHash;
    buildLink = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, ' ', "(", commitLink, ")");
  }
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, packageVersion || version, buildLink);
};
const renderPluginInfo = (systemInfo, config, intl) => {
  const {
    pluginInfo
  } = config;
  if (!pluginInfo) {
    return null;
  }
  return Object.keys(pluginInfo).map(name => {
    const info = pluginInfo[name];
    const {
      messages: pluginMessages
    } = info;
    return /*#__PURE__*/_react.default.createElement("li", {
      key: name
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.version, {
      values: {
        version: renderPluginVersion(systemInfo, info),
        name: intl.formatMessage(pluginMessages.name)
      }
    })));
  });
};
function Footer(props) {
  const {
    config,
    intl,
    systemInfo
  } = props;
  const thisYear = new Date().getFullYear().toString();
  let statusItem;
  if (systemInfo) {
    if (systemInfo.get('error')) {
      statusItem = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.notConnected, {
        values: {
          serverUrl: config.serverUrl
        }
      }));
    } else {
      const releaseVersion = renderServicesReleaseVersion(systemInfo);
      if (releaseVersion) {
        statusItem = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.release, {
          values: {
            version: releaseVersion
          }
        }));
      }
    }
  }
  return /*#__PURE__*/_react.default.createElement("footer", {
    className: _Footer.default.common
  }, /*#__PURE__*/_react.default.createElement("ul", null, /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.copyright, {
    values: {
      startYear: '2009',
      endYear: thisYear
    }
  }))), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("a", {
    href: "http://www.collectionspace.org"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.about))), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("a", {
    href: intl.formatMessage(messages.feedbackUrl)
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.feedback)))), /*#__PURE__*/_react.default.createElement("ul", null, /*#__PURE__*/_react.default.createElement("li", null, statusItem), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.version, {
    values: {
      name: intl.formatMessage(messages.appName),
      version: renderUIVersion(systemInfo)
    }
  }))), renderPluginInfo(systemInfo, config, intl)));
}
Footer.propTypes = propTypes;