import React from 'react';
import PropTypes from 'prop-types';
import {NavLink} from 'react-router-dom';
import classNames from 'classnames';

import {getViewType} from '../../utils/site_utils';

import './navbar_header_item.scss';

const NavbarHeaderItem = props => {
  const appliedClass = `navbar-group__header--${props.menuItem.template_opts.bg_color}`;
  const viewType = getViewType();

  if (viewType === 'desktop') {
    return (
      <NavLink
        key={props.menuItem.template_content.url}
        onMouseEnter={props.onEnter}
        className={classNames(
          'navbar-group__header',
          {
            [appliedClass]:
              props.menuItem.template_opts.bg_color && props.isActive
          },
          {'navbar-group__header--inactive': !props.isActive}
        )}
        to={props.menuItem.template_content.url}
        exact
      >
        {props.menuItem.template_content.label.body}
      </NavLink>
    );
  } else {
    return (
      <section
        onClick={props.onEnter}
        className={classNames(
          'navbar-group__header',
          {
            [appliedClass]:
              props.menuItem.template_opts.bg_color && props.isActive
          },
          {'navbar-group__header--inactive': !props.isActive}
        )}
      >
        {props.menuItem.template_content.label.body}
        <span>{'>'}</span>
      </section>
    );
  }
};

NavbarHeaderItem.propTypes = {
  menuItem: PropTypes.shape({
    active: PropTypes.bool,
    name: PropTypes.string,
    menu_item_type: PropTypes.string,
    model_id: PropTypes.string,
    target_includes: PropTypes.arrayOf(PropTypes.string),
    template_content: PropTypes.shape({
      url: PropTypes.string,
      label: {body: PropTypes.string},
      desktop: {
        units: PropTypes.arrayOf(
          PropTypes.shape({
            element_type: PropTypes.string,
            lists: PropTypes.arrayOf(
              PropTypes.shape({
                element_type: PropTypes.string,
                label: {body: PropTypes.string},
                url: PropTypes.string,
                items: PropTypes.arrayOf(
                  PropTypes.shape({
                    element_type: PropTypes.string,
                    label: {body: PropTypes.string},
                    url: PropTypes.string
                  })
                )
              })
            )
          })
        )
      },
      mobile: {
        units: PropTypes.arrayOf(
          PropTypes.shape({
            element_type: PropTypes.string,
            lists: PropTypes.arrayOf(
              PropTypes.shape({
                element_type: PropTypes.string,
                label: {body: PropTypes.string},
                url: PropTypes.string,
                items: PropTypes.arrayOf(
                  PropTypes.shape({
                    element_type: PropTypes.string,
                    label: {body: PropTypes.string},
                    url: PropTypes.string
                  })
                )
              })
            )
          })
        )
      }
    }),
    template_opts: {bg_color: PropTypes.string}
  }),
  isActive: PropTypes.bool,
  onEnter: PropTypes.func
};

export default NavbarHeaderItem;
