// import

import styles from './NodeProjectLink.css';

import React, {Component} from 'react';
import cn from 'classnames';
import pt from 'prop-types';
import _ from 'lodash';

import MoreLink from '../MoreLink/MoreLink';

// fns

export function getNodeProject(node) {
  return _.get(node, 'series.meta.category') === 'standard' ? _.get(node, 'series') : _.get(node, 'book');
}

// component

export default class NodeProjectLink extends Component {
  static propTypes = {
    className: pt.any,
    color: pt.string,
    node: pt.object,
  };

  static defaultProps = {
    color: 'purple',
  };

  render() {
    const {node, color, className, ...props} = this.props;
    const project = getNodeProject(node);

    if (!project) {
      return null;
    }

    const to = project.type === 'series' ? `/books/${project.slug}` : `/books/${node.series.slug}/${project.slug}`;

    return (
      <MoreLink {...props} className={cn(styles.NodeProjectLink, className)} color={color} prefetch to={to}>{project.title}</MoreLink>
    );
  }
}
