"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 || 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); }

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.object,
  intl: _reactIntl.intlShape.isRequired,
  systemInfo: _propTypes.default.instanceOf(_immutable.default.Map)
};

const formatReleaseVersion = systemInfo => {
  const version = systemInfo && systemInfo.getIn(['ns2:system_info_common', 'version']);

  if (!version) {
    return '';
  }

  const major = version.get('major');
  const minor = version.get('minor');
  return "".concat(major, ".").concat(minor);
};

const renderPluginInfo = (config, intl) => {
  const {
    pluginInfo
  } = config;

  if (!pluginInfo) {
    return null;
  }

  return Object.keys(pluginInfo).map(name => {
    const {
      messages: pluginMessages,
      version
    } = pluginInfo[name];
    return _react.default.createElement("li", {
      key: name
    }, _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.version, {
      values: {
        version,
        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 = _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.notConnected, {
        values: {
          serverUrl: config.serverUrl
        }
      }));
    } else {
      const releaseVersion = formatReleaseVersion(systemInfo);

      if (releaseVersion) {
        statusItem = // TODO: Make release pages in the new wiki.
        // <a href={`https://wiki.collectionspace.org/display/collectionspace/Release+${releaseVersion}`}>
        _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.release, {
          values: {
            version: releaseVersion
          }
        })) // </a>
        ;
      }
    }
  }

  return _react.default.createElement("footer", {
    className: _Footer.default.common
  }, _react.default.createElement("ul", null, _react.default.createElement("li", null, _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.copyright, {
    values: {
      startYear: '2009',
      endYear: thisYear
    }
  }))), _react.default.createElement("li", null, _react.default.createElement("a", {
    href: "http://www.collectionspace.org"
  }, _react.default.createElement(_reactIntl.FormattedMessage, messages.about))), _react.default.createElement("li", null, _react.default.createElement("a", {
    href: intl.formatMessage(messages.feedbackUrl)
  }, _react.default.createElement(_reactIntl.FormattedMessage, messages.feedback)))), _react.default.createElement("ul", null, _react.default.createElement("li", null, statusItem), _react.default.createElement("li", null, _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.version, {
    values: {
      name: intl.formatMessage(messages.appName),
      version: typeof cspaceUI === 'undefined' ? '' : cspaceUI.packageVersion
    }
  }))), renderPluginInfo(config, intl)));
}

Footer.propTypes = propTypes;