import React from 'react';
import { Button, Glyphicon } from 'react-bootstrap';
import moment from 'moment';
import { Link } from 'react-router-dom';
import { INVESTIGATION_DATE_FORMAT } from '../../constants';

export function getInstrumentName(investigation) {
  if (!investigation) {
    return undefined;
  }

  const { investigationInstruments } = investigation;
  return (
    investigationInstruments && investigationInstruments[0]?.instrument?.name
  );
}

export function dateFormatter(date) {
  return date ? moment(date).format(INVESTIGATION_DATE_FORMAT) : '';
}

export function beamlineFormatter(investigation, placeHolder = '') {
  const instrumentName = getInstrumentName(investigation);

  return instrumentName ? instrumentName.toUpperCase() : placeHolder;
}

export function nameFormatter(
  investigation,
  withProposalLinks,
  isUserAdministrator
) {
  if (withProposalLinks || isUserAdministrator) {
    return (
      <Link to={`/investigation/${investigation.id}/datasets`}>
        <Button bsSize="xsmall" style={{ width: 120, textAlign: 'left' }}>
          <Glyphicon glyph="circle-arrow-right" />
          <span style={{ marginLeft: 10 }}>{investigation.name} </span>
        </Button>
      </Link>
    );
  }
  return <span style={{ fontWeight: 'bold' }}>{investigation.name}</span>;
}
